Can't send emails from a Rails app using Devise - ruby-on-rails

I have an app with registration using Devise gem and I have followed the Rails guide on Action Mailer Configuration for Gmail but when I click on Send me password reset instructions I don't receive any emails. This is what I have:
config/environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default :charset => "utf-8"
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
user_name: ENV["TODO_USERNAME"],
password: ENV["TODO_PASSWORD"],
enable_starttls_auto: true
}
I have updated
config/initializers/devise.rb with:
config.mailer_sender = 'myemail#gmail.com'
I have also tried chaning authentication: "plain" to authentication: :plain, I have tried this tutorial, this, this and this, they all provide the same configuration but it doesn't work. I have allowed less secure apps here but no luck. I have also tried removing
domain: "example.com"
completely and changing
config.action_mailer.default_url_options = { host: 'localhost:3000' }
to
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
but that didn't help either.
I've also tried:
ActionMailer::Base.smpt_settings = {
...
}
instead but no luck.
I'v also tried putting this config to config/application.rb, restarting the server severl times but no luck.
This is the console log when send =ing password reset instructions:
Devise::Mailer#reset_password_instructions: processed outbound mail in 15.9ms
Sent mail to [hidden email] (269.2ms)
Date: Thu, 31 May 2018 22:40:03 +0200
From: [hidden email]
Reply-To: [hidden email]
To: [hidden email]
Message-ID: <5b105da3af6e6_164325fb584383ef#Home.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello [hidden email]!</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 http://localhost:3000/users/sign_in
Completed 302 Found in 460ms (ActiveRecord: 9.9ms)
I have already tried everything.
Any ideas?
I am using Rails 5.2

Related

Sendgrid not sending email?

I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email. After making a post request and handling user creation, I get this verification:
UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******#gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******#gmail.com
To: *******#gmail.com
Message-ID: <5a974f2d39c92_c5b2abcd76769fc423e0#albert-VirtualBox.mail>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
My code looks exactly like in this link https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html.
This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending). There is no email in my outbox or inbox.
However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email. I also never did anything with the api key that I made on my sendgrid account. Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com. These all seem kinda sketchy to me, I think the tutorial doesn't contain everything. Does anyone know how to configure this? I've been stuck on it for a while.
Edit:
I tried doing it on production and it is not working. Here is more info:
My production.rb looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DEFAULT_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
}
I have a heroku sendgrid add on. I have set the heroku config vars. In my registrations controller I merely added the line:
UserMailer.send_sign_up_email(#current_user).deliver
My mailer class looks like:
def send_sign_up_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! #{#user.first_name}")
end
However, when I sign up on my website, the user gets added to the database but the email is not sending. Does anyone know why, or how I can debug?
I would suggest to remove all config for ActionMailer from your environment files (i.e. files under /config/environments), except following ones
# Don't care if the mailer can't send.
config.action_mailer.perform_caching = false
# Reference: http://stackoverflow.com/a/20770131/936494
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Then create an initializer /config/initializers/mail_config.rb and add following code to it:
TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)
# =========== DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
when :production, :staging, :experimental
:sendmail
when :test
:test
else
:smtp
end
ActionMailer::Base.delivery_method = delivery_method
# =========== SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)
gmail_config = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: ENV['MAIL_USER_NAME'],
password: ENV['MAIL_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true
}
if :smtp == delivery_method
use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)
smtp_settings = ( use_gmail_config ? gmail_config : {} )
ActionMailer::Base.smtp_settings = smtp_settings
end
# =========== DEFAULT URL OPTIONS
default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol
default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?
ActionMailer::Base.default_url_options = default_url_options
The Settings object is available as part of using config gem. If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.
My /config/settings.yml looks like
default_url_options:
host: ''
port: ''
protocol: ''
and my /config/settings/development.yml looks like
default_url_options:
host: 'localhost'
port: '3000'
Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.
First try it for Gmail account and if it works you can be sure that sending email works. Just make sure in your Gmail account Less Secure Apps setting is enabled.

Rails SendGrid Email From Development Environment

We use SendGrid in a production app and it works fine. We were recently trying to test a new feature/email in development however and cannot seem to get an email to send. Any idea where we're going wrong? We are using similar features to production and we also followed SendGrid's implementation guide. Feels like I'm missing something simple!
First I exported the SENDGRID_USERNAME and SENDGRID_PASSWORD and for kicks added it to my .bash_profile
export SENDGRID_USERNAME=xxxxxxx
export SENDGRID_PASSWORD=xxxxxxx
I've confirmed in the console that these exist and are correct.
Created a developer_email.html.erb file:
<p>Hi! Sendgrid test</p>
And a DeveloperMailer file:
class DeveloperMailer < ActionMailer::Base
default from: "tom#xxxxxx.com"
def developer_email(developer_id)
#recipients = ["tom#xxxxxx.com"]
mail(to: #recipients, subject: 'Does sendgrid work?')
end
end
Updated the development.rb file:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
domain: 'localhost:3000',
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true }
When I go to send the email in the console, it acts like it sent, but the email never actually arrives:
DeveloperMailer.developer_email(1) #to send the email. Seems to work:
2.3.1 :001 > DeveloperMailer.developer_email(1)
Rendered developer_mailer/developer_email.html.erb (1.5ms)
DeveloperMailer#developer_email: processed outbound mail in 133.3ms
=> #<Mail::Message:70263824429080, Multipart: false, Headers: <From: tom#xxxxx.com>, <To: ["tom#xxxx.com"]>, <Subject: Does SendGrid Work?>, <Mime-Version: 1.0>, <Content-Type: text/html>>
#But I never get anything sent to my email
Any idea what I might be missing?
EDIT
Updated development.rb file:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
domain: 'example.com',
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true }
Still no email though.
First, do telnet by following commands
telnet smtp.sendgrid.net 2525
If you get proper response from SMTP Service then change SMTP port from 587 to 2525
In my case "587 port" is not responding
After changing the port it works for me
domain in this context is an SMTP HELO domain, not the actual environment's domain. Change it to the same domain you use in your SendGrid profile and don't specify a port and give it a try.
You'd need to verify the sender email or domain of the sender email before you can send an email with SendGrid. See https://sendgrid.com/docs/ui/sending-email/sender-verification/ and https://sendgrid.com/docs/for-developers/sending-email/sender-identity/#domain-authentication

Running into SMTP error when trying to send email in RoR app

I've been desperately trying to send an email using the Action Mailer class in RoR, and even though the terminal shows that a message is being sent, it keeps returning with this error:
et::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:968:in `check_auth_response'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:739:in `auth_plain'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:731:in `authenticate'...
Here's what my mailer class looks like within app/mailers/my_mailer.rb:
class MyMailer < ActionMailer::Base
default from: "from#example.com"
def welcome_email(user)
#user = user
mail(to: user.email,
from: 'example_email#gmail.com',
subject: 'Welcome!')
end
end
And here's what my config/application.rb file looks like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
#domain: 'gmail.com',
user_name: 'example_email#gmail.com',
password: 'my_password',
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.default_url_options = {
:host => "localhost",
:port => 3000
}
in my config/environments/development.rb file I also have these two lines:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
My problem is that whenever I start the rails console (working in development, have no idea how to run in production), assign a user's email to the user variable then enter the command: MyMailer.welcome_email(user).deliver
the terminal says:
Rendered my_mailer/welcome_email.html.erb (11.7ms)
Rendered my_mailer/welcome_email.text.erb (0.5ms)
Sent mail to abhas.arya#yahoo.com (923.1ms)
Date: Fri, 08 Aug 2014 13:54:38 -0400
From: abhas.aryaa#gmail.com
To: abhas.arya#yahoo.com
Message-ID: <53e50ededebb2_2b0b8082dbf8507c5#Abhass-MacBook-Pro.local.mail>
Subject: Welcome!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_53e50eded7355_2b0b8082dbf85068f";
charset=UTF-8
Content-Transfer-Encoding: 7bit
but then renders the error. My views are simple enough that I didn't think I needed to include them here. I've desperately tried everything, including unlocking google captcha but still nothing works. I'd appreciate any help, all I want is for an email to get sent to the proper inbox. I'd love you forever if you can solve this issue!!!
The reason that you are seeing this error is that Google has blocked your IP. You will need to enable authorization for this IP.
Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.
Then again try to send email from the application, it should work. You will need to send email from your app within 10 min of visiting the above link. By visiting above link, Google will grant access to register new apps for 10 min.
I solved by doing the following:
1) Log into the Google Apps Admin portal and enable Less Secure Apps by checking "Allow users to manage their access to less secure apps" under Security preferences.
2) Login into the account that will serve as the mailer and turn "Allow less secure apps" to ON.
Most of the answers available on-line refer to step 2, but you cannot configure at a user level without the Admin turning the feature on.
This one solved the issue for me after some investigation:
Click https://www.google.com/settings/u/0/security/lesssecureapps
Enable access for less secure apps here by logging in with the email address you have provided in smtp configuration.
I resolved my issue by doing this.
config.action_mailer.default :charset => "utf-8"
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: 'smtp.gmail.com',
port: 587,
domain: 'mysite.com',
user_name: myemail#gmail.com,
password: mypassword,
authentication: 'plain',
enable_starttls_auto: true
}
As google will try to block your sign-in if you have turned off access for less secure apps in your accounts settings. Therefore, follow this link and "Turn on" access for less secure apps.

Can a rails application send emails from the local host?

I think I've set up ActionMailer successfully, however emails do not arrive in their inbox, despite the server stating differently:
Sent mail to julian#designimperial.com (2003ms)
Date: Fri, 30 Aug 2013 22:26:41 +0100
From: from#example.com
To: julian#gmail.com
Message-ID: <52210e11bedc9_16501d84f64257a#Julian-PC.mail>
Subject: Welcome to my awesome site!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, Julian</h1>
<p>
You have successfully signed up to example.com,
your username is: gogo.<br/>
</p>
<p>
To login to the site, just follow this link: http://example.com/login.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Do I need to upload my rails application to a server or heroku inorder for emails to be sent? I know this is the case with PHP mailing and MAMP/XAMPP
Having incorrect settings can result in email failing to send without raising any exceptions. Assuming you're using the SMTP protocol to deliver main, you should ensure that you've properly setup your ActionMailer configuration. Here's a basic configuration for utilizing Gmail (via the Rails Guides):
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
Enabling delivery errors regarding the action mailer to be raised is absurdly helpful in getting the action mailer working. It's also disabled by default.
To get it working, open your development environment file, located at:
yourappname/config/environments/development.rb
around line 17, there is a method that enables and disables error reporting by taking a boolean:
config.action_mailer.raise_delivery_errors = false
Change it to 'true', and restart the rails server. You now have excellent error reporting from rails.
I also like to put my smtp settings directly below in the same file like this:
#SMTP settings
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "gmail.com",
user_name: "mrexample",
password: "01password02"
}
Remember to put your smtp settins in your production.rb and test.rb environment configuration files as well. This example hash is also 100% correct if you're using gmail as the sender address of the emails. The only strings you need to change are the password and username.
Another thing that isn't glaringly obvious with gmail is that your username is everything to left of the # symbol, and the domain everything to the right of the #.
e.g
mrexample#gmail.com
==
config.action_mailer.smtp_settings = {
# address: "smtp.gmail.com",
# port: 587,
domain: "gmail.com",
user_name: "mrexample",
# password: "01password02"
}
Finally, if you're getting the error message
"Errno::ECONNREFUSED (No connection could be made because the target machine actively refused it. - connect(2))"
Chances are the only thing wrong with your application is your smtp settings. Probably your password.

No errors in development.log, but mail wouldn't be sent

I use ActionMailer to send my email through Rails, but I can't receive anything.
My development.log said:
Sent mail to mymail#gmail.com (2107ms)
Date: Wed, 07 Dec 2011 17:14:30 +0800
From: no-reply
To: mymail#gmail.com
Message-ID: <4edf2e765a139_147105390c874e3#raincole-laptop.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>
Hello mymail#gmail.com!
</p>
<p>Someone has requested a link to change your password, and 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>
But there is nothing new in my inbox or spam box.
I have set:
ruby-1.9.2-p290 :008 > Rails.application.config.action_mailer[:raise_delivery_errors]
=> true
So, how can I see the real error logs to debug?
Edit:
I write a email.yml:
development:
reply: "no-reply"
host: "starfish.dev"
smtp:
address: "smtp.gmail.com"
port: 587
domain: "starfish.dev"
authentication: "plain"
user_name: "mymail#gmail.com"
password: "mypassword"
enable_starttls_auto: true
Then in application.rb:
$EMAIL_CONFIG = ActiveSupport::HashWithIndifferentAccess.new YAML.load(File.open("#{Rails.root}/config/email.yml"))[Rails.env]
config.action_mailer.smtp_settings = $EMAIL_CONFIG[:smtp].symbolize_keys if !$EMAIL_CONFIG[:smtp].nil?
in development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
But, it still just sends to log file, no to real address.
The development and test environments don't use real mailers (unless you specifically configure them). By default it just does exactly what you have noticed: it goes through all the motions of sending an email... but then doesn't actually send it, just logs the email that would have been sent.
EDIT: note that this is a reply to the original question, which did not contain any smtp settings.
have you tried telnet smtp.yoursite.com 587?
if there's no responding, please check your firewall.

Resources