Send HTML markup email with attachment

31 views
Skip to first unread message

joel briggs

unread,
Sep 6, 2017, 1:03:14 AM9/6/17
to Ruby on Rails: Talk

So in my app a user can create the body of an email they send to "contacts" they have in the app.

The app uses a CKEditor textbox for the user to enter the body of the email. They can also attach a file to the email.

Using this code the email gets sent the file is attached properly but all of the HTML tags are visible in the body of the email.


Controller

@file = EmailFile.create! file: params[:attachment]
@attachment_path = @file.file.url
@file_name = @file.file_file_name

email_params = {
  to: email_recipients,
  from: current_user.email,
  subject: params[:email_subject],
  body: params[:email_body]
}

CustomMailer.send_individual_email(email_params, @attachment_path, @file_name).deliver_now!


Mailer

  def send_individual_email(email_params, attachment_path, file_name)
    if file_name.present?
      open_file = open(attachment_path)
      attachments[file_name] = File.read(open_file)
    end

    mail(to: email_params[:to],
         from: email_params[:from],
         subject: email_params[:subject],
         body: email_params[:body])
  end


send_individual_email.html.erb

<!DOCTYPE html>
<html xmlns=3D"http://www.w3.org/1999/xhtml" dir=3D"auto">
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
  <%= body.html_safe %>
</body>
</html>

there is also a send_individuial_email.text.erb file.


If I change the Mailer to add the content_type, the attached file becomes a garbled string of text in the email but the HTML tags properly format the text


Mailer

mail(to: email_params[:to],
     from: email_params[:from],
     subject: email_params[:subject],
     content_type: 'text/html',
     body: email_params[:body])


The Rails 5.0.1 app is hosted on Heroku and is using Sendgrid to send the emails.

I have tried changing the content_type as well as removing the .html_safe in the view.


I can get the email to work fine with an attachment and plain text for the body, or no attachment and marked up text showing in the body but not both at the same time.


I know there must be something I'm doing wrong.


How can I send an email with markup text in the body with an attached file?

Thanks!

Frederick Cheung

unread,
Sep 8, 2017, 5:17:54 AM9/8/17
to Ruby on Rails: Talk


On Wednesday, September 6, 2017 at 6:03:14 AM UTC+1, joel briggs wrote:


If I change the Mailer to add the content_type, the attached file becomes a garbled string of text in the email but the HTML tags properly format the text


 

Mailer

mail(to: email_params[:to],
     from: email_params[:from],
     subject: email_params[:subject],
     content_type: 'text/html',
     body: email_params[:body])



If you do this it uses this body option  instead of your template. It looks like when you use this form of mail, it will always set the content type of the entire message to text/html, which isn't what you want: you want the overall message to be a multipart one, and the part derived from the body option to be text/html

I would suggest not setting body here at all (or content type). and letting it render your template (which I think is what you probably thought it was doing at all). You'll need to set @body, as you would with a controller and its view

Fred
Reply all
Reply to author
Forward
0 new messages