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!

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/2074f734-9f52-4bfe-a92a-da337f28833f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to