Ruby 傳送郵件 – SMTP
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, '[email protected]',
'[email protected]'
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, '[email protected]',
'[email protected]'
end
傳送帶附件的郵件
如果需要傳送混合內容的電子郵件,需要設定Content-type為multipart/mixed。
這樣就可以在郵件中新增附件內容。
附件在傳輸前需要使用 pack("m") 函式將其內容轉為 base64 格式。
例項
以下例項將傳送附件為 /tmp/test.txt 的郵件:
例項
require 'net/smtp'
filename = "/tmp/test.txt"
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m")
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
--
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, '[email protected]',
['[email protected]'])
end
rescue Exception => e
print "Exception occured: " + e
end
注意:你可以指定多個傳送的地址,但需要使用逗號隔開。