SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協(xié)議,它是一組用于由源地址到目的地址傳送郵件的規(guī)則,由它來控制信件的中轉(zhuǎn)方式。
Ruby提供了 Net::SMTP 來發(fā)送郵件,并提供了兩個方法 new 和 start:
new 方法有兩個參數(shù):
server name 默認(rèn)為 localhost
port number 默認(rèn)為 25
start 方法有以下參數(shù):
server - SMTP 服務(wù)器 IP, 默認(rèn)為 localhost
port - 端口號,默認(rèn)為 25
domain - 郵件發(fā)送者域名,默認(rèn)為 ENV["HOSTNAME"]
account - 用戶名,默認(rèn)為 nil
password - 用戶密碼,默認(rèn)為nil
authtype - 驗(yàn)證類型,默認(rèn)為 cram_md5
SMTP 對象示例化方法調(diào)用了 sendmail, 參數(shù)如下:
source - 一個字符串或數(shù)組或每個迭代器在任一時間中返回的任何東西。
sender -一個字符串,出現(xiàn)在 email 的表單字段。
recipients - 一個字符串或字符串?dāng)?shù)組,表示收件人的地址。
以下提供了簡單的Ruby腳本來發(fā)送郵件:
require 'net/smtp'
message = <<MESSAGE_END
From: Private Person <me@fromdomain.com>
To: A Test User <test@todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'me@fromdomain.com',
'test@todomain.com'
end在以上示例中,你已經(jīng)設(shè)置了一個基本的電子郵件消息,注意正確的標(biāo)題格式。一個電子郵件要要From,To和Subject,文本內(nèi)容與頭部信息間需要一個空行。
使用Net::SMTP連接到本地機(jī)器上的SMTP服務(wù)器,使用send_message方法來發(fā)送郵件,方法參數(shù)為發(fā)送者郵件與接收者郵件。
如果你沒有運(yùn)行在本機(jī)上的SMTP服務(wù)器,您可以使用Net::SMTP與遠(yuǎn)程SMTP服務(wù)器進(jìn)行通信。如果使用網(wǎng)絡(luò)郵件服務(wù)(如Hotmail或雅虎郵件),您的電子郵件提供者會為您提供發(fā)送郵件服務(wù)器的詳細(xì)信息:
Net::SMTP.start('mail.your-domain.com')以上代碼將連接主機(jī)為 mail.your-domain.com,端口號為 25的郵件服務(wù)器,如果需要填寫用戶名密碼,則代碼如下:
Net::SMTP.start('mail.your-domain.com',
25,
'localhost',
'username', 'password', :plain)以上示例使用了指定的用戶名密碼連接到主機(jī)為 mail.your-domain.com,端口號為 25的郵件服務(wù)器。
Net::SMTP同樣提供了支持發(fā)送 HTML 格式的郵件。
發(fā)送電子郵件時你可以設(shè)置MIME版本,文檔類型,字符集來發(fā)送HTML格式的郵件。
以下示例用于發(fā)送 HTML 格式的郵件:
require 'net/smtp'
message = <<MESSAGE_END
From: Private Person <me@fromdomain.com>
To: A Test User <test@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'me@fromdomain.com',
'test@todomain.com'
end如果需要發(fā)送混合內(nèi)容的電子郵件,需要設(shè)置Content-type為multipart/mixed。 這樣就可以在郵件中添加附件內(nèi)容。
附件在傳輸前需要使用 pack("m") 函數(shù)將其內(nèi)容轉(zhuǎn)為 base64 格式。
以下示例將發(fā)送附件為 /tmp/test.txt 的郵件:
require 'net/smtp'
filename = "/tmp/test.txt"
# 讀取文件并編碼為base64格式
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m") # base64
marker = "AUNIQUEMARKER"
body =<<EOF
This is a test email to send an attachement.
EOF
# 定義主要的頭部信息
part1 =<<EOF
From: Private Person <me@fromdomain.net>
To: A Test User <test@todmain.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
EOF
# 定義消息動作
part2 =<<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{body}
--#{marker}
EOF
# 定義附件部分
part3 =<<EOF
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"
#{encodedcontent}
--#{marker}--
EOF
mailtext = part1 + part2 + part3
# 發(fā)送郵件
begin
Net::SMTP.start('localhost') do |smtp|
smtp.sendmail(mailtext, 'me@fromdomain.net',
['test@todmain.com'])
end
rescue Exception => e
print "Exception occured: " + e
end注意:你可以指定多個發(fā)送的地址,但需要使用逗號隔開。