SMTP connection timing out - ruby-on-rails

I'm pulling my hair out trying to debug an 'execution expired' error while sending email through Rails' ActionMailer, and having no luck. I don't think that it's a Rails problem as the connection also times out when executing telnet smtp.gmail.com 587 from the terminal as well.
The output when executing the telnet command is:
Trying 2607:f8b0:400d:c07::6c...
telnet: connect to address 2607:f8b0:400d:c07::6c: Operation timed out
Trying 74.125.192.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP z32sm7598799qtz.0 - gsmtp
My settings are in config/development.rb as follows:
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 => "gmail.com",
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD'],
:authentication => "plain",
:enable_starttls_auto => true
}
The stack trace is as follows:
Completed 500 Internal Server Error in 30095ms (ActiveRecord: 1.0ms)
Net::OpenTimeout - execution expired:
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/timeout.rb:114:in `timeout'
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/smtp.rb:550:in `do_start'
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/smtp.rb:520:in `start'
mail (2.6.4) lib/mail/network/delivery_methods/smtp.rb:113:in `deliver!'
mail (2.6.4) lib/mail/message.rb:2149:in `do_delivery'
mail (2.6.4) lib/mail/message.rb:237:in `block in deliver'
...
/Users/yawn/.rbenv/versions/2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
I have also tried turning on "Allow access for less secure apps" in Gmail, and have tried my personal gmail address as well as a custom domain Google Apps domain. I initially tried it with only the custom domain Google Apps address and thought that maybe Namecheap blocks SMTP connections the same way that I've read that Digital Ocean does, but the problem persisting with a simple #gmail address seems to rule that out.
When I was going down that path, I also tried adding:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
to /etc/sysctl.conf, but to no avail.
My thinking is now that maybe it's just my ISP blocking SMTP connections and if I were to deploy this to production that maybe it would just work? But I really don't know.
Thanks in advance.

Related

How to solve SMTPAuthentication error / Ruby on Rails app, Devise, SendGrid hosted on Heroku

Good day guys, I am working on a rails application that uses devise to send confirmation emails to registered users. The app properly responds on the development environment and the mails can be caught by mailcatcher. After pushing on heroku my logs reveal a SMTPAuthentication error which leads to a 500 server error each time a user is registered. The logs also reveals that the message gets sent before the error pops up.
2020-02-23T16:04:03.373626+00:00 app[web.1]:
2020-02-23T16:04:03.373627+00:00 app[web.1]: <p>You can confirm your account email through the link below:</p>
2020-02-23T16:04:03.373627+00:00 app[web.1]:
2020-02-23T16:04:03.373628+00:00 app[web.1]: <p>Confirm my account</p>
2020-02-23T16:04:03.373629+00:00 app[web.1]:
2020-02-23T16:04:03.373629+00:00 app[web.1]: [12fef5ae-b267-4f98-a4e9-8dd25b6b077b] Completed 500 Internal Server Error in 152ms (ActiveRecord: 5.4ms)
2020-02-23T16:04:03.373630+00:00 app[web.1]: [12fef5ae-b267-4f98-a4e9-8dd25b6b077b]
2020-02-23T16:04:03.373630+00:00 app[web.1]: [12fef5ae-b267-4f98-a4e9-8dd25b6b077b] Net::SMTPAuthenticationError (530-5.7.0 Authentication Required. Learn more at
2020-02-23T16:04:03.373631+00:00 app[web.1]: ):
Here is my action mailler SMTP configuration on my environment/production.rb file.
# ActionMailer Config
config.action_mailer.default_url_options = {
:host => 'https://own-your-life-campaign.herokuapp.com/'
}
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'own-your-life-campaign.herokuapp.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I also added SendGrid addons on heroku and generated a user name and a password, here is a sample of my environment variables on heroku. am omitting my secrete key base and SENDGRID_API key for security reasons.
SECRET_KEY_BASE: ------------------------------------------------------------------
SENDGRID_API_KEY: -------------------------------------------------------------------
SENDGRID_PASSWORD: -------------------------------------
SENDGRID_USERNAME: --------------------------------------
After creating the SendGrid api key, I verified it on their platform and it gave me a 200 status confirming which is ok. I also changed Api key settings to full access. I don't know if there are further settings I need to do on their platform in other for my post request to properly authenticate from my rails app or It's the fact that am using a Free plan?.

