Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating 'from' does not work #13

Closed
bg opened this issue Dec 1, 2009 · 2 comments
Closed

Updating 'from' does not work #13

bg opened this issue Dec 1, 2009 · 2 comments

Comments

@bg
Copy link

bg commented Dec 1, 2009

I tried to extend Mail::Message to provide a default 'from' address by wrapping the old initialize with my own which would first call the old initialize, then set defaults, then instance_eval if a block was specified, and finally return self. All seemed to go well until I tried to override from= inside the block. This resulted in a message that, when delivered, raised this error:

NoMethodError: undefined method `addresses' for #<Mail::OptionalField:0xb72ab068>
    from /home/ben/.gem/ruby/1.8/gems/mail-1.3.0/lib/mail/field.rb:120:in `send'
    from /home/ben/.gem/ruby/1.8/gems/mail-1.3.0/lib/mail/field.rb:120:in `method_missing'
    from /home/ben/.gem/ruby/1.8/gems/mail-1.3.0/lib/mail/network/delivery_methods/smtp.rb:60:in `deliver!'
    from /home/ben/.gem/ruby/1.8/gems/mail-1.3.0/lib/mail/network/deliverable.rb:11:in `perform_delivery!'
    from /home/ben/.gem/ruby/1.8/gems/mail-1.3.0/lib/mail/message.rb:68:in `deliver!'
    from (irb):20
    from /home/ben/.gem/ruby/1.8/gems/treetop-1.4.2/lib/treetop/compiler/metagrammar.rb:415

First, I thought the problem might be that I now had multiple 'from' addresses and went to RFC 822 to see if that even made sense. Indeed, multiple authorship is valid, provided a single 'Sender' is specified, but when I inspected the message in irb, I did not find that multiple authorship had been set. Instead, I noticed something very odd about the message that results from trying to call 'from' twice, and that is that FromField is removed and replaced with an OptionalField with @name==:from (I have thrown some debug print statements in to see when we're doing an 'update' vs. 'create'):

>> mail.from='bg@dymaxion.ca'
[:create, "from", "bg@dymaxion.ca"]
=> "bg@dymaxion.ca"
>> mail.inspect
=> "#<Mail::Message:0xb72aff64 @header=#<Mail::Header:0xb72afd84 @fields=[#<Mail::Field:0xb72ade80 @field=#<Mail::FromField:0xb72addf4 @length=nil, @element=nil, @tree=nil, @value=\"bg@dymaxion.ca\", @name=\"From\">>], @raw_source=\"\">, @raw_source=\"\", @body=#<Mail::Body:0xb72afd5c @charset=\"US-ASCII\", @encoding=nil, @raw_source=\"\">>"
>> mail.from='barmstrong@dymaxion.ca'
[:update, "from", "barmstrong@dymaxion.ca"]
=> "barmstrong@dymaxion.ca"
>> mail.inspect
=> "#<Mail::Message:0xb72aff64 @header=#<Mail::Header:0xb72afd84 @fields=[#<Mail::Field:0xb72ade80 @field=#<Mail::OptionalField:0xb72ab068 @length=nil, @element=nil, @tree=nil, @value=\"barmstrong@dymaxion.ca\", @name=:from>>], @raw_source=\"\">, @raw_source=\"\", @body=#<Mail::Body:0xb72afd5c @charset=\"US-ASCII\", @encoding=nil, @raw_source=\"\">>"

Of course, to work around this problem, I could make users delete from after establishing the default to set a new value, but that's not very friendly, e.g.

>> mail=Mail.new
...
>> mail.from.value
"my@default.from.address"
>> mail.from nil
...
>> mail.from "some@new.address"

Ideally, what I would like mail.from= to do is to replace the old from address with the new. But failing that, I would like some way to specify a default 'from' address to use that, if no from address was specified by the time the message is delivered, would be used.

@mikel
Copy link
Owner

mikel commented Dec 10, 2009

Good catch. I have fixed this in commit fa257d6..001a868. It will be included in the next gem release. It is a very simple fix if you want to patch your copy of mail.

@bg
Copy link
Author

bg commented Dec 11, 2009

Thanks! Works for me.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants