簡(jiǎn)單郵件傳輸協(xié)議(SMTP)發(fā)送電子郵件及路由的e-mail郵件服務(wù)器之間的協(xié)議處理。
Ruby 提供 Net::SMTP 類的簡(jiǎn)單郵件傳輸協(xié)議(SMTP)客戶端的連接,并提供了兩個(gè)新的方法:new 和 start.
new 帶兩個(gè)參數(shù):
- server name 默認(rèn)為 localhost
- port number 默認(rèn)為熟知的 25
start 方法帶有以下這些參數(shù):
- server - IP SMTP服務(wù)器名稱,默認(rèn)為localhost
- port - 端口號(hào),默認(rèn)為25
- domain - 郵件發(fā)件人的域名,默認(rèn)為 ENV["HOSTNAME"]
- account - 用戶名,默認(rèn)為 nil
- password - 用戶密碼,默認(rèn)為 nil
- authtype - 授權(quán)類型,默認(rèn)為 cram_md5
SMTP對(duì)象有一個(gè)實(shí)例方法調(diào)用sendmail,后者通常會(huì)被用來(lái)做郵寄消息的工作。它有三個(gè)參數(shù):
- source - 字符串或數(shù)組或任何同每個(gè)迭代一次返回一個(gè)字符串。
- sender - 一個(gè)字符串,它將會(huì)出現(xiàn)在電子郵件字段中。
- recipients - 代表收件人的地址的字符串的字符串或數(shù)組
例子:
下面是一個(gè)簡(jiǎn)單使用Ruby腳本的方法來(lái)發(fā)送一個(gè)電子郵件。試一次看看吧:
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)放置了一個(gè)基本的電子郵件消息,使用文件,在這里同時(shí)注意正確格式化標(biāo)題。一封郵件需要發(fā)件人,收件人,主題頭,從電子郵件的主體有一個(gè)空白行分隔。
隨著消息要發(fā)送郵件使用Net::SMTP連接到本地機(jī)器上的SMTP服務(wù)器,然后使用 send_message 方法,從地址,目的地址作為參數(shù)(即使地址電子郵件本身范圍內(nèi),這些并非總是用來(lái)將郵件路由)。
如果還沒(méi)有在機(jī)器上運(yùn)行一個(gè)SMTP服務(wù)器,可以使用的Net::SMTP遠(yuǎn)程SMTP服務(wù)器進(jìn)行通信。除非使用一個(gè)webmail服務(wù)(如Hotmail或雅虎郵件),電子郵件提供商將提供外發(fā)郵件服務(wù)器的細(xì)節(jié),可以提Net::SMTP,具體如下:
Net::SMTP.start('mail.your-domain.com')
這行代碼連接到SMTP服務(wù)器mail.your-domain.com 端口25。,而無(wú)需使用任何用戶名或密碼。不過(guò),如果需要的話可以指定端口號(hào)或其他參數(shù)等。例如:
Net::SMTP.start('mail.your-domain.com',
25,
'localhost',
'username', 'password' :plain)
這個(gè)例子連接mail.your-domain.com到SMTP服務(wù)器使用的用戶名和密碼以純文本格式。它標(biāo)識(shí)為localhost客戶端的主機(jī)名。
使用Ruby發(fā)送HTML電子郵件:
當(dāng)想要發(fā)送文本消息,Ruby所有內(nèi)容將被視為簡(jiǎn)單的文本。即使會(huì)在短信中包含HTML標(biāo)記,它會(huì)顯示簡(jiǎn)單的文本和HTML標(biāo)記將不會(huì)格式化HTML語(yǔ)法。但是Ruby的 Net::SMTP 提供了實(shí)際的HTML郵件發(fā)送HTML消息的選項(xiàng)。
在發(fā)送電子郵件消息時(shí),可以指定一個(gè)MIME版本,內(nèi)容類型和字符集發(fā)送HTML電子郵件。
例如:
下面的例子作為電子郵件發(fā)送HTML內(nèi)容。試一次看看吧:
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)容發(fā)送一封電子郵件,要求設(shè)置Content-type頭為 multipart/mixed。然后,文本和附件部分可以指定 boundaries 范圍內(nèi)。
兩個(gè)連字符后跟一個(gè)唯一的編號(hào),不能出現(xiàn)在電子郵件的消息部分的邊界開(kāi)始。最后一個(gè)邊界表示電子郵件的最后一節(jié)的結(jié)尾也必須有兩個(gè)連字符。
附加文件使用 pack("m") 函數(shù)來(lái)base64編碼傳輸之前進(jìn)行編碼。
例子:
下面的例子將文件 /tmp/test.txt 作為附件發(fā)送。試一次看看吧:
require 'net/smtp'
filename = "/tmp/test.txt"
# Read a file and encode it into base64 format
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m") # base64
marker = "AUNIQUEMARKER"
body =EOF
This is a test email to send an attachement.
EOF
# Define the main headers.
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
# Define the message action
part2 =EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{body}
--#{marker}
EOF
# Define the attachment section
part3 =EOF
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"
#{encodedcontent}
--#{marker}--
EOF
mailtext = part1 + part2 + part3
# Let's put our code in safe area
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
注意:可以指定多個(gè)目的地內(nèi)部數(shù)組,但他們需要用逗號(hào)分隔。
您可能感興趣的文章:- 使用Ruby來(lái)處理JSON的簡(jiǎn)單教程
- 初步講解Ruby編程中的多線程
- 使用Ruby編寫(xiě)發(fā)送郵件的程序的簡(jiǎn)單教程