I am fetching email content from database and storing it in variable and passsing it to email template.
i.e:
#email_content = Email.find(1)
#email_content = #email_content.content
My content is like this: "hi, how are you doing, thanks". With html <br/> tags in between the content.
Problem is, I'm getting the mail as plain text, the html tags are not working.
Could someone help me solve this?
In your view file you need to put this
<%= #email_content.html_safe %>
Related
I'm currently trying to make a mailer that sends a plain text, so I fill my content in a .txt.erb file. In this case, I assume that the Content-Type is text/plain.
If now I want to use some HTML layout as a signature, (<%= render :partial => '/layouts/mail/signature.html.erb' %> ), and this HTML file contains some img tag. So what is the Content-Type of this email when Rails sending it?
Anything .html.X is assumed to be HTML.
The way the view filename parsing goes is by specifying type (e.g. .html) and then zero or more handlers, like .erb or .haml.
If you want more control over the format there's documentation on how to do that.
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.
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
How about #mail.body.encoded (which should give you the result for which you seek)?
Hi I have some html in a file called (a partial):
_quotemail.html.erb
I Am sending an email to a user and I want to put this html in the email. I am using mailgun.
I can define my email body like so:
body = "text to go in email"
which works great but I want to instead include the HTML I have in my quotemail partial.
Whats the best way to do this?
I'm trying to implement redactor as a WYSIWYG editor with ruby on rails. Everything seems to be working fine except that when I edit text in the editor the html tags show up. This happens even when I use the html button on the toolbar.
So on the webpage the text appears something like this:
<p>Edited text here</p>
I haven't included any code because I'm not really sure where to begin looking with this so any help at all will be appreciated :)
when using a text editor you have to tell your rails app that the area is html safe.
This is (by default) not the case as people could attack your site by using a text box you have put into your app.
by declaring an area as html safe you should be able to use the html tags as you like.
be aware of the security risk for using this.
e.g.
<div class="description">
<%= #foo.foo_desc.html_safe%>
</div>
Hope this clears it up for you.
in your view try using raw before the text you are trying to show. For example
<%= raw #post.body %>
this will work out with the html tags and show the processed text only without the tags.
In my application I have places where users can send messages and leave comments on content. So to keep this simple, I have Comment, with and id (integer) and body (text).
When printing these in HTML, I want the user's formatting, for example a comment might be entered like this:
comment added
with an enter
In the database, this comes to be:
comment.body.last
"comment added\r\n\r\nwith an enter"
On my page then, this comes to simple look like "comment added with an enter". What I'm wondering is if there is a way in my view to let it know I want to render these enter characters? As 's or something? Does anyone know how to preserve the user's format?
Thanks a ton guys.
simple_format will format text in the way you are describing. It's built in to Rails.
Use simple_format: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
<%= simple_format(your_string_variable) %>