簡體   English   中英

Rails Action Mailer 不發送電子郵件

[英]Rails Action Mailer not sending email

我是一個完整的 Rails 初學者,我正在嘗試在有人使用 Action Mailer 注冊后發送電子郵件。

我的日志說電子郵件正在發送,但 Gmail 從未收到。

配置/初始化程序/setup_mail.rb

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => "asciicasts.com",
  :user_name            => "asciicasts",
  :password             => "secret",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

郵件程序/user_mailer.rb

class UserMailer < ActionMailer::Base
  default :from => "eifion@asciicasts.com"

  def registration_confirmation(user)
    mail(:to => user.email, :subject => "Registered")
  end
end

控制器/users_controller.rb

...
def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
...

謝謝!

確保您在config/environments/development.rb設置了此選項:

config.action_mailer.delivery_method = :smtp

此外,在ActionMailer::Base.smtp_settings您需要指定一個有效的 gmail 帳戶。 復制粘貼(asciicasts)不會在這里剪切。

請參閱此問題以供參考: 在開發環境中使用 Rails 3 發送郵件

您可以使用“sendmail”代替“smtp”

ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
     :port => "587", :domain => "gmail.com", :user_name => "xxx@gmail.com", 
    :password => "yyy", :authentication => "plain", :enable_starttls_auto => true }

對於我設置的新郵件程序,我遇到了同樣的問題。 我終生無法弄清楚為什么這個新郵件程序無法發送電子郵件,甚至在我瀏覽郵件程序時無法找到郵件程序中的方法。

解決方案

最終結果是,如果您將deliver_nowdeliver_now deliver*代碼放在郵件程序中,它就不會發送電子郵件。

示例損壞

  def email_message()
    message = mail(to: User.first, subject: 'test', body: "body text for mail")
    message.deliver_now
  end

更正

  #Different class; in my case a service
  def caller
    message = MyMailer.email_message
    message.deliver_now
  end

  def email_message()
    mail(to: User.first, subject: 'test', body: "body text for mail")
  end

這為我解決了問題,我希望它為其他人解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM