Skip to content

Instantly share code, notes, and snippets.

@psahni
Forked from wrburgess/gist:3767468
Created July 7, 2014 10:46
Show Gist options
  • Save psahni/f784d68e14fdd48b727b to your computer and use it in GitHub Desktop.
Save psahni/f784d68e14fdd48b727b to your computer and use it in GitHub Desktop.

Generate a Mailer Model and View

In terminal:

rails g mailer UserMailer

or create a mailers/user_mailer.rb

class GroupMailer < ActionMailer::Base
  default from: Settings.mailer.from

  def example_email(user)
    @user = user
    @url  = "http://example.com/login"
    mail(to: user.email, subject: "Example Subject")
  end
end

create a layout for emails as views/layouts/mailer_default.html.haml

!!!
%html{ lang: "en" }
  %head
    %meta{ content: "text/html", charset: "utf-8", http-equiv: "Content-Type" }/
  %body
    #wrapper

create a view for emails as views/user_mailer/user_mailer.html.haml

Designate ActionMailer to recognize HAML files

Add to config/environments.rb

ActionMailer::Base.register_template_extension('haml')

Create Settings for Mailer

Add to file config/settings.yml

mailer:
  from: "Randy Burgess <[email protected]>"
  address: "smtp.gmail.com"
  port: 587
  domain: "gmail.com"
  user_name: <%= ENV["MAILER_USERNAME"] %>
  password: <%= ENV["MAILER_PASSWORD"] %>
  authentication: "plain"
  enable_starttls_auto: true

Initialize Mailer Settings

create a file name config/intializers/mailer_settings.rb

ActionMailer::Base.smtp_settings = {  
  address:              Settings.mailer.address,  
  port:                 Settings.mailer.port,  
  domain:               Settings.mailer.domain,  
  user_name:            Settings.mailer.user_name,  
  password:             Settings.mailer.password,  
  authentication:       Settings.mailer.authentication,  
  enable_starttls_auto: Settings.mailer.enable_starttls_auto  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment