I am developing a Rails app on Cloud9, I am trying to do something pretty simple, which is to get the Devise confirmation emails to work. For some reason, they are not being sent.
I'm using SendGrid for email functionality. I know that SendGrid is working correctly, because I can make a trivial ActionMailer that sends a test email. But for some reason, the Devise confirmation emails are not being sent. I can see that it is making a call on ActionMailer::Base.mail method, but the mail is never delivered.
I have no idea how to debug this.
Just to prevent suggestions to check stuff that is already working:
The user options include :confirmable
I am setting config.action_mailer.perform_deliveries = true
I have the following lines in config/initializers/setup_mail.rb
Code:
if Rails.env.development? || Rails.env.production?
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '2525',
authentication: :plain,
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
domain: "sendgrid.com",
enable_starttls_auto: true
}
I'm guessing that my solution is not the intended solution, but it works (although I'm not sure why).
In the file config/initializers/setup_mail.rb, I added the following two lines:
ActionMailer::Base.layout 'mailer'
ActionMailer::Base.default from: "from#example.com"
These are probably things that should be specified in some configuration file for Devise, but I didn't see any instructions for doing so.
[Edit] It is only line 2 that is important. It doesn't matter that the "from" address is a bogus email address, but if it is missing, the email does not get delivered.
Related
I am working on a basic app called PhotoApp, where I am learning how to set up SMTP with rails. I have used basic Ruby with SMTP sometime ago, and I know my email works, just the emails sent gets to spam folder.
But when I am trying to configure SMTP on Rails, I am getting ArgumentError (SMTP-AUTH requested but missing user name):, and that's the primary reason why the app, when pushed to heroku, crashed every time a user signs up.
Here's my config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost:8080' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.net',
authentication: 'plain',
enable_starttls_auto: true,
user_name: 'gmail_email',
password: '...'
}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
I am running the server on localhost:8080, in development mode, so my host is set to localhost:8080.
Apart from that I am not able to get my app working with my gmail username with SMTP in rails.
Edit, this ruby code works:
#!/usr/bin/ruby -w
require 'net/smtp'
message = <<~EOF
From: mygmail#gmail.com
To: someone#protonmail.com
Subject: Hello!
Hello!
#{"HELLO ".*(50).delete_suffix(?\s)}
EOF
Net::SMTP.new('smtp.gmail.com', 587).tap(&:enable_starttls_auto).start('gmail.com', 'mygmail#gmail.com', 'mygmailpass', :plain) do |smtp|
smtp.send_message message, 'mygmail#gmail.com', 'someone#protonmail.com'
end
But as a newbie to rails, I can't get actionmailer working when the same email is used in the config file...
Rails 4.2.1
Devise 3.4.1
I inherited an application, that, up until about a year ago, was sending notifications, without a problem. Then it was down for about a year. I am trying to restart it, and was testing a password reset action, but I am not receiving the emails.
ActionMailer (production environment), is set up as follows:
config.action_mailer.smtp_settings = {
address: Rails.application.secrets.email_provider_smtp_server,
port: Rails.application.secrets.email_provider_smtp_port,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_password
}
config.action_mailer.default_url_options = { :host => Rails.application.secrets.domain_name }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
I tested the gmail account, and password, and it's all working fine. This is an individual freebie Gmail account, not a paid account.
I noticed that the "Less secure app access" was switched off, so I switched it off, and tried again, but I was not receiving the notification email, even though production log said it was sending email:
Sent mail to xxx#xxxxxxxx.net (539.2ms)
So, I added the following:
config.action_mailer.raise_delivery_errors = true
And when I ran it again, I saw the "Something went wrong" dialog, and now in production log, I have the following:
F, [2020-01-29T18:42:39.172134 #16986] FATAL -- :
Net::SMTPAuthenticationError (534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs
):
app/models/user.rb:38:in `send_reset_password_instructions'
Any ideas?
So I want devise to utilize amazon ses to send the "I forgot my password" emails to a user. I have followed all of the steps for setting up devise with Gmail and then tried to configure SES as the mailer in production.rb I am new with ruby and I am sure there is something I have done incorrectly with either my syntax or configuration.
Production.rb
config.action_mailer.delivery_method = :smtp,
config.action_mailer.smtp_settings = {
:address => "email-smtp.us-west-2.amazonaws.com",
:user_name => ENV['AWS_SMTP_USER'], # Your SMTP user here.
:password => ENV['AWS_SMTP_PASSWORD'], # Your SMTP password here.
:authentication => :login,
:enable_starttls_auto => true
}
I stored both AWS_SMTP_USER & AWS_SMTP_PASSWORD to the config using "$ Heroku Config"
Devise.rb
config.mailer_sender = 'Admin#synapticecho.com'
I don't think I am missing anything else, but I haven't found anyone trying to use Devise & SES as the mailer. I'm just not sure what other pieces I might be missing and I didn't find the AWS documentation all that helpful for configuring SES to work with Devise.
I can post the github project if it helps.
In my case.
I got the following error.
Net::SMTPFatalError (554 Message rejected: Email address is not verified
but, just like you. I didn't know missing anything else.
try below setting.
config.action_mailer.default_url_options = { host: Rails.application.secrets.HOST }
config.after_initialize do
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: Rails.application.secrets.ACTION_MAILER_ADDRESS,
port: Rails.application.secrets.ACTION_MAILER_PORT,
domain: Rails.application.secrets.ACTION_MAILER_DOMAIN,
user_name: Rails.application.secrets.ACTION_MAILER_USER_NAME,
password: Rails.application.secrets.ACTION_MAILER_PASSWORD,
authentication: :login,
ssl: true,
tls: true,
enable_starttls_auto: true
}
end
It works, my env.
That point is config.after_initialize.
but, I don't know why need it.
Rails 4.1.4
Devise 3.2.4
AWS SES
In AWS EC2
Verified email's domain is 'gmail.com' ( I'll change it)
I fix the problem by adding the following line in production.rb.
ActionMailer::Base.default :from => 'my.verified.email.address'
I've configured email on redmine according to the instructions in the redmine wiki, and it all works fine. Here are the contents of my config/configuration.yml file:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: my_email#gmail.com
password: my_password
However I am trying to use environment variables in place of my_email#gmail.com and my_password like so:
production:
delivery_method: :smtp
smtp_settings:
address: "smtp.sendgrid.net"
port: 587
authentication: :plain
domain: "heroku.com"
user_name: <%= ENV['SENDGRID_USERNAME'] %>
password: <%= ENV['SENDGRID_PASSWORD'] %>
When I try to send a test email, with the environment variables in the config file, redmine give this error:
'An error occurred while sending mail (535 Authentication failed: Bad username / password )'.
So I guess the erb snippet is not being evaluated (I have double checked the values of the environment variables).
I've searched for a solution and come up empty, does anyone have any suggestions as to how I should configure email in redmine so that I don't expose my sendgrid credentials in the config file?
Alternatively if someone can tell me that it's not a security risk to use the credentials directly, without environment variables, that would also solve my problem.
I had answered this question 2 weeks ago, and it was deleted by someone else right away.
So I'm not sure if someone will delete this answer again to prevent other guys knowing how to solve this problem. Good luck!
I got the same issue and this article
Set up mailing in redmine on heroku solved my problem.
The idea is moving the settings from config/configuration.yml to config/environments/production.rb and using Ruby code to set it up. Since ENV['key'] is handled by erb, I guess configuration.yml is not handled that way. Maybe there is some security issue?
So in your case, you should add these codes to config/environments/production.rb as follows,
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
And remove codes from config/configuration.yml and make it looks like this,
default:
# Outgoing emails configuration (see examples above)
email_delivery:
delivery_method: :smtp
I need to use a mailer for sending out emails to users to set their passwords to the "recoverable" function of Devise and active admin. On the development environment I have done this by adding the following to these files:
config/environments/development
#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#These settings are for the sending out email for active admin and consequently the devise mailer
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings =
{
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com', #you can also use google.com
:authentication => :plain,
:user_name => 'XXXXX#gmail.com',
:password => 'XXXXXXX'
}
How do I get the same functionality for the production environment? I want to deploy my app to Heroku. What files and code would I need to add?
All configurations you have set in Development mode will work EXCEPT you will need to reconfigure the default mailer url.
So.
Copy-paste your settings from development.rb.
Point your default mailer to your heroku app:
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
Also, be careful of any email limits your smtp may have when moving to production. It's hard to trigger gmail's smtp limits while developing, for example, but they could be more easily triggered in production.
If it works in development mode, then it will work in production mode.
Supposing everything is setup correctly, resetting a password in development will already send an actual email using your gmail account.
Devise only relies on the mailer config setup correctly (which you have done), and configuring devise to allow password reset, and possibly another setting for the From field of the email.
This should work fine!
As long as config/environments/production.rb has the same thing with an exception. The default_url_options should have a :host value of 'localhost' only in development and 'YOURAPPNAME.herokuapp.com' in heroku production.
i.e.
config.action_mailer.default_url_options = { :host => 'YOURAPPNAME.herokuapp.com' }
Remember to unlock captcha on gmail, otherwise it won't send email from heroku (unknown source). You can do that by going to this link: http://www.google.com/accounts/DisplayUnlockCaptcha
Just as a suggestion, I'd say move this from environments.rb
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
and place is in environments/development.rb as
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
It's not needed in production.
See Net::SMTPAuthenticationError when sending email from Rails app (on staging environment) for more information in regards to gmail seeing heroku as an unknown host.