Cannot send email verification link through development and production environment - ruby-on-rails

sendgrid is not sending email verification link in development and production environment.
My environment.rb file looks like this
ActionMailer::Base.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port => '578',
:authentication => :plain,
:enable_starttls_auto => true
}
I added these two line in development.rb file look like this
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options ={:host => 'http://localhost:3000'}
In production.rb file I added these two lins
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options ={:host => 'warm-forest-79168.herokuapp.com',
:protocol => 'https'}
In my .profile file I exported my sendgrid username and password
export SENDGRID_USERNAME=XXXXXXX
export SNDGRID_PASSWORD=XXXXXXXX
The rest to add confirmable in devise user I followed wiki https://github.com/heartcombo/devise/wiki/How-To:-Add-:confirmable-to-Users
When I signup locally the mail is being sent but never recieved
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome deepakchauhan3294#gmail.com!</p>
<p>You can confirm your account email through the link below:</p>
<p><a href="http://localhost:3000/users/confirmation?
confirmation_token=GKBtUsWUJ1N3HQow9d94">Confirm my account</a></p>
When I signup in production(heroku) I get the following error
Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password
I have rechecked my username and password both are right still It is throwing error. Please help me I have been stuck in it for too long.

Related

Send email from rails app in heroku environment

I have deployed my rails web app to heroku. Now I want send email to the user who signed up to my web app. I want to send email from my business email. Let say my business email is abc#business-email.in.
I am getting error when I checked by doing heroku logs.
86ms: SocketError (getaddrinfo: Name or service not known):
Here is my mails settings in config/production.rb
#Heroku mail configuration
config.action_mailer.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.business-email.in",
:domain => 'myApp.herokuapp.com',
:port => 25,
:user_name => "abc#business-email.in",
:password => ENV['SMTP_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}

why is devise not sending email via gmail smtp?

I am using devise for authentication. It provides a forgot password link. When i send email then the email is not sent. Following is the settings i have used. Can you tell me why gmail is not sending the email? I have also turned on "allow less secure app to send email" and i have also enabled imap in gmail setting.
application.rb has the following setting.
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:domain => 'mail.google.com',
:port => 587,
:user_name => 'validemail#gmail.com',
:password => 'validpassword',
:authentication => 'login',
:enable_starttls_auto => true
}
development.rb has
config.action_mailer.default_url_options = { host: '127.0.0.1'}
config.action_mailer.delivery_method = :smtp
After sending the email i get the following text in the console.
Devise::Mailer#reset_password_instructions: processed outbound mail in 215.2ms
Sent mail to validemail#gmail.com (1097.6ms)
Date: Thu, 29 Dec 2016 09:50:41 +0000
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: validemail#gmail.com
Message-ID: <5864dc7161acb_173921a07788707d#kofhearts-rubyonrails-3267120.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello validemail#gmail.com!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p>Change my password</p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Redirected to https://rubyonrails-kofhearts.c9users.io/users/sign_in
Completed 302 Found in 1965ms (ActiveRecord: 14.7ms)
UPDATE:
I am just following this tutorial.
https://www.youtube.com/watch?v=ZEk0Jp2dThc
Send email is not working with the settings that are specified in this video.
Enabling less secure apps in my account settings worked for me.
Google Account settings > Signing in to Google > scroll down to the bottom
Turn on allow less secure apps. Try sending the email again.
That's should work.
Maybe you could also turn on config.action_mailer.raise_delivery_errors = true
in config/environments/development.rb.
It's easier to debug.
Maybe you forget to set config.action_mailer.perform_deliveries = true in config/environments/development.rb?
Have you tried this?
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "myinfo#gmail.com",
:password => "secret",
:authentication => "plain"
# :enable_starttls_auto => true # I don't have this, but it should work anyway
}
it it's sent maybe you don't receive it because of the spam filter, first thing to check:
class UserMailer < ActionMailer::Base
default :from => "myinfo#gmail.com"
# ...
end
You also check your Forwarding and POP/IMAP in your gmail..
include these line of codes on your development.rb
config.action_mailer.default_url_options = {host: 'your server' } # ex. localhost:3000
config.action_mailer.raise_delivery_errors = true # to raise error if smtp has error on setup
config.action_mailer.default :charset => "utf-8"
If you are working with Gmail you should activate permission to
externals apps send email through SMTP.
In development mode Rails does not send emails, you should run Rails in production rails s -e production or turn on config.action_mailer.raise_delivery_errors = true
do not forget to use mail().deliver or for Rails 5+ mail().delivery_now
look you this is my SMTP configuration
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => ENV['mailer_address'],
:port => ENV['mailer_port'],
:user_name => ENV['mailer_username'],
:password => ENV['mailer_password'],
:authentication => "plain",
:enable_starttls_auto => true
}
this is my Class Mailer
def send_email
mail(to: "example#email.com", subject: 'not reply this message').deliver
end
I call that function from my controller. If you got any error you should print it to check what is the problem.
begin
myMailer.send_email
rescue Exception => e
flash[:error] = "Imposible sent email, #{e.inspect}"
end

Rails 4 mailers with gmail smtp

