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.