簡單郵件傳輸協(xié)議(SMTP)發(fā)送電子郵件及路由的e-mail郵件服務(wù)器之間的協(xié)議處理。
Ruby 提供 Net::SMTP 類的簡單郵件傳輸協(xié)議(SMTP)客戶端的連接,并提供了兩個(gè)新的方法:new 和 start.
new 帶兩個(gè)參數(shù):
start 方法帶有以下這些參數(shù):
SMTP對象有一個(gè)實(shí)例方法調(diào)用sendmail,后者通常會被用來做郵寄消息的工作。它有三個(gè)參數(shù):
例子:
下面是一個(gè)簡單使用Ruby腳本的方法來發(fā)送一個(gè)電子郵件。試一次看看吧:
require 'net/smtp'message = <<MESSAGE_ENDFrom: Private Person <[email protected]>To: A Test User <[email protected]>Subject: SMTP e-mail testThis is a test e-mail message.MESSAGE_ENDNet::SMTP.start('localhost') do |smtp| smtp.send_message message, '[email protected]', '[email protected]'end
在這里已經(jīng)放置了一個(gè)基本的電子郵件消息,使用文件,在這里同時(shí)注意正確格式化標(biāo)題。一封郵件需要發(fā)件人,收件人,主題頭,從電子郵件的主體有一個(gè)空白行分隔。
隨著消息要發(fā)送郵件使用Net::SMTP連接到本地機(jī)器上的SMTP服務(wù)器,然后使用 send_message 方法,從地址,目的地址作為參數(shù)(即使地址電子郵件本身范圍內(nèi),這些并非總是用來將郵件路由)。
如果還沒有在機(jī)器上運(yùn)行一個(gè)SMTP服務(wù)器,可以使用的Net::SMTP遠(yuǎn)程SMTP服務(wù)器進(jìn)行通信。除非使用一個(gè)webmail服務(wù)(如Hotmail或雅虎郵件),電子郵件提供商將提供外發(fā)郵件服務(wù)器的細(xì)節(jié),可以提Net::SMTP,具體如下:
Net::SMTP.start('mail.your-domain.com')
這行代碼連接到SMTP服務(wù)器mail.your-domain.com 端口25。,而無需使用任何用戶名或密碼。不過,如果需要的話可以指定端口號或其他參數(shù)等。例如:
Net::SMTP.start('mail.your-domain.com', 25, 'localhost', 'username', 'password' :plain)
這個(gè)例子連接mail.your-domain.com到SMTP服務(wù)器使用的用戶名和密碼以純文本格式。它標(biāo)識為localhost客戶端的主機(jī)名。
使用Ruby發(fā)送HTML電子郵件:
當(dāng)想要發(fā)送文本消息,Ruby所有內(nèi)容將被視為簡單的文本。即使會在短信中包含HTML標(biāo)記,它會顯示簡單的文本和HTML標(biāo)記將不會格式化HTML語法。但是Ruby的 Net::SMTP 提供了實(shí)際的HTML郵件發(fā)送HTML消息的選項(xiàng)。
在發(fā)送電子郵件消息時(shí),可以指定一個(gè)MIME版本,內(nèi)容類型和字符集發(fā)送HTML電子郵件。
例如:
下面的例子作為電子郵件發(fā)送HTML內(nèi)容。試一次看看吧:
require 'net/smtp'message = <<MESSAGE_ENDFrom: Private Person <[email protected]>To: A Test User <[email protected]>MIME-Version: 1.0Content-type: text/htmlSubject: SMTP e-mail testThis is an e-mail message to be sent in HTML format<b>This is HTML message.</b><h1>This is headline.</h1>MESSAGE_ENDNet::SMTP.start('localhost') do |smtp| smtp.send_message message, '[email protected]', '[email protected]'end
作為電子郵件的附件發(fā)送:
混合內(nèi)容發(fā)送一封電子郵件,要求設(shè)置Content-type頭為 multipart/mixed。然后,文本和附件部分可以指定 boundaries 范圍內(nèi)。
兩個(gè)連字符后跟一個(gè)唯一的編號,不能出現(xiàn)在電子郵件的消息部分的邊界開始。最后一個(gè)邊界表示電子郵件的最后一節(jié)的結(jié)尾也必須有兩個(gè)連字符。
附加文件使用 pack("m") 函數(shù)來base64編碼傳輸之前進(jìn)行編碼。
例子:
下面的例子將文件 /tmp/test.txt 作為附件發(fā)送。試一次看看吧:
require 'net/smtp'filename = "/tmp/test.txt"# Read a file and encode it into base64 formatfilecontent = File.read(filename)encodedcontent = [filecontent].pack("m") # base64marker = "AUNIQUEMARKER"body =<<EOFThis is a test email to send an attachement.EOF# Define the main headers.part1 =<<EOFFrom: Private Person <[email protected]>To: A Test User <[email protected]>Subject: Sending AttachementMIME-Version: 1.0Content-Type: multipart/mixed; boundary=#{marker}--#{marker}EOF# Define the message actionpart2 =<<EOFContent-Type: text/plainContent-Transfer-Encoding:8bit#{body}--#{marker}EOF# Define the attachment sectionpart3 =<<EOFContent-Type: multipart/mixed; name=/"#{filename}/"Content-Transfer-Encoding:base64Content-Disposition: attachment; filename="#{filename}"#{encodedcontent}--#{marker}--EOFmailtext = part1 + part2 + part3# Let's put our code in safe areabegin Net::SMTP.start('localhost') do |smtp| smtp.sendmail(mailtext, '[email protected]', ['[email protected]']) endrescue Exception => e print "Exception occured: " + e end
注意:可以指定多個(gè)目的地內(nèi)部數(shù)組,但他們需要用逗號分隔。
新聞熱點(diǎn)
疑難解答
圖片精選