No errors in development.log, but mail wouldn't be sent - ruby-on-rails

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.

Related

Can't send emails from a Rails app using Devise

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

Rails 4 - yandex mail is not sending

In rails 4.2.4, I am using gem 'rails_config' for emails feature. In development environment all mails are sending properly but in test environment it is showing an error like Net::SMTPFatalError (553 5.7.1 Sender address rejected: not owned by auth user.):
In setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.yandex.ru",
:port => 465,
:domain => "yandex.ru",
:authentication => :login,
:user_name => "ssstest#yandex.com",
:password => "pwd567#",
:ssl=> true,
:enable_starttls_auto=> true,
:tls=> true
}
In user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "ssstest#yandex.com"
...
end
Server log's output is,
D, [2016-05-06T06:24:25.089270 #23566] DEBUG -- :
Devise::Mailer#confirmation_instructions: processed outbound mail in 954.3ms
I, [2016-05-06T06:24:26.500832 #23566] INFO -- :
Sent mail to sample_demo#yopmail.com (1408.8ms)
D, [2016-05-06T06:24:26.501240 #23566] DEBUG -- : Date: Fri, 06 May 2016 06:24:25 +0000
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: sample_demo#yopmail.com
Message-ID: <572c389917f09_5c0e10bb99411990#ip-10-65-178-51.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
How can I fix this issue? Please help me
As the error log indicates, please change the from email address inside config/initializers/devise.rb
config.mailer_sender = 'ssstest#yandex.com'
port: 465
authentication: login
smtp_enable_starttls_auto: true
smtp_openssl_verify_mode: true
As mentioned here
https://docs.gitlab.com/omnibus/settings/smtp.html#yandex
Have you added config.action_mailer.delivery_method = :smtp in the environments/test.rb ? And it is not a good practice to store usernames and passwords right in the email setup. Store them as environment variables.

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.

Rails + SendGrid Unauthenticated senders not allowed

I'm currently working on sending emails via sendgrid and currently sending emails to new users is working. ...But strangely, very similar emailing options aren't working - the full email including the recipient is displayed in the log but is never received.
Here's what I have:
In a mail.rb file in initializers:
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => configatron.sendgrid.username,
:password => configatron.sendgrid.password,
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
with the username and password being defined elsewhere - they are confirmed to have their correct values
When a new user registers, I call my UserMailer class and do the following:
def welcome_email(user)
#email = user.email
#name = user.full_name
mail(to: #email, subject: "Welcome!")
end
And the worker calls it by simply doing the following:
UserMailer.welcome_email(user).deliver
This much works; I receive the email and that's that.
But when I try to send an email from a different method, magically things break down.
In the SAME UserMailer class:
def request(org, recipient, item)
#org = org
#recipient = recipient
#item = item
mail(to: #org.email, subject: "A new request has been posted!")
end
Using the SAME function (although this time outside of a worker so I can debug easily):
UserMailer.request(org, recipient, item).deliver
The email appears in the terminal perfectly:
Sent mail to mypersonalemail#test.com (4717ms)
Date: Mon, 31 Dec 2012 01:03:35 -0800
From: mysendingemail#test.com
To: mypersonalemail#test.com
Message-ID: <[longhexstring]#localhost.localdomain.mail>
Subject: A new request has been posted!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_50e154e769903_3c41bba7ec576d5";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]#localhost.localdomain.mail>
Hello world!
----==_mimepart_50e154e769903_3c41bba7ec576d5
Date: Mon, 31 Dec 2012 01:03:35 -0800
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <[longhex]#localhost.localdomain.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<p>
Hello world!
</p>
</body>
</html>
But, I check my inbox on my personal email, the junk, the trash, and received nothing. Even sendgrid has no logs of me ever requesting to send request emails! But the welcome emails are working perfectly fine, and even are logged through sendgrid. I don't see what could cause one group of emails to send but not another and this is really baffling. No obvious errors in syntax jump out at me, and the values I use are of the correct datatype and not nil.
Any help with this strange issue would be extremely appreciated.
The solution ended up being remarkably simple, so for those who find this thread and need help I'll at least post what I did.
In development.rb I did the following to see the error being raised:
config.action_mailer.raise_delivery_errors = true
And got this message:
Net::SMTPFatalError - 550 Cannot receive from specified address <mypersonalemail#test.com>: Unauthenticated senders not allowed
Which is strange considering I thought I was authenticated with the info I put into .env Outputting my configatron.sendgrid.username sure enough is nil. Still trying to figure out why that would be and I'm still not sure how the welcome emails were sent.
Sure enough, changing my smtp settings to be hardcoded values for authentication worked like a charm. Now the question becomes how to get the ENV to not come out nil...
The end solution was simply me being on the wrong localhost. I was using rails's default rails server at port 3000, but foreman start (not sure if by default but in my project's case) was using port 5000 instead.
I had this issue when running code via rake. The problem was that the SENDGRID_USERNAME and SENDGRID_PASSWORD environment variables stored in my .env were not loaded automatically via rake. The solution was to run rake through foreman:
foreman run bundle exec rake namespace:task

Resources