Ruby使用DSN发送邮件

时间:2015-07-22 16:00:43

标签: ruby email smtp report smtpclient

使用gem mail并考虑http://www.sendmail.org/~ca/email/dsn.html我想做这样的事情:

mail = Mail.new
mail.delivery_method :smtp, :address => 'smtp.server.com', :port => 25
mail.from = 'sender@smtp.server.com'
mail.to = '<recipient@yet.another.server.com> NOTIFY=SUCCESS ORCPT=rfc822;recipient@yet.another.server.com'
mail.deliver!

我得到错误:

...ruby/2.1.0/net/smtp.rb:957:in `check_response': 501 5.1.3 Bad recipient address syntax (Net::SMTPSyntaxError)

然后我尝试猴子修补(我知道它很脏):

class Net::SMTP
  def rcptto(to_addr)
    if $SAFE > 0
      raise SecurityError, 'tainted to_addr' if to_addr.tainted?
    end
    # REPLACE
    # getok("RCPT TO:<#{to_addr}>")
    # WITH
    getok("RCPT TO:<#{to_addr}> NOTIFY=SUCCESS,FAILURE,DELAY ORCPT=rfc822;#{to_addr}")
  end
end

它工作正常,但很难看(

有谁知道更多的法律和美容解决方案?

1 个答案:

答案 0 :(得分:1)

根据GitHub上的documentation from source codeREADME page,您需要做类似的事情:

mail = Mail.new
mail.delivery_method :smtp, address: 'smtp.server.com', port: 25
mail[:from] = 'sender@smtp.server.com'
mail[:to]   = '<recipient@yet.another.server.com> NOTIFY=SUCCESS ORCPT=rfc822;recipient@yet.another.server.com'
mail.deliver!

可能会更优雅的方式:

mail = Mail.new do
  from 'sender@smtp.server.com'
  to '<recipient@yet.another.server.com> NOTIFY=SUCCESS ORCPT=rfc822;recipient@yet.another.server.com'
end

mail.delivery_method :smtp, address: 'smtp.server.com', port: 25

mail.deliver!