2008-09-12 46 views
6

我想從Ruby應用程序發送電子郵件。是否有核心語言的調用來做到這一點,還是有我應該使用的圖書館?什麼是最好的方法來做到這一點?如何從Ruby程序發送郵件?

回答

5

如果您不想使用ActionMailer,您可以使用Net::SMTP(用於實際發送)和tmail一起輕鬆創建電子郵件(包含多個部分等)。

+0

用於tmail的+1。它很好地完成。 – 2010-12-04 20:21:28

0

你也可以考慮採取看看ActionMailer組件作爲船舶的組成部分,但不依賴於軌道上。

1
require 'net/smtp' 
SMTP_SERVER = 'mailserver01' #change to your server 

def send_emails(sender_address, recipients, subject, message_body) 
    recipients.each do |recipient_address| 
     message_header ='' 
     message_header << "From: <#{sender_address}>\r\n" 
     message_header << "To: <#{recipient_address}>\r\n" 
     message_header << "Subject: #{subject}\r\n" 
     message_header << "Date: " + Time.now.to_s + "\r\n" 
     message = message_header + "\r\n" + message_body + "\r\n" 
     Net::SMTP.start(SMTP_SERVER, 25) do |smtp| 
      smtp.send_message message, sender_address, recipient_address 
     end 
    end 
end 
send_emails('[email protected]',['[email protected]', '[email protected]'],'test Email',"Hi there this is a test email hope you like it") 
相關問題