2013-01-09 237 views
2

多個收件人我有這樣的代碼:發送電子郵件使用net/SMTP

require "net/smtp" 

MAIL_SERVER = "xxx.ad.xxx.net" 

def lib_sending_report(email_hash) 

    # define message body 

    message = <<"MESSAGE_END" 

From: <#{email_hash[:sender]}> 

To: <#{email_hash[:recipients]}> 

MIME-Version: 1.0 

Content-type: text/html 

Subject: #{email_hash[:subject]} 

<p>Hi All,</p> 

    <p>We've just executed a round of load test.</p> 

<p>Regards</p> 
MESSAGE_END 

    Net::SMTP.start(MAIL_SERVER) do |smtp| 
     smtp.send_message message, email_hash[:sender], email_hash[:recipients] 
    end 
end 

test = "" 
lib_sending_report({:sender => "[email protected]", 
         :recipients => "[email protected]", 
         :subject => "Load.Test.Report.of.#{test}"}) 

當我改變:recipients => "[email protected]":recipients => "[email protected];[email protected]",它給了我這個錯誤:

501 5.5.4 Invalid Address (Net::SMTPSyntaxError) 

我可以成功發送電子郵件時:recipients => "[email protected]"只有一個收件人。

我在哪裏錯了?看來我使用的分隔符(分號)是錯誤的。

我嘗試使用,而不是分號逗號,但沒有奏效

回答

6

的Net :: SMTP的send_message代價「:」數組地址字符串。

每文檔:

to_addr is a String or Strings or Array of Strings, representing the destination mail address or addresses.

從文檔此示例演示如何:

 
Net::SMTP.start('smtp.example.com') do |smtp| 
    smtp.send_message msgstr, 
        '[email protected]', 
        ['[email protected]', '[email protected]'] 
end 
+0

非常感謝。它的工作原理〜我更注重分號並忘記send_message的To參數 – mCY