简体   繁体   中英

An SMTP To address is required to send a message/Argument Errror

ArgumentError at /contacts An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.

Good Day Guys,I am having trouble debugging this myself and have consumed so much time I needed your help and explanation as to how why it didn't work.

Things I did already: 1.Added gem 'binding_of_caller' 2.bundle install

goal: Is when a visitor submit a contact form it supposed to send me a email message automatically.

My woes/confusion:

1.How do you set your email in secrets.yml or put your email wherein you configured the contact form request directly to your email 2.What I did is put to:myemail.com > secrets.yml both in production and development 3.Am I right?

Please explain this to me as I am going in depth on ruby on rails.

You need to generate a mailer first.

rails g mailer example_mailer

app/mailers/example_mailer.rb This file will be generated by this line.

Then you need to create a method in your mailer where u can define whom to send mail. you can give direct email id or you can take it from database. As in my given example u need to pass user to this method and it will send email to that user's email.

  def sample_email(user)
    @user = user
    mail(to: @user.email, subject: 'Sample Email')
  end

Create a HTML template for how your mail will look

app/views/example_mailer/sample_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Hi <%= @user.name %></h1>
    <p>
      Sample mail sent using smtp.
    </p>
  </body>
</html>

Then you need to configure your smtp in /config/environments/production.rb or /config/environments/development.rb whichever environment you are using.

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => 'username@gmail.com',
 :password             => 'Gmail password',
 :authentication       => "plain",
:enable_starttls_auto => true
}

Then you need to trigger your mail action any any method from where you are sending mails.

ExampleMailer.sample_email(@user).deliver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM