Ruby-on-rails rails,动作邮件不发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16754229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails, action mailer not sending
提问by Pavel Babin
I am complete beginner in Rails and I'm trying to send an email after someone signs up using actionmailer.
我是 Rails 的完全初学者,我正在尝试在有人使用 actionmailer 注册后发送电子邮件。
My logs say that email is sending, but gmail never gets it
我的日志说电子邮件正在发送,但 gmail 从未收到
config/initializers/setup_mail.rb
配置/初始化程序/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
}
mailers/user_mailer.rb
邮件程序/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end
controllers/users_controller.rb
控制器/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
...
Thanks!
谢谢!
回答by mihai
Make sure you have this option set in your config/environments/development.rb:
确保您在您的config/environments/development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.delivery_method = :smtp
Also, in ActionMailer::Base.smtp_settingsyou need to specify a valid gmail account. Copy-pasting (asciicasts) is not gonna cut it here.
此外,ActionMailer::Base.smtp_settings您需要指定一个有效的 gmail 帐户。复制粘贴(asciicasts)不会在这里剪切。
See this question for reference: Sending mail with Rails 3 in development environment
请参阅此问题以供参考:在开发环境中使用 Rails 3 发送邮件
回答by pinku
Instead of 'smtp' you can use 'sendmail'
您可以使用“sendmail”代替“smtp”
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
:port => "587", :domain => "gmail.com", :user_name => "[email protected]",
:password => "yyy", :authentication => "plain", :enable_starttls_auto => true }
回答by gwnp
I ran into this same problem for a new mailer I had setup. I couldn't figure out for the life of me why this new mailer couldn't send emails, or even get to the method in the mailer when I stepped through it.
对于我设置的新邮件程序,我遇到了同样的问题。我终生无法弄清楚为什么这个新邮件程序无法发送电子邮件,甚至在我浏览邮件程序时无法找到邮件程序中的方法。
Solution
解决方案
It ended up being that if you put the deliver_nowor deliver*code within the mailer, it does not send the email.
结果是,如果您将deliver_now或deliver*代码放入邮件程序中,则它不会发送电子邮件。
Example Broken
示例损坏
def email_message()
message = mail(to: User.first, subject: 'test', body: "body text for mail")
message.deliver_now
end
Corrected
更正
#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
This solved the problem for me, I hope it solves it for someone else.
这为我解决了问题,我希望它为其他人解决了问题。

