0

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
Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
james
  • 3,989
  • 8
  • 47
  • 102

2 Answers2

1

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.

Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
  • Thanks! Quick follow-ups: 1) I am getting msgs "from" the gmail that I set in SMTP. This makes sense, but what line of code exactly is causing this gmail to send? I'm especially curious since the "from" is `@contact.email`, which doesn't appear anywhere (in fact, using a bogus @contact.email works). Related, works fine without the default donotreply@example.com. 2) Is figaro gem just generating an application.yml file? Why can't I do this manually? I assume not using figaro was a problem earlier 3) Sometimes, I get an "execution timeout" error in submission, do you know why? – james Feb 04 '14 at 06:08
0

I could be wrong here, but based off of my experience in Rails this isn't possible, you would need to have the SMTP settings for the account you are sending the mail from.

There are a ton of questions on here referring to setting up ActionMailer correctly, but it seems like you're trying to send mail without telling it where to send the mail from.

This question and the best answer on it might be of some help if that's your issue:

How to configure action mailer (should I register domain)?

Community
  • 1
  • 1
ohmatt
  • 83
  • 1
  • 6
  • Thanks, that seemed weird to me as well, but ... so interesting that this tutorial does it. I'm going to email the author. – james Feb 03 '14 at 19:23