Rails 3邮件程序不起作用,也没有记录任何错误 [英] Rails 3 mailer not working and not logging any errors
问题描述
我尝试了各种配置,但仍然无法在Rails开发环境中发送电子邮件。
I have tried all kinds of configurations but still I can't send an email in my development environment from rails.
我安装了mailutils来从命令中尝试行并成功了,我收到了电子邮件(当然是垃圾邮件):echo test |邮件-s主题user@example.com
I installed mailutils to try this from the command line and it worked, I received the email (in spam of course): echo test | mail -s Subject user@example.com
这是我的配置:
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true # still no logs about emails
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true # I can't believe I have to add this option. Does it even exist? I found it on google.
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => :login,
:user_name => "some_user@gmail.com",
:password => "abc123",
}
这是邮件中的代码:
class UserMailer < ActionMailer::Base
default :from => "root@ubuntu"
def test_email
Rails.logger.debug 'test_email'
mail(:to => 'user@example.com', :subject => "testing rails")
end
end
控制器:
class PagesController < ApplicationController
def home
UserMailer.test_email
end
end
development.log:
development.log:
[2012-03-01 18:26:45.859] DEBUG [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] test_email
[2012-03-01 18:26:45.888] INFO [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Rendered user_mailer/test_email (1.6ms)
[2012-03-01 18:26:45.898] INFO [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Rendered pages/home.html.erb within layouts/application (1.1ms)
[2012-03-01 18:26:46.815] INFO [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Completed 200 OK in 455ms (Views: 112.4ms)
我也尝试过使用控制台:
I also tried using the console:
root@ubuntu:/srv/www/myapp# rails c
Loading development environment (Rails 3.2.1)
irb(main):001:0> UserMailer.test_email
=> #<Mail::Message:32110400, Multipart: false, Headers: <To: user@example.com>, <Subject: testing rails>, <Mime-Version: 1.0>, <Content-Type: text/html>>
推荐答案
UserMailer.test_email
只需创建 Mail :: Message
宾语。要实际发送电子邮件,您需要
Just creates a Mail::Message
object. To actually send an email you need to do
UserMailer.test_email.deliver
(或从rails 4.2开始 deliver_now
/ deliver_later
)
(or starting with rails 4.2 deliver_now
/ deliver_later
)
这篇关于Rails 3邮件程序不起作用,也没有记录任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!