Created
November 12, 2012 03:57
-
-
Save clauda/4057429 to your computer and use it in GitHub Desktop.
Rack with mail on heroku sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'mail' | |
Mail.defaults do | |
delivery_method :smtp, | |
{ address: "smtp.sendgrid.net", | |
port: 587, | |
domain: 'heroku.com', | |
user_name: ENV['SENDGRID_USERNAME'], | |
password: ENV['SENDGRID_PASSWORD'], | |
authentication: 'plain', | |
enable_starttls_auto: true } | |
end | |
class MailCarrier | |
attr_accessor :name, :email, :subject, :message, :to | |
class InvalidParams < StandardError; end | |
def initialize params | |
raise InvalidParams, "for God sake" if params['email'] == "" | |
self.to = "[email protected]" | |
self.subject = "Contacto Subject" | |
self.name = params['name'] | |
self.email = params['email'] | |
self.message = params['message'] | |
end | |
# trash | |
def compose | |
body = <<END_OF_MESSAGE | |
From: #{self.name} <#{self.email}> | |
To: Inovv <#{self.to}> | |
Subject: #{self.subject} | |
MIME-Version: 1.0 | |
Content-Type: text/plain | |
Date: #{Time.now} | |
#{self.message} | |
END_OF_MESSAGE | |
end | |
def deliver | |
mail = self | |
begin | |
Mail.deliver do | |
to mail.to | |
from "#{mail.name} <#{mail.email}>" | |
subject "#{mail.subject}" | |
text_part do | |
body "#{mail.message}" | |
end | |
end | |
# old | |
# require 'net/smtp' | |
# Net::SMTP.start('smtp.sendgrid.net'], 587, 'heroku.com', ENV['SENDGRID_USERNAME'], ENV['SENDGRID_PASSWORD'], :plain) do |smtp| | |
# smtp.sendmail self.message, self.email, self.to | |
# end | |
rescue => e | |
raise "Troll: #{e} " | |
end | |
end | |
private | |
# trash | |
# sendgrid api | |
def json | |
# "to=#{self.to}&toname=#{self.name}&subject=#{self.subject}&text=#{self.message}&from=#{self.email}&api_user=#{VARS[:user_name]}&api_key=#{VARS[:password]}" | |
# https://sendgrid.com/api/mail.send.json | |
{ | |
body: { status: 'OK' }, | |
status: 200, | |
to: self.to, | |
toname: self.name, | |
subject: self.subject, | |
text: self.message, | |
from: self.email, | |
api_user: VARS[:user_name], | |
api_key: VARS[:password] | |
}.to_json | |
end | |
end | |
class Mailer | |
def call env | |
request = Rack::Request.new(env) | |
case request.request_method | |
when 'POST' | |
begin | |
mail = MailCarrier.new(request.params) | |
rescue MailCarrier::InvalidParams => error | |
# [200, {"Content-Type" => "application/json"}, [{status: 200, message: error.message}.to_json]] | |
[200, {"Content-Type" => "text/plain"}, [error.message] ] | |
else | |
mail.deliver | |
# [200, {"Content-Type" => "application/json"}, [{status: 200, message: "Success"}.to_json]] | |
[200, {"Content-Type" => "text/plain"}, ["Email enviado com sucesso"]] | |
# sendgrid api | |
# [200, {"Content-Type" => "application/json"}, [mail.json]] | |
end | |
else | |
[404, {}, ["Lost"]] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
config.ru