我想从一个Ruby应用程序中发送电子邮件。核心语言中是否有此功能调用,还是我应该使用库?最佳方法是什么?
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('sender@domain.com',['recip1@test.com', 'recip2@other.com'],'test Email',"Hi there this is a test email hope you like it")
我知道这是对此的一个晚回答,但是这刚刚发布:
http://adam.blog.heroku.com/past/2008/11/2/pony_the_express_way_to_send_email_from_ruby/
可能会有用。