tomasbasham
tomasbasham

Reputation: 1734

Default Rails ActionMailer not delivering multipart emails correctly

I have created, what I thought to be, a MultiPart email using rails but upon delivery to any client it is only ever shown in plain text therefore printing out the headers and all the templating.

def order_receipt(order)
  @order = order
  mail subject: "Order #{order.id}"
end

OrderMailer.order_receipt(Order.first).deliver

The email I receive in the clients is as such:

----==_mimepart_5525126014929_7e3f9b4c1fb98c525c2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: etc...

And then just boxes and text where styled content should be.

Do I need to explicitly set the formats in the mailer like so:

def order_receipt(order)
  @order = order
  mail subject: "Order #{order.id}" do |format|
    format.text
    format.html
  end
end

because I thought that was the default ActionMailer behaviour

Upvotes: 3

Views: 868

Answers (2)

tomasbasham
tomasbasham

Reputation: 1734

Strangely I had to specify that the content type would be multipart:

mail content_type: 'multipart/alternative', subject: "Order #{order.id}"

This fixed the issue immediately. I find it very strange that Rails has to be told this. Bug perhaps in the version I am using?

Upvotes: 3

HiteshRawal
HiteshRawal

Reputation: 455

Yes, Action Mailer automatically send multipart emails if you have different templates for the same action.

But if this is not the case then you can also pass content-type along with parameter to mail. for eg.

def order_receipt(order)
  @order = order
  mail(content_type: "text/html", subject: "Order #{order.id}")
end

Upvotes: 2

Related Questions