Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multipart emails with Rails: how to favor the HTML version over the plain text one?

I setup a standard rails mailer with multipart view following the official guide, like this:

mail(to: user.email, subject: "Welcome") do |format|
   format.html { render layout: 'my_layout' }
   format.text
end

With the clear and common intent to give priority to the html version of the message, only to find that, as this article points out, calling format.html before the format.text makes a lot of mail clients to show only the text version of the message. In my case I verified (and struggled with) that with both Gmail and Mozilla Thunderbird.

Is there a reliable solution to give precedence to the html version?

like image 374
Darme Avatar asked Mar 08 '13 13:03

Darme


People also ask

What is Action_ mailer in Rails?

Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.

How do I send an email to ROR?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.

How do I Preview mail in Rails?

To preview it, create a file in test/previews and call the mailer method just as you would from any model or controller in your app. Then you can preview the email at [/rails/mailers/notification/welcome](http:// rails/mailers/notification/welcome).


1 Answers

The only solution I found so far is to switch format.html with format.text so that the text format is called before the html one. Which is exactly the opposite of what one would expect.

like image 181
Darme Avatar answered Oct 24 '22 03:10

Darme