Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the email message body with ActionMailer

I am sending an standard email with Rails like this

@mail = mail(to: registration.user.email, subject: "Registration Confirmation: #{@site.name}")

Now i need to get the message body (or html in this case) from the email. I tried the following but it does not work since it returns not the rendered email but rather the template (including ERB and Haml).

@mail.body
@mail.body.raw_source
@mail.body.encoded

It seems surprisingly difficult to do this. I need the result that a persons sees when receiving the email.

Update

The ERB and Haml i saw was an HTML comment, that's why in the logs it looked like it logged ERB instead of a rendered tempalte. So @mail.body.encoded works fine.

like image 654
Drazen Avatar asked Dec 13 '14 18:12

Drazen


2 Answers

I tried the accepted answer but it is showing the HEADER along with HTML. To get the HTML tags only I've used below code:

@mail.html_part.body.decoded
like image 110
Shiko Avatar answered Nov 18 '22 12:11

Shiko


How about @mail.body.encoded (which should give you the result for which you seek)?

like image 21
TK-421 Avatar answered Nov 18 '22 12:11

TK-421