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
.
Requisites:
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 | |
'[email protected]' | |
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 |
Hi,
Is there any way to test the batch sending (from, to, subject) using Rspec mailer specs. We can test the logic of mailers when sending through ActionMailer(SMTP protocol) as like https://relishapp.com/rspec/rspec-rails/v/3-8/docs/mailer-specs/mailer-spec, we have object (mail) to test the mailers using rspec mailer specs. Have you tried writing test cases for the method that sends batch emails through MailGun API)?