Actionmailer not delivering mail, with rails 3

十年热恋 提交于 2019-12-03 23:25:50

Somewhat late, but nevertheless maybe this will save someone a few hours of head banging. This is probably only relevant to sending emails from gmail. First, in order to help debugging the situation, set the following line in development.rb to true (assuming you're in development mode):

config.action_mailer.raise_delivery_errors = true

This will make ActionMailer not to silently ignore errors. When I did that, I realized gmail is refusing my username and password. I then went to my configuration file where I put all the Action Mailer config directives (for me it was in development.rb, there is probably a better place to put it), and noticed that :user_name was set to "admin" rather than "admin@thedomain.com". Changing it solved the problem. Here is my corrected part of development.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  }

References:

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

Another thing not to forget: you have to restart the application after making changes in your environment config files. when using passenger this can quickly be missed :)

that's what solved my "problem" when ActionMailer didnt want to send emails without showing any errors..

The things written here did not help me.

I am using Rails 3.2.8 and I spent several hours trying to figure this out and it was very simple in the end. I forgot to call .deliver() on the Mail::Message object that is returned by mail(:to => @user.email, :subject => 'Welcome to the Site') method call.

Just leave everything like it is specified in official RoR tutorial.

That is, in your development/production environment files, make a section like:

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

And then you subclass ActionMailer::Base, for example like this:

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

After that, you can simply use this InfoMailer method from your code like a class method:

InfoMailer.welcome_user_and_send_password(user, password)

If you're using the test environment, be sure to comment out this line of code in environments/test.rb:

config.action_mailer.delivery_method = :test
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!