rails 3电子邮件发送问题
我正在使用Rails 3并实现电子邮件发送function。 我不确定我的配置是否正确,但这是我的代码:
邮寄/ user_mailer.rb
class UserMailer "user@gmail.com" def send_to(user) @user = user subject='welcome !' mail(:to=>'y.lan@gmail.com', :subject=>subject, :content_type => "text/html") mail.deliver end end
控制器 :
def CarsController < BaseController ... def register_finish UserMailer.send_to(user) end end
配置/ 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(执行过期)错误消息,可能是什么原因?
我看到有些人在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”方法。
我的问题 :
-
我的实现和我从互联网上找到的上述不同实现方式之间有什么区别。
-
为什么我有超时错误?
我也使用gmail作为我的smtp服务器,我将setup_email.rb加到包含此代码的initiliazers中
ActionMailer::Base.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :domain => "domain.pl", :user_name => "username", :password => "password", :authentication => "plain", :enable_starttls_auto => true }
它对我有用:)
编辑
我刚刚注意到我们正在使用不同的服务器,也许尝试使用我的配置?
超时错误意味着存在一些身份validation错误。
不再需要此行:
ActionMailer::Base.delivery_method = :smtp
虽然建议在初始化程序中设置smtp_settings。
如果您在开发计算机上使用它,则此配置应与gmail一起使用:
ActionMailer::Base.smtp_settings = { :enable_starttls_auto => true, :address => 'smtp.gmail.com', :port => 587, :domain => 'your_domain', :authentication => :plain, :user_name => 'your_gmail_username', :password => 'your_gmail_password' }
编辑
您可以为开发机器添加:
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
关于主题的非常好的railscast
看看这个: http : //lindsaar.net/2010/3/15/how_to_use_mail_and_actionmailer_3_with_gmail_smtp
适合我