Rails 4: EOFError: end of file reached following any email in DEVELOPMENT only

I have rails app which uses devise ofr authentication and sidekiq for background email jobs. All of a sudden, in development only, I am getting the following error associated with an attempt to send an email from the app (e.g. devise forgotten password)
EOFError: end of file reached
Strangely the app in production works (heroku set up using sendgrid). I haven't changed any of my development email settings....
ActionMailer::Base.default :from => 'slfwalsh#gmail.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => '#gmail.com',
:user_name => "xxxxx#gmail.com",
:password => "xxxxxxxx",
:authentication => 'plain',
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I can't find anything to address this online. Also I note that ALL my apps in development, using similar settings are throwing the same error. I tried a different email address and password (again gmail) and no luck....
For those like Aleksey wondering how the problem was solved, he added a valid domain. Went from domain: '#gmail.com' to domain: "myprojectdomain.com"
For me, setting sender email in my Devise config file fixed the issue.
I have got EOFError: end of file reached issue after migration from EC2 to ECS deployment.
Following is short way to diagnose issue.
$ telnet smtp.sendgrid.net 587
Trying 167.89.118.51...
Connected to smtp.sendgrid.net.
Escape character is '^]'.
220 SG ESMTP service ready at ismtpd0009p1las1.sendgrid.net
...
In case when you don't see kind of 220 greeting from the server after connection, it may mean that something sitting between your client and SMTP server and blocks connection. In our case it was AppMesh proxy which is blocking smtp.
Next test is
openssl s_client -crlf -starttls smtp -connect smtp.sendgrid.net:587
which should show a lot info about SSL.
If you can't see expected info by this tests check your infrastructure first, neither smtp settings will work.
Note that 587 is standard replacement for 25. It is expected that client send STARTTLS to obtain SSL connection. While another alternative - 465 expected to connect with SSL by default.
More details: https://aws.amazon.com/premiumsupport/knowledge-center/smtp-connectivity-timeout-issues-ses/
For those who are stuck....this worked but I unclear why.
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: "myprojectdomain.com",
user_name: "xxxx#gmail.com",
password: "xxxx",
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I had this same issue. The problem was that I was using the Imap host setting (imap.gmail.com) for Smtp host setting (smtp.gmail.com).
Here's how I solved it:
I simply replaced the host
imap.gmail.com
with
smtp.gmail.com
That's all.
I hope this helps.

Unable to send email via SMTP over SSL using cram_md5 authentication

I am writing a Ruby script to send email using the 'mail' gem.
These are my SMTP settings on my local machine:
mailer_options:
address: smtp.gmail.com
port: 465
domain: gmail.com
user_name: example#gmail.com
password: example_password
authentication: :cram_md5
enable_starttls_auto: true
ssl: true
When I try to send email with the above SMTP settings, I get the following exception:
/opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:968:in `check_auth_continue': 504 5.7.4 Unrecognized Authentication Type ka3sm12016635pbc.32 - gsmtp (Net::SMTPSyntaxError)from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:758:in `block in auth_cram_md5from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:941:in `critical'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:756:in `auth_cram_md5'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:731:in `authenticate'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:566:in `do_start'
from /opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/net/smtp.rb:519:in `start'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:2129:in `do_delivery'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/message.rb:234:in `deliver'
from /opt/rtpg/vendor/bundle/ruby/2.0.0/gems/mail-2.5.4/lib/mail/mail.rb:140:in `deliver'
I tried searching for this error and found
SASL LOGIN authentication failed: Invalid authentication mechanism on Rails using Postfix and Dovecot on Ubuntu 12.10
but it does not help.
Why are you using MD5? If you're using TLS (SSL) you won't need to do this because the connection itself is encrypted and even a Base64 encoded password is secure.
When you connect to a server it will advertise what authentication types are allowed. In the case of Google Gmail the header looks like:
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
CRAM-MD5 is not supported. All the others are.
Google's probably dropped MD5 because that method provides little in the way of security given how easily cracked MD5 is.
You won't be able to authenticate to Gmail using cram_md5. Here is an example configuration for using Gmail:
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => ENV['GMAIL_SMTP_USER'],
:password => ENV['GMAIL_SMTP_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
end
Source: https://github.com/mikel/mail/wiki/Sending-email-via-Gmail-SMTP

Can't send mail with gmail smtp server (in discourse)

I'm trying to setup discourse, which is a rails3 webapp, but have some problems configuring smtp with gmail smtp server.
I have registered a new gmail account yesterday, and I can logged in browser and email-client software.
Then I configure discourse, in the file config/environments/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:user_name => "smtp4shuzu#gmail.com",
:password => "12345678",
:authentication => :plain,
:domain => "shuzhu.org",
:enable_starttls_auto => true
}
Start the sidekiq which is used to sending the mails in the background:
nohup bundle exec sidekiq > log/sidekiq.log 2>&1 &
Then start discourse in production mode:
rails server -e production -d
But it doesn't work. I can see some errors in sidekiq.log:
2013-03-01T03:06:02Z 30687 TID-qib28 WARN: {"retry"=>true, "queue"=>"default", "class"=>"Jobs::UserEmail", "args"=>[{"type"=>"signup", "user_id"=>42, "email_token"=>"b40a21ece2b14586e346abfd96685975", "current_site_id"=>"default"}], "jid"=>"558bb6bd5aa36cfc8d3d1e91", "error_message"=>"Connection refused - connect(2)", "error_class"=>"Errno::ECONNREFUSED", "failed_at"=>2013-03-01 03:06:02 UTC, "retry_count"=>0}
2013-03-01T03:06:02Z 30687 TID-qib28 WARN: Connection refused - connect(2)
2013-03-01T03:06:02Z 30687 TID-qib28 WARN: /home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:in initialize'
/home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:inopen'
/home/discourse/.rvm/rubies/ruby-1.9.3-p385/lib/ruby/1.9.1/net/smtp.rb:540:in `tcp_socket'
I have tried all kinds of smtp settings, but none of them works.
UPDATE:
Per #Basil's answer, I just tried:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "smtp4shuzu",
:password => "12345678",
:authentication => "plain",
:enable_starttls_auto => true
}
But it has the same error. The domain shuzu.org is the domain of my site, I was thinking I should passing it to smtp. Now I removed it, but still not working.
At last, I found the (stupid) reason.
I should start sidekiq in production mode:
nohup bundle exec sidekiq -e production > log/sidekiq.log 2>&1 &
Try removing the quotes around the port
:port => 587,
Also, I don't understand why your email address is #gmail but your domain is #shuhzu... smtp settings should show the domain for your email account. If you have a custom gmail, i.e me#custom.com then domain would be custom.com. Here is an example of what smtp settings for your domain should be if you have a custom email address:
{
:address => "smtp.gmail.com",
:port => 587 ,
:domain => "custom.com",
:user_name => "some_email#custom.com",
:password => "some_password",
:authentication => "plain",
:enable_starttls_auto => true
}
Sometimes it is useful to uncomment the following line in /var/discourse/containers/app.yml:
## If you want to set the 'From' email address for your first registration, uncomment and change:
## After getting the first signup email, re-comment the line. It only needs to run once.
- exec: rails r "SiteSetting.notification_email='noreply#YOURDOMAIN.com'"
You should put here the address on behalf of which all Discourse's emails should come. By default Discourse will try to use your forum's domain name, but it may not be allowed by your SMTP. For example, your forum is at forum.example.com, while your Gmail SMTP only allows emails from example.com.
For SMTP without authentication just leave the auth fields like this:
DISCOURSE_SMTP_USER_NAME:
DISCOURSE_SMTP_PASSWORD:
and after everything is saved:
./launcher rebuild app
Once emails are working, you can re-comment this line (with SiteSetting).
You can even just set this SiteSetting via console, but it's more difficult than uncommenting/re-commenting a single line and re-build the container, so I'll not elaborate on this.
On my install, which is one of those pre-made image thingies (Bitnami), I just had to run this:
/opt/discourse-0.9.5-0/ctlscript.sh start discourse_sidekiq
Anyone know how I can automate this so that it happens on startup?
I just set up a new discourse instance in a docker container on my own ubuntu physical on site server and edited the app.yml to contain:
DISCOURSE_SMTP_ADDRESS: 'smtp.gmail.com'
DISCOURSE_SMTP_AUTHENTICATION: 'plain'
DISCOURSE_SMTP_PORT: 587
DISCOURSE_SMTP_USER_NAME: 'my.name#gmail.com'
DISCOURSE_SMTP_PASSWORD: 'myPa$$word'
DISCOURSE_SMTP_ENABLE_START_TLS: true
and it worked. Half the battle was knowing where to put single inverted commas (') and where not to.
Another way was available to me also - my ISP provides a relay smtp for it's static IP customers so I used this in app.yam:
DISCOURSE_SMTP_ADDRESS: mail.myisp.tld
DISCOURSE_SMTP_AUTHENTICATION: none
DISCOURSE_SMTP_PORT: 25
and it works for me also.

problem with Actionmailer "Timeout::Error"?

im trying to send a email with action mailer and it gives me a Timeout::Error (execution expired): even though in the console it says that the e mail is sent:
it shows
Sent mail to aldeirm2#gmail.com
then displayes the e mail that was sent then it shows the following error:
Timeout::Error (execution expired):
/usr/lib/ruby/1.8/timeout.rb:60:in `open'
/usr/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/lib/ruby/1.8/net/smtp.rb:525:in `start'
app/models/appointment.rb:10:in `tomorrows_appointments'
app/models/appointment.rb:8:in `each'
app/models/appointment.rb:8:in `tomorrows_appointments'
app/controllers/show_appointments_controller.rb:11:in `send_email'
-e:2:in `load'
-e:2
Rendered rescues/_trace (35.8ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (internal_server_error)
here is my settings:
config.cache_classes = false
config.whiny_nils = true
config.action_controller.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => 587,
:domian => "gmail.com",
:authentication => :login,
:user_name => "username",
:password => "blablabla",
}
i also tried setting authentication to :plain and using username#gmail.com as the user_name with no hope.
any ideas
I saw a typo there: you wrote
:domian => "gmail.com",
instead of
:domain => "gmail.com",
SMTP requests seem to time out occasionally, probably depending on how long you've set for timeouts. The other problem you'll get is a delay in the web-app while the user waits for your server to communicate with Gmail's SMTP server.
I know gmail is a popular option for new rails apps, but I don't think it's a long-term solution since you have to send the mail through a particular gmail account which has daily sending limits. And even with other accounts, I have found that SMTP timeouts are a problem.
Now I am using SendGrid to send emails and have found that using sendgrid as a relayhost via postfix is the favourable option. This means the user gets a response from the web-app faster and the mail is queued through postfix and then sent via sendgrid (so no more timeouts!).
See here for sendgrid's postfix setup instructions:
http://wiki.sendgrid.com/doku.php?id=postfix
Then, in your rails environment, you just need something like the following:
config.action_mailer.delivery_method = :sendmail
config.action_mailer.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}
In case anyone else has the same problem:
make sure that your firewall allows outgoing connections to port 587
For iptables, you can list all existing rules with:
sudo iptables -L
have a look here for the necessary rules.
If you're using an AWS instance, have a look at your security groups.

Resources