Skip to content

Instantly share code, notes, and snippets.

@lopezjurip
Last active July 19, 2017 09:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lopezjurip/884082f41b76816160e6aaf03ec4cf20 to your computer and use it in GitHub Desktop.
Save lopezjurip/884082f41b76816160e6aaf03ec4cf20 to your computer and use it in GitHub Desktop.
Ruby on Rails - Send (batch) emails using mailgun-ruby (HTTP, not SMTP)

Requisites:

  • Create a free account at https://www.mailgun.com/
  • Save your API Key as a environment variables: export MAILGUN_API_KEY=key-1111111111111111111111111

As example, I will use a model named Request.

# app/mailers/application_mailer.rb
# See: http://blog.mailgun.com/the-official-mailgun-ruby-sdk-is-here/
require 'mailgun'
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
def host
'mydomain.com'
end
def from
'no-reply@mydomain.com'
end
def client
# Lazy initialization
@client ||= Mailgun::Client.new(ENV['MAILGUN_API_KEY'])
end
def batch_builder()
obj = Mailgun::BatchMessage.new(client, host)
obj.set_from_address(from)
obj
end
end
<!-- app/views/layouts/mailer.html.erb -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<%= yield %>
</body>
</html>
app/views/layouts/mailer.text.erb
---
<%= yield %>
<!-- app/views/request_mailer/new_request.html.erb -->
<div>
<p>
Id: <%= request.id %>
</p>
</div>
app/views/request_mailer/new_request.text.erb
---
Id: <%= request.id %>
# app/mailers/request_mailer.rb
class RequestMailer < ApplicationMailer
def new_request(request, users = [])
builder = batch_builder() # From super-class
builder.set_subject("Email title for: #{request.id}")
# Render html version
html = render_to_string(
template: 'request_mailer/new_request',
formats: [:html],
locals: { request: request }
)
# Render plain text version
txt = render_to_string(
template: 'request_mailer/new_request',
formats: [:text],
# layout: false,
locals: { request: request }
)
builder.set_text_body(txt.to_str)
builder.set_html_body(html.to_str)
users.each do |user|
builder.add_recipient(:to, user.email, 'first' => user.name,
'account-id' => user.id)
end
# Send it
builder.finalize
end
end
# app/controllers/requests_controller.rb
class RequestsController < ApplicationController
# ...
# POST /requests
# POST /requests.json
def create
@request = Request.new(request_params)
respond_to do |format|
if @request.save
# Send emails to users with enabled notifications
RequestMailer.new_request(@request, User.where(notifications: true)).deliver_now
format.html { redirect_to @request, notice: 'Request was successfully created.' }
format.json { render :show, status: :created, location: @request }
else
format.html { render :new }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment