我编写了一个小脚本来发送带有 Ruby 附件的电子邮件。只要有一个收件人,它就像一个魅力,但是我需要将邮件发送到收件人列表。收件人存储在 yaml 文件中,脚本正在读取该文件,然后在循环的帮助下将电子邮件发送给每个收件人:
Mail.defaults do
delivery_method :smtp, options
end
mail['mail_to'].each do |i|
mail = Mail.new do
to i
from mail['mail_from']
subject mail_subject
body mail_body
add_file :filename => 'Report.pdf', :content => File.read(global['filename_pdf'])
end
mail.deliver!
end
然而,这只适用于第一个收件人,之后 ruby 抛出这个:
/home/juwi/.gem/ruby/1.9.1/gems/mail-2.5.4/lib/mail/check_delivery_params.rb:5:in `check_delivery_params': An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address. (ArgumentError)
from /home/juwi/.gem/ruby/1.9.1/gems/mail-2.5.4/lib/mail/network/delivery_methods/smtp.rb:98:in `deliver!'
from /home/juwi/.gem/ruby/1.9.1/gems/mail-2.5.4/lib/mail/message.rb:248:in `deliver!'
from main.rb:47:in `block in <main>'
from main.rb:33:in `each'
from main.rb:33:in `<main>'
我真的不明白为什么会这样。所以我很想开悟,在这里!