其實(shí)只要你任性的可以,用telnet也是可以發(fā)郵件的哦。不過本貓沒那么任性,還是用KISS原則來(lái)發(fā)郵件吧。本篇博文只介紹了如何發(fā)郵件,但沒涉及收郵件的事,以后如有機(jī)會(huì)會(huì)單獨(dú)開一篇博文介紹。
Ruby通過smtp發(fā)郵件有2種操作手段,一是直接用Net::SMTP來(lái)發(fā)送,比較底層。如果還要發(fā)送附件,則需要額外gem:mailfactory,而mailfactory又依賴于包mime-types。即便如此如果郵件服務(wù)器連接需要ssl,則還需要require文件smtp-tls.rb,而這個(gè)rb文件需要openssl包的支持啊!第二種方法是直接使用高層的gem包mail,包含添加附件的功能,不過該gem也依賴于mime-types包,這個(gè)包專門用來(lái)描述郵件文件格式的,俗稱多用途互聯(lián)網(wǎng)郵件擴(kuò)展類型啊。下面依次介紹下每種方法。
方法一:使用Net::SMTP
正常的非加密連接的smtp端口號(hào)為25,如果是加密則可能為587或465,要看具體的郵件服務(wù)器的說明。開始用的是QQ的郵件服務(wù)器,但老是不穩(wěn)定,遂換為hotmail的,但hotmail需要ssl連接。按上面所述需要openssl包。麻煩開始了:rubygems.org用gem死活連不上,后來(lái)將openssl.gem直接下載到本地,用gem install -l openssl.gem安裝,發(fā)現(xiàn)require還是出錯(cuò),需要native ext:openssl.so動(dòng)態(tài)庫(kù)擴(kuò)展,進(jìn)入openssl的ext源代碼目錄用make編譯提示出錯(cuò):Ignore OpenSSL broken by Apple,提示我用其他的openssl庫(kù)路徑,我用毛啊!是不支持Mac OS X嗎?但是檢查了系統(tǒng)中是安裝過openssl的:
/private/etc/openssl
/private/etc/openssl/cert.pem
/usr/bin/openssl
/usr/lib/pkgconfig/openssl.pc
/usr/local/Cellar/openssl
動(dòng)態(tài)庫(kù)也是存在的,但不是ruby 的c_ext!
apple@kissAir: ruby_src$locate libssl.dylib
/Applications/Xcode6-Beta4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib/libssl.dylib
/Applications/Xcode6-Beta4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib/libssl.dylib
/opt/com.bitaxis/lib/libssl.dylib
/opt/local/lib/libssl.dylib
/usr/lib/libssl.dylib
/usr/local/Cellar/openssl/1.0.1e/lib/libssl.dylib
神馬情況?考慮到tk對(duì)ruby的問題(見我另一篇在mac OS X下ruby使用tk的博文),我懷疑該ruby版本本身不支持原生openssl,這個(gè)版本是我在ruby-lang下載的ruby-2.1.5源代碼編譯并且安裝的!遂用rvm下載了其ruby-2.1.5版本,一試竟然可以鳥!但隨后發(fā)現(xiàn)hotmail.com的加密連接還是連不上,又換回QQ郵箱,用非加密的smtp,25端口連接。這回基本可以穩(wěn)定發(fā)送了,如果換位163的郵箱測(cè)試發(fā)現(xiàn)更加穩(wěn)定,上未重構(gòu)的代碼:
#!/usr/bin/ruby
#encoding:utf-8
require 'net/smtp'
require './smtp-tls.rb'
require 'mailfactory'
#Senders and Recipients
from_name = 'localhost'
from_mail = '12345678@qq.com'
to_name = 'ks'
to_mail = '88888888@qq.com'
#Servers and Authentication
#smtp_host = 'smtp.qq.com'
smtp_host = 'smtp.163.com'
smtp_port = 25 #465 587 25
#smtp_domain = 'qq.com'
smtp_domain = 'localhost.localdomain'
smtp_user = "wangyi@163.com"
smtp_pwd = "xxxxxxxx"
#smtp_user = "12345678@qq.com"
#smtp_pwd = 'xxxxxxxx'
#The subject and the message
t = Time.now
subj = '1331 thinkpad test hopy'
msg_body = "send msg from ruby.\n"
#The date/time should look something like: Thu, 03 Jan 2006 12:33:22 -0700
msg_date = t.strftime("%a, %d %b %Y %H:%M:%S +0800")
#Compose the message for the email
神馬情況?考慮到tk對(duì)ruby的問題(見我另一篇在mac OS X下ruby使用tk的博文),我懷疑該ruby版本本身不支持原生openssl,這個(gè)版本是我在ruby-lang下載的ruby-2.1.5源代碼編譯并且安裝的!遂用rvm下載了其ruby-2.1.5版本,一試竟然可以鳥!但隨后發(fā)現(xiàn)hotmail.com的加密連接還是連不上,又換回QQ郵箱,用非加密的smtp,25端口連接。這回基本可以穩(wěn)定發(fā)送了,如果換位163的郵箱測(cè)試發(fā)現(xiàn)更加穩(wěn)定,上未重構(gòu)的代碼:
#!/usr/bin/ruby
#encoding:utf-8
require 'net/smtp'
require './smtp-tls.rb'
require 'mailfactory'
#Senders and Recipients
from_name = 'localhost'
from_mail = '12345678@qq.com'
to_name = 'ks'
to_mail = '88888888@qq.com'
#Servers and Authentication
#smtp_host = 'smtp.qq.com'
smtp_host = 'smtp.163.com'
smtp_port = 25 #465 587 25
#smtp_domain = 'qq.com'
smtp_domain = 'localhost.localdomain'
smtp_user = "wangyi@163.com"
smtp_pwd = "xxxxxxxx"
#smtp_user = "12345678@qq.com"
#smtp_pwd = 'xxxxxxxx'
#The subject and the message
t = Time.now
subj = '1331 thinkpad test hopy'
msg_body = "send msg from ruby.\n"
#The date/time should look something like: Thu, 03 Jan 2006 12:33:22 -0700
msg_date = t.strftime("%a, %d %b %Y %H:%M:%S +0800")
#Compose the message for the email
#如果使用mailfactory發(fā)送則實(shí)際用不著msg格式了
msg = <<END_OF_MESSAGE
Date: #{msg_date}
From: #{from_name} <#{from_mail}>
To: #{to_name} <#{to_mail}>
Subject: #{subj}
#{msg_body}
END_OF_MESSAGE
mail = MailFactory.new
mail.to = to_mail
mail.from = from_mail
mail.subject = subj
mail.text = msg_body
mail.attach(File.expand_path("./mail.rb")) #發(fā)送附件
#smtp = Net::SMTP.new(smtp_host,587)
#smtp.enable_starttls
#Net::SMTP.start(smtp_host, smtp_port, smtp_domain, smtp_user, smtp_pwd, :plain) do |smtp|
Net::SMTP.start(smtp_host,smtp_port,smtp_domain, smtp_user, smtp_pwd, :login) do |smtp|
#smtp.send_message msg, smtp_user, to_mail
#mail.to = to_mail
#puts smtp.methods
#smtp.enable_starttls
smtp.send_message(mail.to_s,smtp_user,to_mail)
end
方法二:使用ruby gem:mail(未完待續(xù))
mail是一個(gè)比較高級(jí)的郵件庫(kù),包含了發(fā)送附件的功能。不過下載下來(lái)本地安裝后開始報(bào)錯(cuò)提示refuse port 25之類的錯(cuò)誤。開始我以為是權(quán)限不夠,用sudo執(zhí)行后,發(fā)現(xiàn)在執(zhí)行require 'mail'命令時(shí)又報(bào)錯(cuò)!發(fā)現(xiàn)當(dāng)初安裝時(shí)未以sudo權(quán)限安裝,先gem uninstall mail.gem,然后重新安裝:sudo gem install mail.gem,此時(shí)在sudo和普通權(quán)限下require 'mail'都正常鳥:
#!/usr/bin/ruby
require 'mail'
smtp = { :address => 'smtp.163.com', :port => 25, :domain => '163.com', \
:user_name => 'wangyi@163.com', :password => 'xxxxxxxx',\
:enable_starttls_auto => true, :openssl_verify_mode => 'none' }
Mail.defaults { delivery_method :smtp, smtp }
mail = Mail.new do
from 'wangyi@163.com'
to '12345678@qq.com'
subject 'test mail'
body 'body:hello send mail way 2 :)'
add_file File.expand_path("./mail2.rb")
end
mail.deliver!
有一個(gè)小問題,就是開始smtp中user_name和from里設(shè)置的發(fā)送郵件賬戶不一樣,導(dǎo)致老是發(fā)送失敗,這里都改成wangyi@163.com就可以了。但在方法一中可以不一樣哦。還有2種方法中domain改成'localhost‘等其他值也能發(fā)送成功,好像沒啥關(guān)系。
最后要說的是,2種方法都是用的非加密的方式連接,即郵件服務(wù)器允許非ssl方式連接,如果郵件服務(wù)器只能加密連接上面代碼就不能用。至于這時(shí)該如何寫代碼,若知道的各位童鞋請(qǐng)不吝賜教哦。