Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 4.40 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Send email via Ruby code on Heroku
  2. require 'net/smtp'
  3.  
  4. =begin
  5.     http://apidock.com/ruby/v1_9_3_125/Net/SMTP/start/class
  6.     https://devcenter.heroku.com/articles/config-vars
  7.  
  8.     I added the following config vars to Heroku
  9.     heroku config:add GM_USR=xxxx
  10.     heroku config:add GM_PSW=xxxx
  11. =end
  12.  
  13. class Email
  14.     def initialize (to, from, subject, body)
  15.             @to = to
  16.             @from = from
  17.             @subject = subject
  18.             @body = body
  19.             @message = <<MESSAGE_CONTENT
  20.                 From: User <#{@from}>
  21.                 To: Integralist <#{@to}>
  22.                 MIME-Version: 1.0
  23.                 Content-type: text/html
  24.                 Subject: #{@subject}
  25.                 #{@body}
  26. MESSAGE_CONTENT
  27.  
  28.             @smtp = Net::SMTP.new('smtp.gmail.com', 587)
  29.         end
  30.  
  31.         def send_email
  32.             @smtp.enable_starttls
  33.             @smtp.start('furious-wind-9309.herokuapp.com', ENV['GM_USR'], ENV['GM_PSW'], :login) do |smtp|
  34.             @smtp.send_message(@message, @from, @to)
  35.         end
  36.     end
  37. end
  38.        
  39. email = Email.new('myemail@gmail.com', params[:email], 'test subject', params[:message]);
  40. email.send_mail
  41.        
  42. 2012-06-13T08:01:08+00:00 heroku[router]: POST furious-wind-9309.herokuapp.com/contact dyno=web.1 queue=0 wait=0ms service=628ms status=500 bytes=2060
  43. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
  44. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
  45. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
  46. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
  47. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
  48. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
  49. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
  50. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `block in call'
  51. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1416:in `synchronize'
  52. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call'
  53. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process'
  54. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch'
  55. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process'
  56. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `call'
  57. 2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `block in spawn_threadpool'
  58. 2012-06-13T08:01:08+00:00 app[web.1]: 82.69.39.185 - - [13/Jun/2012 08:01:08] "POST /contact HTTP/1.1" 500 2060 0.6254
  59.        
  60. require 'mail'
  61. Mail.defaults do
  62.   delivery_method :smtp, { :address   => "smtp.sendgrid.net",
  63.                            :port      => 587,
  64.                            :domain    => "yourdomain.com",
  65.                            :user_name => "yourusername@domain.com",
  66.                            :password  => "yourPassword",
  67.                            :authentication => 'plain',
  68.                            :enable_starttls_auto => true }
  69. end
  70.  
  71. mail = Mail.deliver do
  72.   to 'yourRecipient@domain.com'
  73.   from 'Your Name <name@domain.com>'
  74.   subject 'This is the subject of your email'
  75.   text_part do
  76.     body 'Hello world in text'
  77.   end
  78.   html_part do
  79.     content_type 'text/html; charset=UTF-8'
  80.     body '<b>Hello world in HTML</b>'
  81.   end
  82. end
  83.        
  84. require 'net/smtp'
  85.  
  86.     msg = "your message goes here"
  87.     smtp = Net::SMTP.new 'smtp.gmail.com', 587
  88.     smtp.enable_starttls
  89.     smtp.start(YourDomain, YourAccountName, YourPassword, :login) do
  90.       smtp.send_message(msg, FromAddress, ToAddress)
  91.     end
  92.        
  93. @email = Email.new('mark.mcdx@gmail.com', params[:email], 'test subject', params[:message])
  94. @email.send_email