문제

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.

다른 팁

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)?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top