SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用于由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。
Ruby提供了 Net::SMTP 來發送郵件,并提供了兩個方法 new 和 start:
new 方法有兩個參數:
- server name 默認為 localhost
- port number 默認為 25
start 方法有以下參數:
- server - SMTP 服務器 IP, 默認為 localhost
- port - 端口號,默認為 25
- domain - 郵件發送者域名,默認為 ENV["HOSTNAME"]
- account - 用戶名,默認為 nil
- password - 用戶密碼,默認為nil
- authtype - 驗證類型,默認為 cram_md5
SMTP 對象實例化方法調用了 sendmail, 參數如下:
- source - 一個字符串或數組或每個迭代器在任一時間中返回的任何東西。
- sender -一個字符串,出現在 email 的表單字段。
- recipients - 一個字符串或字符串數組,表示收件人的地址。
實例
以下提供了簡單的Ruby腳本來發送郵件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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 |
在以上實例中,你已經設置了一個基本的電子郵件消息,注意正確的標題格式。一個電子郵件要要From,To和Subject,文本內容與頭部信息間需要一個空行。
使用Net::SMTP連接到本地機器上的SMTP服務器,使用send_message方法來發送郵件,方法參數為發送者郵件與接收者郵件。
如果你沒有運行在本機上的SMTP服務器,您可以使用Net::SMTP與遠程SMTP服務器進行通信。如果使用網絡郵件服務(如Hotmail或雅虎郵件),您的電子郵件提供者會為您提供發送郵件服務器的詳細信息:
Net::SMTP.start('mail.your-domain.com')
以上代碼將連接主機為 mail.your-domain.com,端口號為 25的郵件服務器,如果需要填寫用戶名密碼,則代碼如下:
1
2
3
4
|
Net:: SMTP .start( 'mail.your-domain.com' , 25 , 'localhost' , 'username' , 'password' :plain ) |
以上實例使用了指定的用戶名密碼連接到主機為 mail.your-domain.com,端口號為 25的郵件服務器。
使用 Ruby 發送 HTML 郵件
Net::SMTP同樣提供了支持發送 HTML 格式的郵件。
發送電子郵件時你可以設置MIME版本,文檔類型,字符集來發送HTML格式的郵件。
實例
以下實例用于發送 HTML 格式的郵件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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 |
發送帶附件的郵件
如果需要發送混合內容的電子郵件,需要設置Content-type為multipart/mixed。 這樣就可以在郵件中添加附件內容。
附件在傳輸前需要使用 pack("m") 函數將其內容轉為 base64 格式。
實例
以下實例將發送附件為 /tmp/test.txt 的郵件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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 # 發送郵件 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 |
注意:你可以指定多個發送的地址,但需要使用逗號隔開。