简体   繁体   中英

Error “An SMTP To address is required to send a message” on Learn-Rails Tutorial

I keep getting error on "Send Mail" section on the Learn Ruby on Rails tutorial.

I have tried to clone https://github.com/RailsApps/learn-rails.git on my local machine but the issue is still here.

Below is my code:

user_mailer.rb

class UserMailer < ApplicationMailer
  default from: "do-not-reply@example.com"

  def contact_email(contact)
    @contact = contact
    mail(to: Rails.application.secrets.owner_email, from: @contact.email, :subject => "Website Contact")
  end
end

development.rb

config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: Rails.application.secrets.domain_name,
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: Rails.application.secrets.email_provider_username,
    password: Rails.application.secrets.email_provider_password
  }
  # ActionMailer Config
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.raise_delivery_errors = true
  # Send email in development mode?
  config.action_mailer.perform_deliveries = true

secrets.yml

development:
  email_provider_username: <%= ENV["GMAIL_USERNAME"] %>
  email_provider_password: <%= ENV["GMAIL_PASSWORD"] %>
  mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
  mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
  domain_name: example.com
  owner_email: <%= ENV["OWNER_EMAIL"] %>

I have also made sure that the environment variables were set up correctly on .bashrc file.

Any help will be greatly appreciated, and thank you in advance!

For development, you can use the letter opener gem. Once you send an email it'll automatically open in your default browser.

Add the following to the development section of your Gemfile.

gem 'letter_opener'

I too was having the identical issue. I tried everything from disabling 2-step verification (Google) to hardcoding my credentials in the secrets.yml with zero success.

I used my outlook.com credentials and it worked perfectly.

Note: this requires a change in development.rb

config.action_mailer.smtp_settings = {       
    address : "smtp.live.com"
    # everything else is identical.
}

Setting it for gmail

ActionMailer::Base.delivery_method = :smtp
    ActionMailer::Base.smtp_settings = {
      :address => "smtp.gmail.com",
      :domain => "gmail.com",
      :port => 587, 
      :user_name => "username",
      :password => "password",
      :authentication => :plain,
      :enable_starttls_auto => true,
      :openssl_verify_mode => 'none' 
    }

make it true config.action_mailer.raise_delivery_errors = true it will tell if any error occurs.

Also try port 25

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