Skip to content

Instantly share code, notes, and snippets.

@beneggett
Last active December 25, 2015 17:49
Show Gist options
  • Save beneggett/7016378 to your computer and use it in GitHub Desktop.
Save beneggett/7016378 to your computer and use it in GitHub Desktop.
Email Basics
Configure your email settings:
In config/environments/development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'localhost:3000',
user_name: ENV['GMAIL_EMAIL'],
password: ENV['GMAIL_PASSWORD'],
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Verify an Email is sending out (Devise Forgot Password)
Create a new Mailer
rails generate mailer UserMailer
Setup a welcome email message (method)
class UserMailer < ActionMailer::Base
default from: "[email protected]"
# Call email using: UserMailer.welcome(user).deliver
def welcome(user)
@user = user
mail(to: @user.email, subject: "Welcome to DevPoint Labs!")
end
end
Create email views (html & text)
app/views/user_mailer/welcome.html.erb
app/views/user_mailer/welcome.text.erb
Verify Email is sending in Rails Console
Automatically send welcome email after a user is created
in User.rb
after_create :send_welcome_email
private
def send_welcome_email
UserMailer.welcome(self).deliver
end
______
Create a layout for your emails
app/views/layouts/email.html.erb
Implement it in your mailers
layout 'email'
Install letter_opener Gem
gem "letter_opener"
bundle install
In development.rb
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment