Home > Net >  Rails Action Mailer Issue
Rails Action Mailer Issue

Time:01-12

Im trying to send HTML code as a variable using ActionMailer's ( Ruby on Rails default mailer ).

I have an example bellow with an easy variable ( thats also HTML code )


def newsletter // ==> my method to send
@template = "<h1>wait what ?</h1>"
mail(to: "[email protected]", subject: "test de newsletter")
end

newsletter.html.erb // ==> my html.erb file
<%= @template %>
<h1>wait what ?</h1>

the result I get is :

<h 1 > wait what ? </ h 1> // ==> cannot write h1 here tho 
<b>wait what ?</b>

// ==> in the  terminal the mail looks like this 
<body>
&lt;h1&gt;wait what ?;/h1&gt;
<h1>wait what ? </h1>
</body>

Maybe I can upload an entire file as template ? How can I write HTML code in my variable ?

CodePudding user response:

Maybe I can upload an entire file as template ?

Yes, you can use an entire file as the template. It is called a view. See reference below.

How can I write HTML code in my variable ? Use the raw method:

<%= raw @template %>

You may want to try this ActionMailer walkthrough.

Reference: Create a Mailer View

CodePudding user response:

In your view file(newsletter.html.erb) mark the string as trusted safe.

<%= @template.html_safe %>

Use raw if there are chances that the string will be nil.

<%= raw(@template) %>
  • Related