- Ruby 基礎
- Ruby - 首頁
- Ruby - 概述
- Ruby - 環境設定
- Ruby - 語法
- Ruby - 類和物件
- Ruby - 變數
- Ruby - 運算子
- Ruby - 註釋
- Ruby - IF...ELSE
- Ruby - 迴圈
- Ruby - 方法
- Ruby - 塊
- Ruby - 模組
- Ruby - 字串
- Ruby - 陣列
- Ruby - 雜湊表
- Ruby - 日期和時間
- Ruby - 範圍
- Ruby - 迭代器
- Ruby - 檔案 I/O
- Ruby - 異常
使用 Ruby 傳送電子郵件 - SMTP
簡單郵件傳輸協議 (SMTP) 是一種協議,用於處理電子郵件的傳送和郵件伺服器之間的電子郵件路由。
Ruby 提供了 Net::SMTP 類用於簡單郵件傳輸協議 (SMTP) 的客戶端連線,並提供了兩個類方法 new 和 start。
new 方法接受兩個引數:
伺服器名稱,預設為 localhost。
埠號,預設為眾所周知的埠 25。
start 方法接受以下引數:
伺服器 - SMTP 伺服器的 IP 名稱,預設為 localhost。
埠 - 埠號,預設為 25。
域名 - 郵件傳送者的域名,預設為 ENV["HOSTNAME"]。
賬戶 - 使用者名稱,預設為 nil。
密碼 - 使用者密碼,預設為 nil。
authtype - 授權型別,預設為 cram_md5。
SMTP 物件有一個名為 sendmail 的例項方法,通常用於執行郵件傳送任務。它接受三個引數:
源 - 字串或陣列,或任何具有 each 迭代器並一次返回一個字串的物件。
傳送者 - 將出現在電子郵件 發件人 欄位中的字串。
收件人 - 表示收件人地址的字串或字串陣列。
示例
這是一個使用 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
這裡,你將基本的電子郵件放在 message 變數中,使用文件,注意正確格式化標題。電子郵件需要發件人、收件人和主題標題,並用空行與電子郵件正文分開。
要傳送郵件,你可以使用 Net::SMTP 連線到本地機器上的 SMTP 伺服器,然後使用 send_message 方法以及郵件、發件人地址和目標地址作為引數(即使發件人和收件人地址在電子郵件本身中,這些地址並不總是用於郵件路由)。
如果你的機器上沒有執行 SMTP 伺服器,你可以使用 Net::SMTP 與遠端 SMTP 伺服器通訊。除非你使用的是網路郵件服務(例如 Hotmail 或 Yahoo!郵箱),否則你的電子郵件提供商會提供你可以提供給 Net::SMTP 的外發郵件伺服器詳細資訊,如下所示:
Net::SMTP.start('mail.your-domain.com')
這行程式碼連線到 mail.your-domain.com 埠 25 上的 SMTP 伺服器,不使用任何使用者名稱或密碼。但是,如果需要,你可以指定埠號和其他詳細資訊。例如:
Net::SMTP.start('mail.your-domain.com',
25,
'localhost',
'username', 'password' :plain)
此示例使用明文格式的使用者名稱和密碼連線到 mail.your-domain.com 上的 SMTP 伺服器。它將客戶端主機名標識為 localhost。
使用 Ruby 傳送 HTML 電子郵件
當你使用 Ruby 傳送文字訊息時,所有內容都將被視為純文字。即使你在文字訊息中包含 HTML 標籤,它也會顯示為純文字,並且 HTML 標籤不會根據 HTML 語法進行格式化。但是 Ruby Net::SMTP 提供了將 HTML 訊息作為實際 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"
# 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
注意 - 你可以在陣列內指定多個目標,但它們應該用逗號隔開。