Skip to content

Instantly share code, notes, and snippets.

@clauda
Created November 12, 2012 03:57
Show Gist options
  • Save clauda/4057429 to your computer and use it in GitHub Desktop.
Save clauda/4057429 to your computer and use it in GitHub Desktop.
Rack with mail on heroku sample
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 = "your@email.com"
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
@clauda
Copy link
Author

clauda commented Nov 12, 2012

config.ru

require './simple_rack_mail'

use Rack::Static, 
      :urls => ["/images", "/javascripts", "/stylesheets"], 
      :root => Dir.pwd

run lambda { |env|
  [200,
    { 'Content-Type'  => 'text/html',
      'Cache-Control' => 'public, max-age=86400'},
    File.open('index.html', File::RDONLY)
  ]
}

map '/mail' do
  run Mailer.new
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment