Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'mail'
- Mail.defaults do
- delivery_method :smtp, {
- :address => 'smtp.sendgrid.net',
- :port => '587',
- :user_name => ENV['SENDGRID_USERNAME'],
- :password => ENV['SENDGRID_PASSWORD'],
- :authentication => 'plain',
- :enable_starttls_auto => true,
- :domain => ENV['SENDGRID_DOMAIN']
- }
- end
- module Rack
- class Emailer
- def initialize(app)
- @app = app
- end
- def call(env)
- request = Rack::Request.new(env)
- if request.path == '/send' && request.post?
- deliver_mail(request.params)
- return [301, {'Location' => request.base_url, 'Content-Type' => 'text/html'}, ['']]
- end
- @app.call(env)
- end
- def deliver_mail(params)
- begin
- from = "#{params['name']} <#{params['email']}>"
- message = "#{params['message']}"
- puts "Sending email"
- Mailer.deliver(from, "Blog contact form", message)
- rescue Exception => e
- puts "Mail send fail #{e}"
- end
- end
- end
- end
- module Mailer
- def self.deliver(msg_sender, msg_subject, msg_body)
- Mail.deliver do
- to '[email protected]'
- from msg_sender
- subject msg_subject
- body msg_body
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment