Rails 3 邮件发送问题
我正在使用 Rails 3 并实现电子邮件发送功能。我不确定我的配置是否正确,但这是我的代码:
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def send_to(user)
@user = user
subject='welcome !'
mail(:to=>'[email protected]', :subject=>subject, :content_type => "text/html")
mail.deliver
end
end
controller:
def CarsController < BaseController
...
def register_finish
UserMailer.send_to(user)
end
end
config/enviroment.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.googlemail.com",
:port => 532,
:arguments => '-i'
:enable_starttls_auto => true
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
当我的控制器调用“register_finish”函数并尝试向用户发送电子邮件,我总是收到 Timeout::Error (execution expired) 错误消息,可能是什么原因?
我看到有些人在 config/initializers/setup_email.rb 中定义配置并使用,
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = { ...}
而我在 config/enviroment.rb 中配置它并使用:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {...}
我还看到有些人调用当我在“UserMailer”内调用它时,控制器内的“deliver”方法。
我的问题:
我的实现和我从互联网上找到的上述不同实现方式有什么区别。
为什么我遇到超时错误?
I am using Rails 3 and implementing the email sending feature. I am not sure if my configuration is correct, but here are my codes:
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "[email protected]"
def send_to(user)
@user = user
subject='welcome !'
mail(:to=>'[email protected]', :subject=>subject, :content_type => "text/html")
mail.deliver
end
end
controller:
def CarsController < BaseController
...
def register_finish
UserMailer.send_to(user)
end
end
config/enviroment.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.googlemail.com",
:port => 532,
:arguments => '-i'
:enable_starttls_auto => true
}
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
When my controller invoke 'register_finish' function and try to send email to a user, I always get Timeout::Error (execution expired) error message, what could be the reason??
I saw some people define the configuration in config/initializers/setup_email.rb and use
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = { ...}
while I configure it in config/enviroment.rb and use:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {...}
I also saw some people invoke "deliver" method inside controller while I invoke it inside 'UserMailer'.
My questions:
What's the difference between my implementation and the above different way of implementations I found from internet.
Why I got timeout errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还使用 gmail 作为我的 smtp 服务器,并且我已将 setup_email.rb 添加到包含此代码的启动器中
,它对我有用:)
编辑
我刚刚注意到我们正在使用不同的服务器,也许尝试使用我的配置?
I'm also using gmail as my smtp server and I've adder setup_email.rb to initiliazers containing this code
and it works for me :)
EDIT
I've just notice we are using different servers, maybe try with my config?
超时错误意味着存在一些身份验证错误。
不再需要此行:
虽然建议在初始值设定项中设置 smtp_settings。
如果您在开发计算机上使用它,则此配置应该适用于 gmail:
编辑
您可以为开发计算机添加:
关于主题的非常好的railscast
Timeout errors mean that there is some authentication errors.
This line is no longer needed:
While it is adviceable to set the smtp_settings in an initializer.
If you are using it on a development machine this configuration should work with gmail:
EDIT
you can add for a development machine:
Very good railscast on subject
看看这个:
http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp
对我来说效果很好
Have a look at this:
http://lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp
Works nicely for me