I'm following a paid tutorial exactly and setting up a contact page where when a visitors fills out the form and submits, I, the website owner, get an email with their name, email, and comment. The goal is to allow me to easily hit reply and respond to them.
This seems weird to me, because it's odd that ActionMailer gives you the capability to send from someone else's email account for which there are no SMTP settings defined. In fact, following this tutorial, I don't need to declare any SMTP settings.
But it's not working... would love some troubleshooting help.
My mailer code:
class UserMailer < ActionMailer::Base
def contact_email(contact)
@contact = contact
mail(to: jdong8@gmail.com, from: @contact.email, :subject => "New message at JamesDong.com")
end
end
Controller code snippet:
def create
@contact= Contact.new(secure_params)
if @contact.save
UserMailer.contact_email(@contact).deliver
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.