簡單郵件傳輸協(xié)議(SMTP)發(fā)送電子郵件及路由的e-mail郵件服務器之間的協(xié)議處理。
Ruby 提供 Net::SMTP 類的簡單郵件傳輸協(xié)議(SMTP)客戶端的連接,并提供了兩個新的方法:new 和 start.
new 帶兩個參數(shù):
- server name 默認為 localhost
- port number 默認為熟知的 25
start 方法帶有以下這些參數(shù):
- server - IP SMTP服務器名稱,默認為localhost
- port - 端口號,默認為25
- domain - 郵件發(fā)件人的域名,默認為 ENV["HOSTNAME"]
- account - 用戶名,默認為 nil
- password - 用戶密碼,默認為 nil
- authtype - 授權類型,默認為 cram_md5
SMTP對象有一個實例方法調(diào)用sendmail,后者通常會被用來做郵寄消息的工作。它有三個參數(shù):
- source - 字符串或數(shù)組或任何同每個迭代一次返回一個字符串。
- sender - 一個字符串,它將會出現(xiàn)在電子郵件字段中。
- recipients - 代表收件人的地址的字符串的字符串或數(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)放置了一個基本的電子郵件消息,使用文件,在這里同時注意正確格式化標題。一封郵件需要發(fā)件人,收件人,主題頭,從電子郵件的主體有一個空白行分隔。
隨著消息要發(fā)送郵件使用Net::SMTP連接到本地機器上的SMTP服務器,然后使用 send_message 方法,從地址,目的地址作為參數(shù)(即使地址電子郵件本身范圍內(nèi),這些并非總是用來將郵件路由)。
如果還沒有在機器上運行一個SMTP服務器,可以使用的Net::SMTP遠程SMTP服務器進行通信。除非使用一個webmail服務(如Hotmail或雅虎郵件),電子郵件提供商將提供外發(fā)郵件服務器的細節(jié),可以提Net::SMTP,具體如下:
Net::SMTP.start('mail.your-domain.com')
這行代碼連接到SMTP服務器mail.your-domain.com 端口25。,而無需使用任何用戶名或密碼。不過,如果需要的話可以指定端口號或其他參數(shù)等。例如:
Net::SMTP.start('mail.your-domain.com',
25,
'localhost',
'username', 'password' :plain)
這個例子連接mail.your-domain.com到SMTP服務器使用的用戶名和密碼以純文本格式。它標識為localhost客戶端的主機名。
使用Ruby發(fā)送HTML電子郵件:
當想要發(fā)送文本消息,Ruby所有內(nèi)容將被視為簡單的文本。即使會在短信中包含HTML標記,它會顯示簡單的文本和HTML標記將不會格式化HTML語法。但是Ruby的 Net::SMTP 提供了實際的HTML郵件發(fā)送HTML消息的選項。
在發(fā)送電子郵件消息時,可以指定一個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ā)送一封電子郵件,要求設置Content-type頭為 multipart/mixed。然后,文本和附件部分可以指定 boundaries 范圍內(nèi)。
兩個連字符后跟一個唯一的編號,不能出現(xiàn)在電子郵件的消息部分的邊界開始。最后一個邊界表示電子郵件的最后一節(jié)的結尾也必須有兩個連字符。
附加文件使用 pack("m") 函數(shù)來base64編碼傳輸之前進行編碼。
例子:
下面的例子將文件 /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
注意:可以指定多個目的地內(nèi)部數(shù)組,但他們需要用逗號分隔。
您可能感興趣的文章:- 使用Ruby來處理JSON的簡單教程
- 初步講解Ruby編程中的多線程
- 使用Ruby編寫發(fā)送郵件的程序的簡單教程