I want to use the Mailer of Rails 4 with the Gmail's smtp configuration
In my development file i set:
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "mymail#gmail.com",
:password => "mygmailpassword",
:authentication => :plain,
:enable_starttls_auto => true
}
but i don't recive any mails.
This is the output of the terminal:
To: antonioni.giovanni9#gmail.com
Message-ID: <55ffb56da9e91_aa929362303435c#pc-rails.mail>
Subject: Conferma ordine
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_55ffb56da807b_aa92936230342e1";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_55ffb56da807b_aa92936230342e1
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
so my application try to send an email but this not appear on my gmail dashboard.
Any idea for resolve this trouble?
Gmail smtp service need an APP PASSWORD for send a mail from a gmail account (for more info visit this link). After generating the password you must change the configuration of the development file:
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "mymail#gmail.com",
:password => "GENERATEDPASSWORD",
:authentication => :plain,
:enable_starttls_auto => true
}
And then restart the server.
For catching the error i've set: config.action_mailer.raise_delivery_errors = true (Thanks Dipak for the tip)
You also need to change this: https://www.google.com/settings/security/lesssecureapps

devise forgot password email not received

I am facing issue with devise forgot password.I am not receiving any email though devise displays messsage that "you will receive an email with instructions about how to reset your password in a few minutes".
Ruby-1.9.3 Rails 3.2 devise 2.2.4
My environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:authentication => :plain,
:domain => 'xyz#gmail.com',
:user_name => 'xyz#gmail.com',
:password => 'abcde',
}
My environment.rb
ActionMailer::Base.delivery_method = :smtp
My intializers/devise.rb
config.mailer_sender = "xyz#gmail.com"
And development.log shows
Sent mail to xyz#gmail.com (3205ms)
Date: Wed, 26 Jun 2013 23:33:01 +1000
From: xyz#gmail.com
Reply-To: x#gyzmail.com
To: xyz#gmail.com
Message-ID: <51caed8dd99a5_15365823b9268927#nbnco-U01.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello xyz#gmail.com!</p>
<p>Someone has requested a link to change your password. You can do this the link below.</p>
<p>Change my password</p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 3423ms (ActiveRecord: 0.0ms)
As have been mentioned above in development mode mails aren't being send for real, if you're curious how that's obtained (and maybe want to override that behavior) look into the following file
config/environments/development.rb:
#don't send emails in development
config.action_mailer.perform_deliveries = false
Of course it would be better to use some mailtrap but if you want to just quickly see mail in your mailbox just change that false to true and here it goes if you've got properly configured mailer of course.
Edit:
That's my config for gmail and it works, there is no domain param so probably it's making trouble:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => '<login>',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
Edit2: And don't forget to restart your server ;)
In development with your configuration emails are not sent but are just visible in the logs. You can try and use a service such as:
http://mailtrap.io/pages/ruby_landing
https://github.com/ryanb/letter_opener
https://github.com/37signals/mail_view

Sending an email from Rails app- works in development, not in production on Heroku

I am trying to use the gem Active Admin to send emails to users that I sign up so that they can create a password.
This entails a process of inserting the following code on the config/environments/development.rb
#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 => 'XXX#gmail.com',
:password => 'XXXX'
}
THis works no problems
For deploying to the production site on Heroku. I inserted the following code into the config/environments/production.rb
#Added per active admin install instructions
config.action_mailer.default_url_options = { :host => 'http://XXXX.herokuapp.com/' }
#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 => 'XXX#gmail.com',
:password => 'XXX'
}
But now the emails donot get sent. Instead I see a "We're sorry soemthing went wrong" message in the browser and the logs say the following lines
2012-08-17T17:39:34+00:00 app[web.1]: cache: [GET /admin/admin_users/new] miss
2012-08-17T17:39:34+00:00 app[web.1]: Started POST "/admin/admin_users" for 96.49.201.234 at 2012-08-17 17:39:34 +0000
2012-08-17T17:39:35+00:00 app[web.1]: Net::SMTPAuthenticationError (535-5.7.1 Please log in with your web browser and then try again. Learn more at
2012-08-17T17:39:35+00:00 app[web.1]: ):
2012-08-17T17:39:35+00:00 app[web.1]: app/models/admin_user.rb:35:in `block in <class:AdminUser>'
2012-08-17T17:39:35+00:00 app[web.1]: cache: [POST /admin/admin_users] invalidate, pass
Where should I go from here? Can someone please give me a hand
Here is a useful help from Gmail: http://support.google.com/mail/bin/answer.py?hl=en&answer=14257&p=client_login.
The problem is Gmail prevents suspicious sign-in attempt that may be robot. We need to grant permission to the app so that it can use Google Account to send email.
Gmail requires SSL connections to their mail servers. Try adding this to your SMTP settings:
:enable_starttls_auto => true
Try following code:
ActionMailer::Base.smtp_settings =
{
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'gmail.com', #you can also use google.com
:authentication => 'plain',
:user_name => 'XXX#gmail.com',
:password => 'XXX',
:enable_starttls_auto => true
}
Changes made:
1. :authentication => :action to :authentication => 'plain'
2. added :enable_starttls_auto => true , as commented by janders223 above.
http://www.google.com/accounts/DisplayUnlockCaptcha click on this and give access to it. The next time it will work

Resources