Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Ruby) Getting Net::SMTP working with Gmail...?

Tags:

Does anyone have any quality (and up-to-date) information regarding sending mail via Gmail using Ruby's Net::SMTP? I've seen several examples -- most dating from 2007 to mid-2008 and none of them work for me. I need more current examples that use the most recent 1.8.7 release. I'd also appreciate if the documentation didn't only cover simple examples that no one ever really uses.

Currently I'm receiving an error:

 SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol 

I'm not terribly familiar with SSL as regards the SMTP protocol, so this is all lost on me. Unfortunately the Net::SMTP documentation only covers the bases and doesn't provide a full list of the various potential OpenSSL::SSL contexts either so I can't try various ones.

Anyway, if anyone has any quality info on getting this to work with Gmail it would be most appreciated.

Best.

like image 746
humble_coder Avatar asked Jul 26 '09 04:07

humble_coder


People also ask

Why is my SMTP for Gmail not working?

In Google Mail, you must allow "less secure" apps access in order for your SMTP settings to work. There are two places this setting must be enabled: The first is here: https://myaccount.google.com/ under “Connected apps & sites.”

Can I use SMTP with Gmail?

Use the Gmail SMTP serverIf you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.


2 Answers

Actually the below works for gmail without a plugin or a gem, at least with Ruby 1.9.1p376, but good luck finding documentation that'll tell you so:

    require 'net/smtp'      msg = "Subject: Hi There!\n\nThis works, and this part is in the body."     smtp = Net::SMTP.new 'smtp.gmail.com', 587     smtp.enable_starttls     smtp.start(YourDomain, YourAccountName, YourPassword, :login) do       smtp.send_message(msg, FromAddress, ToAddress)     end 

YourAccountName looks like '[email protected]' & YourDomain can probably be anything you like, but I use the actual domain name.

like image 55
David Avatar answered Oct 29 '22 03:10

David


I actually just got this working. Wrote a quick script to test it.

I was getting a different error than you were (requiring STARTTLS), I also found I had to use port 587 instead of 465.

I found the trick to get it working in a Rails plugin I found. (agilewebdevelopment.com/plugins/net_smtp_tls_support)

if you 'eval' this file (it adds tls support to the standard Net::SMTP library):

http://happiness-is-slavery.net/wp-content/rails-plugins/smtp_add_tls_support/lib/smtp_add_tls_support.rb

then run 'Net::SMTP.enable_tls()'

everything seems to work fine.

Here's my code:

require 'rubygems' require 'net/smtp'  eval File.read("smtp_tls.rb") Net::SMTP.enable_tls()  FROM_EMAIL = "REMOVED" PASSWORD = "REMOVED" TO_EMAIL = "REMOVED"  msgstr = <<END_OF_MESSAGE From: Your Name <#{FROM_EMAIL}> To: my phone <#{TO_EMAIL}> Subject: text message Date: Sat, 23 Jun 2001 16:26:43 +0900 Message-Id: <[email protected]>  This is a test message. END_OF_MESSAGE  Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',                       FROM_EMAIL, PASSWORD, :plain) do |smtp|   smtp.send_message msgstr, FROM_EMAIL, TO_EMAIL  end 

obviously, i downloaded the above mentioned file to the same directory and named it 'smtp_tls.rb'

Hope this helps!

like image 32
Jerceratops Avatar answered Oct 29 '22 03:10

Jerceratops