Forum: Ruby on Rails Help with Custom Mail Header

Posted by Adam O'Connor (Guest)
on 2011-08-10 21:31
(Received via mailing list)
Hi -

I'm stuck on something 'simple' - custom mail header.  Seems this
should work but can't get it to come out correctly.  Any help
appreciated:

http://pastie.org/private/skedkstanvm8hpzr8ounia
Posted by Chris M. (chris_m)
on 2011-08-10 21:55
(Received via mailing list)
On 10 Aug 2011, at 16:35, Adam O'Connor wrote:

> I'm stuck on something 'simple' - custom mail header.  Seems this
> should work but can't get it to come out correctly.  Any help
> appreciated:
>
> http://pastie.org/private/skedkstanvm8hpzr8ounia

In your call to #mail:

    mail(:to => 'test@sample.com',
         :from => email,
         :subject => "Message from the site",
         :headers['X-SMTPAPI'] => "{\"category\" : \"Drip Email\"}"
    )

you're passing a hash of headers to the #mail method, and one of your 
keys is :headers['X-SMTPAPI']. Ruby tries to execute this as calling the 
#[] method on the symbol :headers, which returns nil (under 
ActiveSupport).

This all happens before the #mail method gets a chance to see it; in 
other words, your method call looks like this:

    mail(:to => 'test@sample.com',
         :from => email,
         :subject => "Message from the site",
         nil => "{\"category\" : \"Drip Email\"}"
    )


That's why you're seeing the {"category" : "Drip Email"} value appear in 
your email, but with an empty name for the header.

To set a header value, you can either specify it directly in the hash 
you pass to #mail:

    mail(:to => 'test@sample.com',
         :from => email,
         :subject => "Message from the site",
         'X-SMTPAPI' => "{\"category\" : \"Drip Email\"}"
    )

or you can use the #headers method separately:

    headers['X-SMTPAPI'] = "{\"category\" : \"Drip Email\"}"
    mail(:to => 'test@sample.com',
         :from => email,
         :subject => "Message from the site"
    )

Chris
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.