Sending mail with Rails 3 in development environment
Asked Answered
D

8

54

I'm sure this has been asked a million times before but I can't find anything that works for me so I'm asking again!

I just need a way of sending emails using ActionMailer in rails 3. I have followed numerous tutorials including the Railscasts tutorial on the new ActionMailer and I can see the mails being generated but I don't receive them.

I have tried a bunch of different ways but they generally amount to configuring the following settings

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

I have tried the above code (with valid gmail details of course) in my config/environment.rb, config/environments/development.rb and currently have it in its own initialiser config/initialisers/setup_mail.rb

I have also tried with a few different smtp servers including Gmail and Sendgrid, adjusting the smtp settings accordingly but still nothing. I can see the mail in the terminal and the development log and that's it.

Does anyone know of any other gotcha's that I may have missed that need to be setup for ActionMailer to work? Failing that is there a way of getting more information about why the mails aren't being sent? I have

config.action_mailer.raise_delivery_errors = true

set in my config/development.rb but the development log still just shows the same as I see in the terminal.

For what it's worth, I am developing on a Ubuntu 10.04 laptop just in case there's any specific setup needed for that.

Many thanks

Durazzo answered 30/11, 2010 at 11:23 Comment(1)
Can you try removing :domain from the settings or changing it to some other domain.Rizzi
D
54

Well I have resolved the issue, but quite why this works and the other methods did not, I don't know.

The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following

if Rails.env != 'test'
  email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
  ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end

I then added config/email.yml containing the details of the dev and production email accounts

development:
  :address: smtp.gmail.com
  :port: 587
  :authentication: plain
  :user_name: xxx
  :password: yyy
  :enable_starttls_auto: true
production:
  :address: smtp.gmail.com
  :port: 587
  :authentication: plain
  :user_name: xxx
  :password: yyy
  :enable_starttls_auto: true

Like I say, no idea why, but this seemed to do the trick. Thanks all for the pointers

Durazzo answered 7/12, 2010 at 14:39 Comment(2)
This worked for and nothing else did. Quite confused as to why it works but thanks. Hours of googling has come to an end. I should have come here to begin with.Epicure
I looked around for a good solution to this for a while and this is exactly what I should have been doing in the first place. Thanks!Pendleton
R
27

I have the following in config/environments/development.rb

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

The actual mail-configuration, config.actionmailer.* i have placed in config\application.rb.

Hope this helps :)

Rubricate answered 30/11, 2010 at 12:32 Comment(4)
Thanks guys, unfortunately they were both already there. Thanks for the suggestions thoDurazzo
In that case you should be getting errors why it is not working. Correct?Rubricate
Hi nathanvda, sorry I only just saw your comment. Part of the issue was that I wasn't getting any errors. I have posted below as to how I fixed the issue, tho to be quite honest I don't see any reason as to why the method below should work and none of the others did.Durazzo
perform_deliveries did it - you also need to restart the server.Urbano
C
6

Try using 'sendmail' instead of 'smtp'.

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}
Corbel answered 30/11, 2010 at 12:11 Comment(1)
Thanks for the suggestion. Unfortunately I have just tried it and I get exactly the same behaviour with sendmailDurazzo
T
3

Three things.

First, the port is an integer and does not need quotes, as in your first example. (But I think a string should still work.)

Second, don't forget to restart your server each time you modify this (or any) initializer file. This could explain why you didn't see an error after adding:

config.action_mailer.raise_delivery_errors = true

Without having that error message, it's hard to determine why the mail wasn't going but now is. One possiblity is your use of double quotes around the password. If you were using a strong password and had a token in your password that wasn't escaped it could have been reinterpreted. (i.e. "P@ssw\0rd" would become P@ssrd). For just this reason, I always use single quotes in my code unless I specifically need the syntactic sugar.

Lastly, enable_starttls_auto: true is the default and unnecessary.

Trombidiasis answered 30/5, 2011 at 0:22 Comment(0)
D
2

ActionMailer::Base.delivery_method = :sendmail
and
config.action_mailer.perform_deliveries = true

were the two necessary steps that got me over this issue

Dewar answered 8/8, 2015 at 15:55 Comment(0)
B
1

Just put all config to: config/environments/development.rb

I mean

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

and

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

It worked for me.

Bookstand answered 5/2, 2014 at 10:21 Comment(0)
C
0

In addition to, your gmail username does not alias.

Ref: https://support.google.com/mail/answer/12096?hl=en

Chromatics answered 2/9, 2013 at 5:44 Comment(0)
A
0

My two pennies worth:

I had those exact same symptoms with Rails 5.1: Nothing happened, the settings in my development.rb file were utterly ignored...

Then I remembered to restart the machine! (which solved magically the issue)

This had been pointed out by a couple of previous comments.

The issue is tricky however because you do not expect this behavior. In my view, the default comments in development.rb are, in this respect, misleading:

# 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 web server when you make code changes*.
Archidiaconal answered 30/1, 2018 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.