Sending mail with Rails 3 in development environment - ruby-on-rails

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 => "xxx#gmail.com",
: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

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

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 :)

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 => "xxx#gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}

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.

ActionMailer::Base.delivery_method = :sendmail
and
config.action_mailer.perform_deliveries = true
were the two necessary steps that got me over this issue

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 => "xxx#gmail.com",
: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.

In addition to, your gmail username does not alias.
Ref: https://support.google.com/mail/answer/12096?hl=en

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*.

Related

EOF error in Rails utilizing the devise gem

I've recently set up the devise gem for authentication and all is working well excluding the forgot my password function. My smtp is currently pointed towards a yahoo email address. Yet, I get a EOF end of file extension error. In development it delivers message just fine ,but to no avail in production. Also, I initialized the ENV variables for username and password with Heroku Config:set .Addtionally, I tried port 587. Any help would be much appreciated! I'm fairly new to rails and searched for similar log issues, but most varied significantly in similarity to this issue. Thank you!
config.action_mailer.perform_caching = false
config.action_mailer.default_url_options = {:host => 'https:app.herokuapp.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:user_name => ENV['example#yahoo.com'],
:password => ENV['examplepass'],
:domain => 'https://example.herokuapp.com/',
:address => 'smtp.mail.yahoo.com',
:port =>465,
:authentication => "plain",
:enable_starttls_auto => true,
}
My first think:
I think your file have non visible characters, try to clean your file. https://alvinalexander.com/blog/post/linux-unix/how-remove-non-printable-ascii-characters-file-unix
Erase the file and create a new one an rewrite the config, avoid the copy paste. maybe works if you do a copy paste without format to note bloc, word something like that and then copy that back to your file.
Also read more What is an EOFError in Ruby file I/O?
https://airbrake.io/blog/ruby-exception-handling/eof-error
Continue searching i found this
EOFError in Devise::PasswordsController#create
Devise mailer EOF error
a user comment this
Ah classic. My config/secrets.yml isn't tracked in git (naturally) and >looks like it was overwritten and lost those entries for the email >provider smtp/username/password at some point. Thanks
Another one Rails EOFError (end of file reached) when saving a devise user
The resolution to my issue was two fold. I didn't have the heroku config vars properly set up and initialized.My .gitignore file didn't have the exclusions as well to prevent uploading to repository. In conclusion, I added the figaro gem, bundle exec install figaro , and it created the application.yml needed to store that actual user/pass information. https://railsapps.github.io/rails-environment-variables.html ----> quite useful in explaining the options for setting the vars.
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port =>587,
:authentication => :plain,
:enable_starttls_auto => true,
Also in the above code , I didn't have domain specified to heroku.com but my application domain on Heroku. For production.rb you do need the default url set to your applications name. If you kept the default url "heroku.com" in production.rb it would 404 error when the emailed link was clicked on.
config.action_mailer.default_url_options = {:host => 'https://exampleapp.herokuapp.com/' }
Thanks for the help! I also hope this can be of help to other new rails devs.

ruby on rails tutorial ch 10 could get mail but links are wrong also Bitbucket could not add the code

I've partially solved this problem: I could get links with confirmation and resetting password.
The problem is that those links pointing to the wrong app and I need to adjust name of app manually in order to get the right redirection. Heroku representative said that those wrong address related to code....
1.Where in the Michael Harti ruby on rails tutorial could I find and change the code?
I've used Cloud 9 and Bitbucket as repository. I've created the new app on Bitbucket clone my existing app from Bitbucket to Cloud and want to push it to Bitbucket in order to make some changes.
But Bitbucket doesn't allow me to do saying that there is the existing app. clone probably doesn't work in this case.
2.How to create exactly the same app as existing in Bitbucket but with different name on Cloud9 and push it to Bitbucket with different name?
thanks.
//config/environments/production.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'tatyanaa.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
If you see that output in console, it means your email was actually sent.
By "sent" I mean that Rails in development mode, instead of really sending the email (so you don't have to setup anything), it outputs it to the console like you can see there.
This configuration can be changed in development.rb by setting
config.action_mailer.delivery_method = :stmp
config.action_mailer.perform_deliveries = true
You are then required to properly configure the smtp settings, check the Action Mailer configuration guide for additional details, should be something like:
config.action_mailer.delivery_method = :stmp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
address: "foo.bar.com",
user_name: "someone",
password: "apassword"
}
However, I highly suggest you to keep the default configuration and let it log in console instead of sending real emails.
Did you properly write the code in production.rb ?
Here is mine (I've followed the same tutorial as well and it worked):
(production.rb)
....
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'my_heroku_app_name.herokuapp.com'
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
....
Also did you properly configure sendgrid as an addon on heroku?
(terminal window)
heroku addons:create sendgrid:starter
If you are stuck and REALLY don't know how to proceed, I recommend you to do the tutorial again from the start. It may seem inefficient to go through the same process again when (I assume) you haven't finished the entire tutorial itself.
Believe me, I've also had the same situation where I "thought" I've followed the every step of the tutorial correctly before the app stopped working for some reason and when I restarted it again from the beginning and followed the steps again, this time with a more relaxed mind, the code worked perfectly :) And I've gone through the same tutorial "4 times" before I could finally "get it". I think it would be good for you if you repeat the same process again as it helps solidify your knowledge AND solve the problem.

