the mail configuration is through smtp.
everything is working ok with localhost in dev mode, devise sends recover password email using smtp conf in development.rb
manual mail sending with action mail is also ok
when in production mode,
only the host is modified to match the host in production machine
the smtp conf is unchanged, and manually sending mail is ok in a rails console
BUT devise does not send recover password mail
how to debug that ?
is Devise really taking on the global mail conf in production.rb ?
there is no Devise::Mailer override.
and this is uncommented in initializer
please share the github page of your project, so we can have a look. Then also share the log of your production server. you can recover it with heroku logs. I did configure this for my apps, eventually it broke down. It is a little bit tricky. I did follow some guide. If you did the same quote the guide you followed. I followed this guide, I remember that I had to allow in my gmail account setting the usage of gmail account from my app. Then there is some settings that you need to do in production.rb, I am quoting the guide, go to that link to see the full guide and check also other guides online:
Setting Up Production Environment
Now you will need to edit the file “config/environments/production.rb”. We will be adding very similar things to this file. First you can add:
config.action_mailer.default_url_options = { :host => 'yoursite.herokuapp.com' }
NOTE: You may also need to add this line. When I was setting up the mailer, I did not find this line in any other tutorials. I had the development mailer working, however I could not get heroku to work. I received this error in the heroku logs:
ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
I found the answer here: ActionView::Template::Error: Missing host to link to on stackoverflow. If you come across this, then add:
Rails.application.routes.default_url_options[:host] = 'yoursite.herokuapp.com'
to your production.rb file.
Next you will have to add in these lines. Once again make sure that you leave the variables as they are.
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.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: ENV["GMAIL_DOMAIN"],
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
read the comments from this post and all the other relevant guides to configure this, you may need to use some smtp api to send the email....
Related
I have created Rails 6 application with ruby 2.6.5. Using SMTP for sending emails.
Added configurations in config/environment/*rb file.
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
user_name: ENV['SENDMAIL_USERNAME'],
password: ENV['SENDMAIL_PASSWORD'],
domain: ENV['MAIL_HOST'],
address: 'smtp.gmail.com',
port: '587',
authentication: :plain,
enable_starttls_auto: true
}
Getting Error: Net::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS
After Verifiying captcha from the below link
[https://accounts.google.com/b/1/DisplayUnlockCaptcha][1]
It work for some time than again it start throwing the same error.
I have enabled less secure apps also for my email.
It is common issue i used to get when using action mailer with google SMTP configuration. I used to fellow below few step to solve it.
Try to login to google account manually with same location where rails server is there and check if captcha is showing.
Once Mail working from rails sever never login manually to goole account again.
Try changing authentication: :plain to :login
I am trying to generate a basic email using ActionMailer. I know that things are setup semi-correctly, as I received an email from Gmail saying an unsecure app tried to login to my account. I have done as others said in this post. Those steps are:
1) Enabled less-secure apps setting in Gmail settings.
2) Changed domain: to "gmail.com" from "mydomainname.com"
3) Changed authentication from "plain" to :login
4) In Gmail settings, enabled POP and IMAP.
Here is my development configuration setting for mail:
...config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
#change mail delivery to smtp for Gmail addresses.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
I've even set the environment variables like a good boy. Error persists even if I hard code the values.
And the exception I get is:
Net::SMTPAuthenticationError
534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtj
Also, in development you can use local mail-catchers.
One of them is MailHog, it's similar to gmail.
Installation
brew update && brew install mailhog
development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
Run MailHog in terminal typing: MailHog and in a browser http://localhost:8025
Not really a solution to Gmail's configuration problem, but another option. I had the same problem and solved it. And the problem eventually appeared again. I'm not sure if I changed something or if it´s just Gmail who doesn't like less secure applications and blocks emails even if you configure everything correctly.
I read other people solved the problem (https://stackoverflow.com/a/48300220/3372172) using two steps authentication, but never tried it.
I finally changed to another ESP (as even Rails seems to recommend in their guides): http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail
Note: As of July 15, 2014, Google increased its security measures and now blocks attempts from apps it deems less secure. You can change your gmail settings here to allow the attempts. If your Gmail account has 2-factor authentication enabled, then you will need to set an app password and use that instead of your regular password. Alternatively, you can use another ESP to send email by replacing 'smtp.gmail.com' above with the address of your provider.
I am using Sengrid because their free plan (100 daily emails) is enough for me right now and I get many interesting analytics. But if you need more volume, I don't think they are cheap.
What fixed it is playing around with the settings until I finally got:
config.action_mailer.raise_delivery_errors = true
#change mail delivery to smtp for Gmail addresses.
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "mydomainname.com",
authentication: "login",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
But the real kicker was to make sure and restart the server with each change to the ...config/environments/development.rb file above.
Edit: Well, now I feel like i'm going crazy, because the above settings did not work when I tried to run it again...
I created rails g mailer from this reference site. It is working fine on local there is no error. I also added {config.action_mailer.delivery_method = :smtp} with did smtp settings and deployed on heroku,
but now its giving error
We're sorry, but something went wrong. Please help.
For sending mail in production environment, do following thing -
1. In config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mail.com',
port: 587,
domain: 'YOUR_DOMAIN_GOES_HERE',
user_name: 'YOUR_EMAIL_ID_GOES_HERE',
password: 'YOUR_PASSWORD_GOES_HERE',
authentication: 'plain',
enable_starttls_auto: true }
and deploy this change on heroku and if you again found any error there, please check -
heroku stack:set cedar-14
then again deploy on heroku - git push heroku master.
Try it.
The Heroku platform itself doesn’t provide an email service. You need to use external services. Read their documentation about how to send emails from your app.
Why is this mailer not sending any mail? (Or any ideas for debugging?)
In my_app/config/environments/development.rb I have this code:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'my_app.com',
user_name: ENV['GMAIL_USERNAME'],
password: ENV['GMAIL_PASSWORD'],
authentication: 'plain',
enable_starttls_auto: true }
Then on my local computer in ~/.bash_profile I have this code:
export GMAIL_USERNAME='blah#my_app.com'
export GMAIL_PASSWORD='***'
When I run $ env in my terminal, I see that both environment variables are correctly set.
I have also restarted my rails server.
You should add
config.action_mailer.perform_deliveries = true
as by default this is on false, preventing mails to be sent from your development environment...
For anyone not using smtp, switching the delivery method to sendmail helped me in addition to explicitly setting deliveries to be performed:
config.action_mailer.delivery_method = :sendmail
If you're having issues sending email from console, you have to call the deliver method on your mail.
MyMailer.create_email.deliver
All of these answers are great, but there is another place where you can get burned, especially in the context of debugging.
In development.rb make sure you set config.action_mailer.raise_delivery_errors = true
If your .deliver method seems to be working without issue, but you never actually receive the email across the wire, your delivery method may be throwing an exception and rails is swallowing the error. This is very true if you simply have something as simple as a misconfigured credentials, or an aws access denied API error. Save ripping your hair out and make sure you have raise_delivery_errors turned on. It wants to tell you something but can't.
So I've figured it out. Having the line ActionMailer::Base.delivery_method = :smtp in config/environment.rb overrides ActionMailer::Base.delivery_method = :test in config/environments/test.rb.
So, delete that line, ActionMailer::Base.delivery_method = :smtp from config/environment.rb and place it in config/environments/production.rb. That allows you to place ActionMailer::Base.delivery_method = :test in config/environments/test.rb and the version you want in config/environments/development.rb. I made development.rb :test as I populated my database using Faker and changed it to :smtp so I was sure that real emails were sent as an additional check.
Note: You must restart your server for these changes to take effect.
Another note: Heroku's current SendGrid Instructions (https://devcenter.heroku.com/articles/sendgrid) put the SendGrid Heroku configuration code in a new config/initializers/mail.rb file which will likely require removing its last line and placing the desired version in each config/environments/[production.rb, development.rb, test.rb]
When attempting to send email from within my Rails 3 app in development mode on my local machine, I received the following error:
Errno::ECONNREFUSED in RemindersController#create
No connection could be made because the target machine actively
refused it. - connect(2)
After spending several hours sifting through similar questions on SO and blog posts with none of the proposed solutions working, here is the solution I finally arrived at:
tl;dr
Change your smtp port setting to 25, instead of 587 which is in all the tutorials & docs.
Detailed answer (for humans):
Step 1. Go watch railscast #206 then come back. I'll wait.
Step 2. Get extra aggravated at how easy he makes it seem even though you're getting ridiculous errors.
Step 3. Go to app/config/environments/development.rb and change line 17 from false to true. This will show you where things are getting messed up.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true
Step 4. Still in app/config/environments/development.rb add the following code before the last end statement. (You no longer need the app/config/initializers/setup_mail.rb file created in the screencast when you are in development mode and can safely delete it.)
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 25,
:user_name => "<your username>#gmail.com",
:password => "<your password>",
:authentication => "plain",
:enable_starttls_auto => true
}
Notes:
Notice we did not specify the :domain setting like in the tutorials
and it works fine.
Notice also that we used your_username #gmail.com as the
:user_name setting.
If you use 2-step authentication for your gmail account you will have
to generate a special app-specific
password. disclaimer: I did not try this.
AND THE BIG FAT ELEPHANT IN THE ROOM, is that we used :port => 25
NOT 587 despite the fact that nearly every single tutorial, SO question, and the rails guide only suggest 587. Granted, this could
possibly be because my work blocks port 587? I have no idea. But
this was my key problem no one solved for me. The way to find out if
this is your problem is to use telnet. If you are on windows you
have to first enable telnet by following these
instructions.
Then go to your command prompt and type telnet smtp.gmail.com 587
if it works you will get a response like 220 mx.google.com ESMTP
pi6sm33274849wic.3 - gsmtp. Otherwise it will tell you it could not
connect, in which case you can try it on a different port eg. telnet
smtp.gmail.com 25.
I hope this helps some lost soul.
I encountered this issue whilst reading through Agile Web Development with Rails 4 chapter 13, and it turned out that any email config stuff added to config/environment.rb was being ignored, despite the book indicating that "shared" email settings for all environments could be added there.
Moving the settings into config/environments/development.rb (and restarting the Rails server) fixed the issue, allowing emails to be sent on either port 25 or port 587 without any problems:
# development.rb
Rails.application.configure do
# ...blah blah blah...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
user_name: 'yourusername#gmail.com',
password: 'yourpassword',
authentication: 'plain',
enable_starttls_auto: true
}
end
NOTE: development.rb didn't contain any email-related settings originally, so I don't think that the values in environment.rb were being superseded - it's more like they simply had no effect. The I18n.default_locale = 'en-GB' line in environment.rb was working, though, so it's not like the file was being completely disregarded...