Search code examples
emailruby-on-rails-3actionmailermultipart

Adding a BCC to a multipart ActionMailer action makes it not send as multipart, but garbage


I have code that looks like this:

def coupon_available_notice(order, bcc=false)
  # [...]
  headers = {
    :to       => @user.email,
    :from     => "[email protected]",
    :subject  => subject
  }
  headers[:bcc] = "[email protected]" if bcc == true
  mail(headers)
end

and when bcc is true - the message gets sent as plain text, and all the multipart boundaries and code show up in the e-mail as plain text garbage. When bcc is false, it sends just fine.

Why would that happen??


Solution

  • It turns out this is what I needed:

    def coupon_available_notice(order, bcc=false)
      # [...]
      h = {
        :to       => @user.email,
        :from     => "[email protected]",
        :subject  => subject
      }
      h[:bcc] = "[email protected]" if bcc == true
      mail(h)
    end
    

    headers is a reserved keyword within ActionMailer actions, so it was apparently setting bcc in some non-standard way or something. Just by changing it to 'h', the problem disappeared!

    Stupid mistake? Maybe. But I bet someone else makes the same one ;)