“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

Accessing TMail::Mail headers

While trying to test TMail::Mail#to header, despite setting the address in full with the recipients name I kept getting just the address back

>> t = TMail::Mail.new
=> #<TMail::Mail port=#<TMail::StringPort:id=0x3ff92369be30> bodyport=nil>
>> t.to = "RobL <someaddress@robl.me>"
=> "RobL <someaddress@robl.me>"
>> t.to
=> ["someaddress@robl.me"]

It seems there are a few ways of grabbing the to address back, to just returns an array of e-mail addresses. to_addrs or [‘to’] actually return the full “to” address.

>> t.to_addrs
=> [#<TMail::Address contact@robl.me>
>> t.to_addrs.first.to_s
=> "RobL <someaddress@robl.me>"
>> t['to'].first.to_s
=> "RobL <someaddress@robl.me>"