Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails convert an html email to a text email

Given an html email message, is there a way to convert that to a text version? I'm doing email ingestion and notice that some times an email doesn't include a text version, especially with blackberry devices.

thanks

like image 264
AnApprentice Avatar asked Mar 04 '11 05:03

AnApprentice


People also ask

What is ActionMailer?

1 Introduction. 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 use mailer in Ruby on Rails?

Mail will automatically Base64 encode an attachment. If you want something different, encode your content and pass in the encoded content and encoding in a Hash to the attachments method. Pass the file name and specify headers and content and Action Mailer and Mail will use the settings you pass in.


2 Answers

HTML to Text is one of the features provided by the Premailer gem.

premailer = Premailer.new('http://example.com/html_email.html')
premailer.to_plain_text

In case you don't want to use it because it does a lot, you can look at the code for how it does it here

like image 78
Akshay Rawat Avatar answered Oct 04 '22 12:10

Akshay Rawat


Perhaps I'm missing something, but couldn't you just take the HTML message and run ActionView::Helpers::SanitizeHelper#strip_tags over it?

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags

like image 34
dnch Avatar answered Oct 04 '22 12:10

dnch