Rails Mailer is not actually sending emails - ruby-on-rails

I have just started learning ruby on rails and this was one of the tutorials i followed:
http://railscasts.com/episodes/206-action-mailer-in-rails-3
I followed it to a T and I have yet to receive anything in my gmail account. Is it fair to assume some updates to ruby have changed how you do things?
My best guess from reading everything over the net, the tutorial is missing actually setting the mailer to use smtp setting.
Here are my smtp settings:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "alexei.herokuapp.com",
:user_name => "mygmailaccount",
:password => "mygmailpassword",
:authentication => "plain"
:enable_starttls_auto => true
}
Could it be that i am still in local environment when testing this?

In that Railscast, Ryan has a line in there that doesn't send any email in development:
Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
Did you put that in? If so, then no email will go out when you run your app locally in development.
Try removing it.
Also open myapp/logs/development.log to see if what shows up there when you try to send an email.

Related

How do I send emails using Sendgrids API to send emails with rails 4? I can seem to put all the pieces together

I want to send emails with sendgrids API from a google cloud platform instance. They have a short tutorial on how to do this but it has the email message, who its from, who to send to, and other info all in app.rb which isnt the normal way messages are sent in rails apps.
I looked at the sendgrid ruby docs and they also dont have very good information. All the info in in one place and they dont state what files to put them in or even mention any smtp settings to use.
Here is what I have so far
development.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 465,
:domain => "mydomain.com",
:user_name => ENV['sendgrid_username'],
:password => ENV['sendgrid_password'],
:authentication => :plain,
:ssl => true,
:enable_starttls_auto => true
}
gemfile
gem "sendgrid-ruby"
The email views are in app/views, the mailer methods are in app/mailer the same as normal rails 4 apps have it setup.
So I guess here are my main questions:
Where do I call the environment variables holding the sendgrid api key?
Where do I tell Action Mailer to use sendgrid or SendGrid::Mail as I've seen in a couple of places?
Do I even need the sendgrid gem?
Are the smtp settings correct for sending over SSL with sendgrid?
I'm new to sending emails like this in a rails app and would really appreciate the help.
Put apikey as the name and the actual apikey as password:
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "yourwebsite.com",
authentication: :plain,
user_name: 'apikey',
password: Rails.application.secrets.sendgrid_api_key
}
Source
Use Figaro gem: https://github.com/laserlemon/figaro
It will generate an application.yml file for you, where you can store your sendgrid_username and sendgrid_password.
Have you generated your mailer? For example, generate mailer user_notifier where you can define your a default-from email, and some methods like this:
r
# send a signup email to the user, pass in the user object that contains the
user email address
default :from => 'hello#yourdomain.com'
def send_signup_email(user)
#user = user
mail( :to => #user.email,
:subject => 'Thanks for signing up!'
)
end
No, you don't need it.
https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html

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.

Heroku Action Mailer with Gmail, sends email only once = Net::SMTPAuthenticationError

Strange behaviour I have. I'm using gmail to send my emails in my Rails app. I have my gmail configured to accept less secure apps.
However I send one email and then when I go to my google account it prompts me to restore the account via my phone.
After that, my app no longer sends email and I get this in my Heroku logs:
Net::SMTPAuthenticationError (534-5.7.9 Please log in with your web browser and then try again.
My production.rb is set up this way:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => 'xxx#gmail.com',
:password => 'xxxxxx',
:authentication => "plain",
:enable_starttls_auto => true,
}
Anybody any ideas?
I think you have to set the domain to gmail.com
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "xxx#gmail.com",
:password => "xxxxxxx",
:authentication => :plain,
:enable_starttls_auto => true
}
If it worked then ok, if not try to change the :authentication to :login.
Advice: Check Gmail's security events too and double-check that all security settings are intact.
So I had several problems. The one that was caused by my inattention was that I didn't read Figaro gem docs properly and I wasn't sending out the proper info to Heroku with figaro heroku:set -e production.
Secondly my account would go into lockdown after I created it and sent first email. Steps to do:
enable two-step verifiation
generate app specific keys
put them in your config/application.yml if you're using Figaro
push and run figaro command
you might have to clear CAPTCHA on gmail see here
Follow the coment by #Mohamed
Advice: Check Gmail's security events too and double-check that all security settings are intact.
LINK: https://www.google.com/settings/security/lesssecureapps

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.

how to set up restful_authentication email activation using gmail SMTP?

I have installed restful_authentcation from technoweenie with activation, and so I see the generated UserMailer < ActionMailer::Base.
However, the instructions don't include how to set it up to work with Google SMTP.
I am guessing that environments/development.rb needs to have the SMTP settings, but still not sure given Google (via Google apps) are all TLS.
Anyone set up activation using restful_authentication?
I currently put into environments.rb the following:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "mydomain.com",
:authentication => :plain,
:user_name => "xxx#mydomain.com",
:password => "mypassword"
}
Thanks!!
As far as I know, ActionMailer doesn't do TLS out of the box (2.3.2). A couple of months ago I had the same issue and found some code on a Japanese page and integrated that. it appears that code has been wrapped up into a plugin now (with english docs yeah!). That's not exactly what I'm using, but it advertises the same effect.
so add this plugin:
http://github.com/openrain/action_mailer_tls/tree/master
and in environments/development.rb or environements.rb you need something like this:
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "yourdomain.com",
:user_name => "first.last#gmail.com",
:password => "passwd",
:authentication => :plain
}
I see that :enable_starttls_auto => true is now in the docs, but it wasn't when I started. this at least works for me...
Edit: for some reason that link doesn't work if you follow it, but copy paste in the address bar and it's live...
I've never used SMTP from ruby (I have from python), but that looks right. You have the right domain and port (actually, multiple ports are supported, but that's one of them), and you're using starttls and AUTH PLAIN, which Google does use.

Resources