Last active
December 26, 2015 06:39
-
-
Save featherart/7109061 to your computer and use it in GitHub Desktop.
FredMailer or UserMailer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# There are 4 basic steps to implementing your ActionMailer functionality in a Rails app: | |
# Step 1. Create your ActionMailer object. Remember that it can have any name, but should finish with "Mailer": | |
rails g mailer UserMailer | |
class UserMailer < ActionMailer::Base | |
default from: "[email protected]" | |
def confirm_user(user) | |
@user = user | |
@url = 'http://fred.com/login' | |
mail(to: @user.email, subject: 'Welcome to Fred Site') | |
end | |
end | |
# Step 2. Create your HTML and text version of your email body: | |
Text version of greeting email (in views/UserMailer/welcome_email.text.erb ) | |
Welcome to example.com, <%= @user.name %> | |
=============================================== | |
You have successfully signed up to example.com, | |
your username is: <%= @user.login %>. | |
To login to the site, just follow this link: <%= @url %>. | |
Thanks for joining and have a great day! | |
HTML version (in views/UserMailer/welcome_email.html.erb) | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> | |
</head> | |
<body> | |
<h1>Welcome to example.com, <%= @user.name %></h1> | |
<p> | |
You have successfully signed up to example.com, | |
your username is: <%= @user.login %>.<br/> | |
</p> | |
<p> | |
To login to the site, just follow this link: <%= @url %>. | |
</p> | |
<p>Thanks for joining and have a great day!</p> | |
</body> | |
</html> | |
# Step 3. Add the call to your mailing method found in your ActionMailer object to your controller. | |
# This can be any action you want to trigger a message being sent from. The most common place will | |
# be your create method: | |
def create | |
@user = User.new(params[:user]) | |
respond_to do |format| | |
if @user.save | |
# Tell the UserMailer to send a welcome Email after save | |
# only send the message if the user is successfully saved | |
UserMailer.welcome_email(@user).deliver | |
format.html { redirect_to(@user, notice: 'User was successfully created.') } | |
else | |
format.html { render action: 'new' } | |
end | |
end | |
end | |
# Step 4. Update your configuration environment. If you are in development (on your machine) you will use the | |
# development.rb file settings. In Heroku you will use production.rb. The settings will depend on where you | |
# are deploying from | |
Then the configuration files: | |
for local testing in config/environment/development.rb put something like this: | |
config.action_mailer.delivery_method = :sendmail | |
# Defaults to: | |
# config.action_mailer.sendmail_settings = { | |
# location: '/usr/sbin/sendmail', | |
# arguments: '-i -t' | |
# } | |
config.action_mailer.perform_deliveries = true | |
config.action_mailer.raise_delivery_errors = true | |
config.action_mailer.default_options = {from: '[email protected]'} | |
You will use the config/environment/production.rb file for deployment to Heroku, and | |
use the environment variables suggested by SendGrid, or whoever you choose for your | |
3rd party mailer service. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment