Ruby-on-rails 从 Rails 控制台发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3267213/
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
Send email from rails console
提问by Max Williams
I'm trying to send out some mails from the console on my production server, and they're not going out. I can't work out why. I have just your standard email setup with sendmail. When i call the Mailer.deliver_ method i get this back:
我正在尝试从我的生产服务器上的控制台发送一些邮件,但它们没有发送出去。我不明白为什么。我只有您使用 sendmail 的标准电子邮件设置。当我调用 Mailer.deliver_ 方法时,我得到了这个:
#<TMail::Mail port=#<TMail::StringPort:id=0x3fe1c205dbcc> bodyport=#<TMail::StringPort:id=0x3fe1c2059e00>>
EDIT: Added some more info:
编辑:添加了更多信息:
So, for example, i have this line in my controller when a new user signs up, to send them a "welcome" email:
因此,例如,当新用户注册时,我的控制器中有这一行,向他们发送“欢迎”电子邮件:
Mailer.deliver_signup(@user, request.host_with_port, params[:user][:password])
This works fine. I thought that i should be able to do the same thing from the console, eg
这工作正常。我认为我应该能够从控制台做同样的事情,例如
user = User.find(1)
Mailer.deliver_signup(user, "mydomainname.com", "password")
When i do this, i get the Tmail::StringPort object back, but the mail appears to not get sent out (i'm trying to send emails to myself to test this).
当我这样做时,我得到了 Tmail::StringPort 对象,但邮件似乎没有发送出去(我试图给自己发送电子邮件来测试这个)。
I'm on an ubuntu server in case that helps. thanks - max
我在 ubuntu 服务器上以防万一。谢谢 - 最大
采纳答案by Dhiraj
For Sending email from Rails Console first we have to execute this setting in console for action mailer settings.
为了首先从 Rails 控制台发送电子邮件,我们必须在控制台中执行此设置以进行操作邮件程序设置。
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
authentication: 'plain',
enable_starttls_auto: true,
user_name: '[email protected]',
password: 'yourpassword'
}
After that If we execute email sending code it will deliver email.
之后,如果我们执行电子邮件发送代码,它将发送电子邮件。
UserMailer.activation_instructions(@user).deliver_now
回答by jmgarnier
Quicker version:
更快的版本:
ActionMailer::Base.mail(
from: "[email protected]",
to: "[email protected]",
subject: "Test",
body: "Test"
).deliver_now
回答by sscirrus
I ran into a similar problem this morning on a Rails 3 app where I called:
今天早上我在 Rails 3 应用程序上遇到了类似的问题,我打电话给:
UserMailer.activation_instructions(@user)
This gave me the data but did not send the e-mail out. To send, I called:
这给了我数据,但没有发送电子邮件。要发送,我打电话给:
UserMailer.activation_instructions(@user).deliver
This did the trick. Hopefully this might work for you too!
这成功了。希望这也适用于您!
回答by txwikinger
I am not 100% if I understand what you are trying to do.
如果我理解你想要做什么,我不是 100%。
If you try to send out e-mails to the Internet, your sendmail must be configured in a way to forward those e-mails to the proper e-mails server. Depending on which Ubuntu release you use you can just reconfigure the package to do this.
如果您尝试向 Internet 发送电子邮件,则必须将 sendmail 配置为将这些电子邮件转发到正确的电子邮件服务器。根据您使用的 Ubuntu 版本,您可以重新配置包来执行此操作。
You also might think if you want to use procmail instead of sendmail.
您可能还会考虑是否要使用 procmail 而不是 sendmail。
You can reconfigure the e-mail configuration with
您可以重新配置电子邮件配置
dpkg-reconfigure sendmail
of use procmail instead if you use that package. The configuration dialogue gives you some option where you can configure it to forward all mail to the appropriate e-mail server. However, you need to think if you need authentication or if that server just accepts e-mails from your server.
如果您使用该软件包,请改用 procmail。配置对话框为您提供了一些选项,您可以将其配置为将所有邮件转发到适当的电子邮件服务器。但是,您需要考虑是否需要身份验证,或者该服务器是否只接受来自您服务器的电子邮件。

