繁体   English   中英

Actionmailer不使用rails 3传递邮件

[英]Actionmailer not delivering mail, with rails 3

我正在尝试创建一个应用程序,在用户注册时发送电子邮件。

我在config / application.rb文件中输入了gmail的smtp设置,邮件功能看起来像

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

现在,当我看到日志时,我看到它说邮件已被发送,但我从来没有收到任何邮件......

另外,当我调用邮件传递函数Emails.signed(@user).deliver ,表单页面不会重定向,但如果我注释掉电子邮件发送代码,它就可以工作

Emails.signed(@user).deliver

要么

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

谢谢 :)

编辑:development.rb

App::Application.configure do
  # Settings specified here will take precedence over those in config/environment.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

end

有点晚了,但是这可能会让人节省几个小时的敲击声。 这可能仅与从Gmail发送电子邮件有关。 首先,为了帮助调试情况,请将development.rb中的以下行设置为true(假设您处于开发模式):

config.action_mailer.raise_delivery_errors = true

这将使ActionMailer不会默默地忽略错误。 当我这样做时,我意识到gmail拒绝我的用户名和密码。 然后我去了我的配置文件,在那里我放了所有的Action Mailer配置指令(对我来说,它在development.rb中,可能有一个更好的地方放置它),并注意到:user_name设置为“admin”而不是“admin@thedomain.com”。 改变它解决了这个问题。 这是我更正的开发部分.rb:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'thedomain.com',
  :user_name            => 'admin@thedomain.com',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

参考文献:

http://forums.pragprog.com/forums/43/topics/541 http://edgeguides.rubyonrails.org/action_mailer_basics.html

另一件事不要忘记:在环境配置文件中进行更改后,必须重新启动应用程序。 使用乘客时,很快就会错过:)

当ActionMailer不想发送电子邮件而没有显示任何错误时,这就解决了我的“问题”。

这里写的东西对我没有帮助。

我正在使用Rails 3.2.8,我花了几个小时试图解决这个问题,最后它非常简单。 我忘了在mail(:to => @user.email, :subject => 'Welcome to the Site')方法调用返回的Mail::Message对象上调用.deliver()

只需保留官方RoR教程中指定的所有内容即可。

也就是说,在您的开发/生产环境文件中,创建一个类似于的部分:

  # mailer
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'gmail.com',
      user_name:            '<username>@gmail.com',
      password:             '<password>',
      authentication:       'plain',
      enable_starttls_auto: true
  }

然后你继承ActionMailer :: Base的子类,例如:

class InfoMailer < ActionMailer::Base
  default from: "<username>@gmail.com"

  def welcome_user_and_send_password(user, password)
    default_url_options = self.default_url_options()


    @user = user
    @password = password
    @url = default_url_options[:host]
    mail(:to => @user.email, :subject => 'Welcome to the Site').deliver()
  end

end

之后,您可以像使用类方法一样在代码中使用此InfoMailer方法:

InfoMailer.welcome_user_and_send_password(user, password)

如果您正在使用测试环境,请务必在环境/ test.rb中注释掉这行代码:

config.action_mailer.delivery_method = :test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM