How to configure action mailer (should I register domain)?
The configuration of your mailer should/can be defined in both development
and production
the purpose of this configuration is that when you set this up when you use the actionmailer
these SMTP options will be used. You could have a simple mailer like the following:
Mailer
class UserMailer < ActionMailer::Base
default :from => DEFAULT_FROM
def registration_confirmation(user)
@user = user
@url = "http://portal.herokuapp.com/login"
mail(:to => user.email, :subject => "Registered")
end
end
Controller
def create
@title = 'Create a user'
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
redirect_to usermanagement_path
flash[:success] = 'Created successfully.'
else
@title = 'Create a user'
render 'new'
end
end
So what happens here is that when the create
action is being used this fires the mailer UserMailer
Looking at the above UserMailer it uses the ActionMailer as the base. Following the SMTP setup shown below which can be defined in both config/environments/production.rb
and development.rb
You would have the following:
config.action_mailer.default_url_options = { :host => 'portal.herokuapp.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com',
:user_name => 'EMAIL_ADDRESS@gmail.com',
:password => 'pass',
:authentication => 'login',
:enable_starttls_auto => true
}
If you want to define the SMTP settings in development mode you would replace
config.action_mailer.default_url_options = { :host => 'portal.herokuapp.com' }
with
config.action_mailer.default_url_options = { :host => 'IP ADDRESS HERE:3000' }
This should be a thorough enough explanation to kick start you in the right direction.
What is the :domain symbol referring to when configuring action mailer?
The :domain
key is set up for HELO
checking. You don't need to specify this if you're using GMail.
The STARTTLS call starts an encrypted connection with your mail server, which is required to use GMail's SMTP.
Setting up a Gmail Account to work with ActionMailer in Rails 3
If you are using the development environment, change the development.rb to raise delivery errors, with
config.action_mailer.raise_delivery_errors = true
Also, the problem might be that :user_name should be the entire email address (myemail@gmail.com), that's how Gmail authenticates users.
:domain, :user_name, and :password in ActionMailer
Here is an example of setting up ActionMailer to use GMAil to send mail:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'gmail.com',
:user_name => 'my_user_name@gmail.com',
:password => 'my_gmail_password',
:authentication => 'plain',
:enable_start_tts_auto => true
}
The domain should be "gmail.com" and the username and password should be those of your Google account. To better protect your main GMail account, you should setup a separate GMail account just for sending email.
Rails contact form send email to myself
You can clone the git repo at
https://github.com/RailsApps/learn-rails
to get the code from the book Learn Ruby on Rails. You'll see that the code works as implemented.
If you look at the example code, the SMTP settings are configured in the file config/environments/development.rb and config/environments/production.rb.
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
The GMAIL_USERNAME and GMAIL_PASSWORD set up the SMTP origin for the mail.
The UserMailer
code only creates (part of) the header and body of the email message. The "from" and "to" will be displayed, for appearances only. Have a look at the raw email message and you will see the full set of headers, that show the real origin of the email.
So, in short, the UserMailer
code sets a fake "from" and the real "from" is set when the email is sent from the Gmail account.
Rails & Devise: how to configure mail with domain name automatically?
in your devise configuration, usually config/initializers/devise.rb
you can configure the mail-sender for devise. this configuration takes a proc, so that it's possible to evaluate something at runtime.
Devise.setup do |config|
config.mailer_sender = Proc.new { your_magic_here }
end
Related Topics
Adding Keywords to Ruby Syntax Highlighting for Notepad++
Active Admin Scopes for Each Instance of a Related Model
Can't Convert Symbol into String
Running Multiple Background Parallel Jobs with Rails
Customize Error Message with Simple_Form
Rails: Find Rows Without Connection in Habtm Relation
Deploy with Capistrano Failing
How Does Pack() and Unpack() Work in Ruby
Get Title, Content via Link in Rails
Stream Multiple Body Using Async Sinatra
How to Split String into Only Two Parts with a Given Character in Ruby
Why Is Ruby's Date Class Automatically Loaded But Datetime Is Not
Get Text Directly Inside a Tag in Nokogiri
Heroku - How to Write into "Tmp" Directory
How Are Array.Each and Array.Map Different
Can't Run Jenkins Build - Bundle: "Command Not Found"
What's the Difference Between Ruby Core API and Standard Library API