【问题标题】:Rails Action Mailer not sending emailRails Action Mailer 不发送电子邮件
【发布时间】:2013-05-21 04:36:16
【问题描述】:

我是 Rails 的完全初学者,我正在尝试在有人使用 Action Mailer 注册后发送电子邮件。

我的日志显示电子邮件正在发送,但 Gmail 从未收到。

config/initializers/setup_mail.rb

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

mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default :from => "eifion@asciicasts.com"

  def registration_confirmation(user)
    mail(:to => user.email, :subject => "Registered")
  end
end

控制器/users_controller.rb

...
def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
...

谢谢!

【问题讨论】:

  • 电子邮件没有在开发模式下发送吗?

标签: ruby-on-rails email actionmailer


【解决方案1】:

确保您在 config/environments/development.rb 中设置了此选项:

config.action_mailer.delivery_method = :smtp

另外,在ActionMailer::Base.smtp_settings 中,您需要指定一个有效的 gmail 帐户。复制粘贴(asciicasts)不会在这里剪切它。

请参阅此问题以供参考:Sending mail with Rails 3 in development environment

【讨论】:

  • 我的代码发送电子邮件.. 但只有少数......就像,我可以收集超过 50 个用户作为收件人,但是,Rails 只发送其中几个......不要知道为什么,没有给出错误...你能帮忙吗?
  • @Hamdan 我真的不知道...可能值得提出一个问题,并提供更多详细信息。
  • @Hamdan,同意 mihai。这是另一个问题的主题,超出了本次对话的范围。
【解决方案2】:

您可以使用“sendmail”代替“smtp”

ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = { :address => "smtp.gmail.com",
     :port => "587", :domain => "gmail.com", :user_name => "xxx@gmail.com", 
    :password => "yyy", :authentication => "plain", :enable_starttls_auto => true }

【讨论】:

    【解决方案3】:

    我为我设置的新邮件程序遇到了同样的问题。我终其一生都无法弄清楚为什么这个新的邮件程序无法发送电子邮件,甚至在我逐步浏览它时也无法找到邮件程序中的方法。

    解决方案

    结果是,如果您将deliver_nowdeliver* 代码放在邮件中,它不会发送电子邮件。

    示例损坏

      def email_message()
        message = mail(to: User.first, subject: 'test', body: "body text for mail")
        message.deliver_now
      end
    

    已更正

      #Different class; in my case a service
      def caller
        message = MyMailer.email_message
        message.deliver_now
      end
    
      def email_message()
        mail(to: User.first, subject: 'test', body: "body text for mail")
      end
    

    这解决了我的问题,我希望它可以解决其他人的问题。

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2011-05-15
      • 2011-07-14
      • 2012-07-25
      • 2021-01-22
      • 2013-04-14
      相关资源
      最近更新 更多