当前位置: 动力学知识库 > 问答 > 编程问答 >

actionmailer - Rails - How do I use full email addresses without triggering Net::SMTPFatalError?

问题描述:

I am new to rails and using rails-2.3.5 and ruby-1.8.7. Here is my notifier.rb model:

# app/models/notifier.rb

class Notifier < ActionMailer::Base

default_url_options[:host] = "foo.com"

#This method sends an email with token to users who request a new password

def password_reset_instructions(user)

subject "Password Reset Instructions"

from "Support Team<[email protected]>"

recipients user.email

sent_on Time.now

body :edit_password_reset_url =>

edit_password_reset_url(user.perishable_token)

end

end

When I call this method I get the following error:

Net::SMTPFatalError in Password resetsController#create

555 5.5.2 Syntax error. 36sm970138yxh.13

I found an article that said the problem was a bug in ruby-1.8.4 and that the fix is to remove the angle brackets from the :from field. Sure enough, if I just use "[email protected]" instead of "Support Team<[email protected]>" everything works fine.

However, there is no reference to this issue in either the rails-2.3.5 API or ActionMailer Basics rails guide, and in fact both show "name<mail address>" in their actionmailer setup examples. Anyone know what I am doing wrong?

网友答案:

From the ticket that Travis referenced, it looks as though you can possibly avoid the problem with:

  def password_reset_instructions(user)  
    subject       "Password Reset Instructions"  
    from          "Support Team<[email protected]>"  
+   headers       "return-path" => '[email protected]'
    recipients    user.email  
    sent_on       Time.now  
    body          :edit_password_reset_url => 
                   edit_password_reset_url(user.perishable_token)  
  end  

Otherwise, you can grab one of the patches noted in the ticket or wait for 2.3.6 or 3.x

网友答案:

Rails/ActionMailer broke this:

https://rails.lighthouseapp.com/projects/8994/tickets/2340

And since critical bugs like this don't get high priority or interim releases to fix them in the Rails project, you either have to patch it up yourself or wait a looong time to get it fixed. Just like this insanely bad bug that came out in Rails 2.3.4 that made Rails completely unusable for Ruby 1.9: https://rails.lighthouseapp.com/projects/8994/tickets/3144-undefined-method-for-string-ror-234 . Took months to get a fix for that.

网友答案:

The problem is a perform_delivery_smtp method from ActionMailer::Base used in rails 2.3.4 and 2.3.5. You can always try to monkey-patch-it like that:

class ApplicationMailer < ActionMailer::Base

  def welcome_email(user)
    recipients user.email from "Site Notifications<[email protected]>"
    subject "Welcome!"
    sent_on Time.now
    ...
  end

  def perform_delivery_smtp(mail)
    destinations = mail.destinations
    mail.ready_to_send
    sender = mail['return-path'] || mail.from
    smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
    smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
    smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
               smtp_settings[:authentication]) do |smtp|
      smtp.sendmail(mail.encoded, sender, destinations)
    end
  end

end
分享给朋友:
您可能感兴趣的文章:
随机阅读: