In terminal:
rails g mailer UserMailer
or create a mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
layout 'mailer_default'
def user_email(user)
body { user: user, url: "http://example.com/login" }
charset "utf-8"
content_type "text/html"
from Settings.mailer.from
recipients user.email
sent_on Time.now
subject "User Email Example"
end
end
create a layout for emails as views/layouts/mailer_default.html.haml
!!!
%html{ lang: "en" }
%head
%body
#wrapper
#header
= render :partial => "shared/mailer_header_default"
#content
= yield
#footer
= render :partial => "shared/mailer_footer_default"
create a partial for the email header at views/shared/_mailer_header_default.html.haml
%p
This is header content
create a partial for the email footer at views/shared/_mailer_footer_default.html.haml
%p
This is footer content
create a view for emails as views/user_mailer/user_email.html.haml
%p
This is the content of the User Email
Add to config/environments.rb
ActionMailer::Base.register_template_extension('haml')
Add to file config/settings.yml
mailer:
from: "FirstName LastName <[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
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
}