Rails 3 邮件发送问题

发布于 2024-10-22 06:02:06 字数 1850 浏览 3 评论 0原文

我正在使用 Rails 3 并实现电子邮件发送功能。我不确定我的配置是否正确,但这是我的代码:

ma​​ilers/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”方法。

我的问题

  1. 我的实现和我从互联网上找到的上述不同实现方式有什么区别。

  2. 为什么我遇到超时错误?

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:

  1. What's the difference between my implementation and the above different way of implementations I found from internet.

  2. Why I got timeout errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

壹場煙雨 2024-10-29 06:02:06

我还使用 gmail 作为我的 smtp 服务器,并且我已将 setup_email.rb 添加到包含此代码的启动器中

ActionMailer::Base.smtp_settings = {
   :address              => "smtp.gmail.com",
   :port                 => 587,
   :domain               => "domain.pl",
   :user_name            => "username",
   :password             => "password",
   :authentication       => "plain",
   :enable_starttls_auto => true
}

,它对我有用:)

编辑

我刚刚注意到我们正在使用不同的服务器,也许尝试使用我的配置?

I'm also using gmail as my smtp server and I've adder setup_email.rb to initiliazers containing this code

ActionMailer::Base.smtp_settings = {
   :address              => "smtp.gmail.com",
   :port                 => 587,
   :domain               => "domain.pl",
   :user_name            => "username",
   :password             => "password",
   :authentication       => "plain",
   :enable_starttls_auto => true
}

and it works for me :)

EDIT

I've just notice we are using different servers, maybe try with my config?

酷遇一生 2024-10-29 06:02:06

超时错误意味着存在一些身份验证错误。

不再需要此行:

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

Timeout errors mean that there is some authentication errors.

This line is no longer needed:

ActionMailer::Base.delivery_method = :smtp

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:

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'
}

EDIT

you can add for a development machine:

ActionMailer::Base.default_url_options[:host] = "localhost:3000"

Very good railscast on subject

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文