# Ruby 發送郵件 - SMATP
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腳本來發送郵件:
```
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的郵件服務器,如果需要填寫用戶名密碼,則代碼如下:
```
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 格式的郵件:
```
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 的郵件:
```
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
```
**注意:**你可以指定多個發送的地址,但需要使用逗號隔開。
- Ruby 基礎
- Ruby 簡介
- Ruby 環境
- Ruby 安裝 - Unix
- Ruby 安裝 - Windows
- Ruby 命令行選項
- Ruby 環境變量
- Ruby 語法
- Ruby 數據類型
- Ruby 類和對象
- Ruby 類案例
- Ruby 變量
- Ruby 運算符
- Ruby 注釋
- Ruby 判斷
- Ruby 循環
- Ruby 方法
- Ruby 塊
- Ruby 模塊(Module)
- Ruby 字符串(String)
- Ruby 數組(Array)
- Ruby 哈希(Hash)
- Ruby 日期 & 時間(Date & Time)
- Ruby 范圍(Range)
- Ruby 迭代器
- Ruby 文件的輸入與輸出
- Ruby File 類和方法
- Ruby Dir 類和方法
- Ruby 異常
- Ruby 高級
- Ruby 面向對象
- Ruby 正則表達式
- Ruby 數據庫訪問 - DBI 教程
- Ruby CGI 編程
- Ruby CGI方法
- Ruby CGI Cookies
- Ruby CGI Sessions
- Ruby 發送郵件 - SMATP
- Ruby Socket 編程
- Ruby XML, XSLT 和 XPath 教程
- Ruby Web Services 應用 - SOAP4R
- Ruby 多線程
- 免責聲明