Skip to content

Instantly share code, notes, and snippets.

@nelsnelson
Created December 10, 2014 00:04
Show Gist options
  • Save nelsnelson/9c9e781092d683f99373 to your computer and use it in GitHub Desktop.
Save nelsnelson/9c9e781092d683f99373 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'pony'
def send_email(to, from, subject, message, attachment=nil)
begin
content = open(attachment, 'rb') { |io| io.read }
basename = File.basename(attachment)
puts basename
mail = Pony.mail(
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => '[email protected]',
:password => 'password',
:authentication => :plain,
:domain => 'unknown.com'
},
:to => to,
:from => from,
:subject => subject,
:body => message,
:attachments => {
basename => content
},
:headers => {
'Content-Type' => 'multipart/mixed',
'Content-Transfer-Encoding' => 'base64',
'Content-Disposition' => 'attachment'
}
)
rescue Exception => ex
puts "Error posting email: #{ex.message}"
return ex.message
end
nil
end
def main
file = 'tmp.dump'
send_email(
'[email protected]',
'[email protected]',
'See attachment',
'This is a file',
file
)
end
main if __FILE__ == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment