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

試著忘記壹切 提交于 2019-11-26 20:33:51

问题


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.


回答1:


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: <unique.message.id.string@example.com>

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!




回答2:


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 'you@domain.com' & YourDomain can probably be anything you like, but I use the actual domain name.




回答3:


Are you connecting to smtp.gmail.com port 465 I am assuming? .

openssl s_client -connect smtp.gmail.com:587

CONNECTED(00000003) 8298:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:601:

The error looks very similar to yours. The following command does work:

 openssl s_client  -starttls smtp -connect smtp.gmail.com:587

So I think what is happening is that you do not have STARTTLS support enabled. I am not sure how to do it in ruby buy what I did find out is that the action_mailer_tls plugin allows this by patching Net::SMTP. As of Ruby 1.8.7, Net::SMTP has this support built-in.

If you are using Ruby < 1.8.7 here is the patch.




回答4:


You also need to have two newline characters between the subject and the body of the message. Otherwise, only the subject will be transmitted, and the message body will be left blank.

msg = "Subject: My Subject Goes Here\n\nMy message goes here"



回答5:


https://web.archive.org/web/20110113211408/http://blog.segment7.net/articles/2009/07/08/smtp_tls-1-0-3 might help

and here are some more characters to satisfy SO requirements...



来源:https://stackoverflow.com/questions/1183743/ruby-getting-netsmtp-working-with-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!