devise forgot password email not received - ruby-on-rails

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

Related

Cannot send email verification link through development and production environment

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.

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

Actionmailer says email sent but it never gets there (Rails 3)

Actionmailer won't send emails in development, even though they say 'sent' in the console.
gemlist:
gem 'send_grid'
development.rb:
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => "localhost:3000" }
setup_mail.rb:
ActionMailer::Base.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => '587',
:domain => "heroku.com",
:user_name => "app123456789#heroku.com",
:password => "asdfasdfasdf",
:authentication => :plain,
:enable_starttls_auto => true
}
app/mailers/user_mailer.rb:
class UserMailer < ActionMailer::Base
include SendGrid
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Registered", :from => "media#myemail.com")
end
end
app/views/user_mailer/registration_confirmation.html.haml:
Confirm your email address please!
= accept_invitation_users_url(email_token: #user.email_token)
console:
Sent mail to alain#myemail.com (656ms)
Date: Tue, 11 Feb 2014 13:35:11 -0500
From: media#myemail.com
To: alain#myemail.com
Message-ID: <52fa6d5f667ea_27ce5564820279f3#Ice-box.mail>
Subject: Registered
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SMTPAPI: {}
Confirm your email address please!
http://localhost:3000/users/accept_invitation?email_token=_JL8DQ2GItxl
Redirected to http://localhost:3000/
Completed 302 Found in 1396ms (ActiveRecord: 192.2ms)
you can try setting:
config.action_mailer.raise_delivery_errors = true
to see if it raises any error on delivery. Otherwise try using other email or check it in you junk folder
Or try this in development mode to check if there is any issues there(it seems fine as you can see the confirmation in the console)
https://github.com/ryanb/letter_opener
Add config.action_mailer.raise_delivery_errors = true to production.rb or development.rb and restart your app to make the changes working
Put this before your smtp settings:
config.action_mailer.raise_delivery_errors = true
Add ".deliver" at the end where you are calling the email method i.e.:
UserMailer.welcome_email(#user).deliver
It will raise exception that will help you to find the failure cause.
If you are using background job to send email, You can put this snippet in exception block and use ExceptionNotification gem to send email with details which will help you to catch and find the reason of failure easily.

How to send and get mail using Mandrill?

I'm using Ruby on Rails, could you teach me how to use Mandrill to send and receive mail.
My problem is when user input text and press submit, I want message to be sent to my mail.
I have read documentation, but I don't understand it.
This is my development.rb, same as production.rb
ActionMailer::Base.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => 'myemail#gmail.com',
:password => 's82SlRM5dPiKL8vjrJfj4w',
:domain => 'heroku.com',
:authentication => :plain
}
ActionMailer::Base.delivery_method = :smtp
user_mailer.rb:
class UserMailer < ActionMailer::Base
default from: "from#example.com"
def welcome
mail(to: "morumotto26#gmail.com", subject: "Welcome", body: "Have a nice day")
end
end
and in controller:
def index
UserMailer.welcome.deliver
end
My logs file look like this:
Started GET "/users/" for 127.0.0.1 at 2014-01-21 16:14:10 +0700
Processing by UsersController#index as HTML
Sent mail to morumotto26#gmail.com (2008.0ms)
Date: Tue, 21 Jan 2014 16:14:10 +0700
From: myemail#gmail.com
To: morumotto26#gmail.com
Message-ID: <52de3a62c89b2_5e8c9ea1881631#tnkjapan10.mail>
Subject: Welcome
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Have a nice day
Rendered text template (0.0ms)
Completed 200 OK in 2018ms (Views: 1.0ms | ActiveRecord: 0.0ms)
First of all you will need to create account on the mandrill.com.
After log in, select type of integration: SMTP or API. SMTP will suite you in most of the cases.
Click SMTP, and create you API key.
Then in your development.rb or production.rb file
add these lines:
config.action_mailer.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:domain => 'domain.com',
:authentication => :plain
}
That's basically all. Now you can send email with Mandrill.
EDIT
Also try to add to your to your environment files these line to perform deliveries and raise delivery errors if any. Also add your default_url_options - in development localhost:3000, in production heroku.com
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "localhost:3000" }
Restart your app before testing
EDIT 2
If you want ActionMailer to send email on submit button click, then you will need to move UserMailer.welcome.deliver to create action of the respective controller.
def create
if #object.save
UserMailer.welcome.deliver
else
render "new"
end
end

Resources