Why is my Heroku app not sending emails in production with Sendgrid?

My devise emails work fine in development.
But now that I have pushed to Heroku and am using the sendgrid add-on, they don't get sent. I don't get an error. It seems like it is sent just fine, it is just that it never actually reaches my inbox.
This is my config/environment/production.rb file:
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.default_url_options = { :host => 'http://myapp.herokuapp.com' }
config.action_mailer.smtp_settings = {
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"],
:address => 'smtp.sendgrid.net',
:domain => 'myapp.herokuapp.com',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I checked those config vars on Heroku, and they return valid results.
Any ideas?
P.S. I don't have my domain pointing to the Heroku App yet, I just want to test it with the stock Heroku settings first.
Everything look good, did you check if the sendgrid account is provisioned?
If you are on heroku, go to the sendgrid panel to finish the setup, just visit the app profile and under resources(which is the default) you should see the sendgrid add-on, click it and just make sure you have everything setup. In this case it will ask you to setup a template, but you can 'skip' this.
Hope this helps!
This was happening to me too, or something like it. I was using the emails to only send to one email address, for my personal notifications.
It turns out that the email address "bounced" the first time and was being "suppressed" at Sendgrid for future sends. I would check the "suppressions" tab to make sure that's not happening to you.

Email for user confirmation not sending in production. Rails, Heroku, Devise, Gmail

I've looked all over Google and stackoverflow for an answer, but none of them work.
I'm trying to set up smtp emailing for user confirmation after signing up. It works fine in development. Even with MailCatcher on, it bypasses it somehow and sends to the right email from my assigned gmail.
Here's what's in development.rb:
config.action_mailer.default_url_options = { :host => 'localhost:3000'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}
Here's what's in production.rb:
config.action_mailer.default_url_options = {:host => 'myapp.herokuapp.com', :protocol => 'http'} #I've also tried it without ":protocol => 'http'"
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain, # I've also tried :login
:enable_starttls_auto => true, # Also tried tls => true
:user_name => 'myemail#gmail.com',
:password => 'mypassword'
} #I've also tried having the attribute :domain => 'myapp.herokuapp.com',
In config/initializers/devise.rb
config.mailer_sender = 'please-change-me-at-config-initializers-devise#example.com' #It still sends from myemail#gmail.com even with this line uncommented.
I don't know how to paste out my entire 7 pages (in word document) worth of heroku logs in code block, so I've pasted them in a google doc.
google doc of heroku logs (I believe I highlighted the part where it started to go wrong on page 1-2):
https://docs.google.com/document/d/1G-cCX7T1sPL5XtjyjHRaWmzfaBbWkuTR2edsDmDm1Pc/edit?usp=sharing
I'm not sure where it's finding the correct email to send from in development. I'm still a novice at this.
edit: I just found out that users that were registered before adding confirmation are not able to log in as well, so it might be a users problem with heroku. But everything still works fine in development.
Help is greatly appreciated, thanks!
You might miss this config.action_mailer.perform_deliveries = true
Please check if your gmail asked for captcha for sending the mail or not. and to debug the error please make config.action_mailer.raise_delivery_errors = true on your production.rb .Please check the similar question Net::SMTPAuthenticationError when sending email from Rails app (on staging environment)

Has anyone successfully set up their email settings on EngineYard?

I am attempting to add email capabilities to my app (forgotten password, notifications, etc.) and I am using EngineYard for hosting. I have successfully configured email in my test environment but upon uploading to EY it seems to error out in Production. I don't pay for their support and the only resource is a bit vague (or beyond me).
I am curious to know if there is any specific file additions, server set up etc. that is needed when using email on EY. I am using Google apps so I thought it would be as easy as adding the same code block for test in production but doesn't seem to be the case.
Here's my config for Google apps, in .../config/environments/production.rb:
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 587,
:tls => true,
:domain => 'example.com',
:authentication => :plain,
:user_name => "sender#example.com",
:password => 'tr1ckypwd!'
}
Note, for the security minded out there, I actually keep the password in a separate file and have code to patch it into the settings on launch, but I figured that would distract from the meat of the response.
Hope that helps.

Resources