Skip to content

Instantly share code, notes, and snippets.

@gertig
Last active January 21, 2019 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gertig/af53e5d4f7a35bbc85b5970a53bab17e to your computer and use it in GitHub Desktop.
Save gertig/af53e5d4f7a35bbc85b5970a53bab17e to your computer and use it in GitHub Desktop.
Custom Devise Mailer and views using Mailgun and Premailer
class RmtwrkDeviseMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
default from: 'RMTWRK <rmtwrk@inflowhq.com>'
default reply_to: 'RMTWRK <rmtwrk@inflowhq.com>'
layout 'mailer_rmtwrk'
#############################
# CONFIRMATION EMAIL
# overrides Devise's confirmation_instructions method
#############################
def confirmation_instructions(record, token, opts={})
@individual = record
@token = token
@to_email = @individual.email
template = render_to_string(template: "mailers/rmtwrk/confirmation_instructions")
premailer = Premailer.new(template, :with_html_string => true, :warn_level => Premailer::Warnings::SAFE)
mg_client = Mailgun::Client.new
mb_obj = Mailgun::MessageBuilder.new
mb_obj.from("RMTWRK <rmtwrk@wrkhq.com>")
mb_obj.add_recipient(:to, @to_email, {'first' => "#{@individual.first_name}", 'last' => "#{@individual.last_name}"})
mb_obj.subject("RMTWRK - email confirmation instructions")
# mb_obj.body_text("Plaint text email goes here")
mb_obj.body_html((premailer.to_inline_css).to_str)
# mg_client.send_message(Rails.application.credentials.dig(Rails.env.to_sym, :mailgun, :domain), mb_obj)
mg_client.send_message("mailgun.domain.url/goes/here", mb_obj)
end
end
<!-- views/layouts/mailer_rmtwrk.html.erb -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width"/>
<style type="text/css">
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: local('Roboto'), local('Roboto-Regular'), url(https://fonts.gstatic.com/s/roboto/v18/KFOmCnqEu92Fr1Mu4mxK.woff2) format('woff2');
}
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 500;
src: local('Roboto Medium'), local('Roboto-Medium'), url(https://fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmEU9fBBc4.woff2) format('woff2');
}
body,
.body {
font-family: 'Roboto', sans-serif;
color: #1C1C1C;
font-weight: 400;
font-size: 14px;
}
.logo {
margin-bottom: 8px;
}
.even-more-styles {
// etc.
}
</style>
</head>
<body>
<a href="https://rmtwrk.com"><img class="logo" src="https://s3.amazonaws.com/inflow-public/rmtwrkEmailLogo.png" width="64"/></a>
<%= yield %>
</body>
</html>
<!-- views/mailers/rmtwrk/confirmation_instructions.html.erb -->
<div class="secondary-text light-gray space-below">Just need to confirm your email!</div>
<div class="secondary-text light-gray">You can activate your RMTWRK subscription through the link below:</div>
<div class="secondary-text space-below"><%= link_to 'Confirm my email address', @individual.devise_confirmation_url(@token) %></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment