Guest User

Untitled

a guest
Jun 22nd, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. require 'mail'
  2. Mail.defaults do
  3. delivery_method :smtp, {
  4. :address => 'smtp.sendgrid.net',
  5. :port => '587',
  6. :user_name => ENV['SENDGRID_USERNAME'],
  7. :password => ENV['SENDGRID_PASSWORD'],
  8. :authentication => 'plain',
  9. :enable_starttls_auto => true,
  10. :domain => ENV['SENDGRID_DOMAIN']
  11. }
  12. end
  13.  
  14. module Rack
  15. class Emailer
  16. def initialize(app)
  17. @app = app
  18. end
  19.  
  20. def call(env)
  21. request = Rack::Request.new(env)
  22. if request.path == '/send' && request.post?
  23. deliver_mail(request.params)
  24. return [301, {'Location' => request.base_url, 'Content-Type' => 'text/html'}, ['']]
  25. end
  26. @app.call(env)
  27. end
  28.  
  29.  
  30. def deliver_mail(params)
  31. begin
  32. from = "#{params['name']} <#{params['email']}>"
  33. message = "#{params['message']}"
  34. puts "Sending email"
  35. Mailer.deliver(from, "Blog contact form", message)
  36. rescue Exception => e
  37. puts "Mail send fail #{e}"
  38. end
  39. end
  40. end
  41. end
  42.  
  43.  
  44. module Mailer
  45. def self.deliver(msg_sender, msg_subject, msg_body)
  46. Mail.deliver do
  47. from msg_sender
  48. subject msg_subject
  49. body msg_body
  50. end
  51. end
  52. end
Advertisement
Add Comment
Please, Sign In to add comment