2
votes

I'm having an issue with my Rails application--I'm having trouble sending both a HTML and plaintext version of my email. NOTE: the email does send; however, it's not styled correctly... there is a link to the results below.

It's recommended everywhere that if you want to send HTML you should also send a plain text alternative too. Unfortunately, it appears that I'm doing something wrong, as my application does not allow me to send both HTML and plaintext, without the HTML looking very weird.

here is my mailer model:

class ProjectMembersMailer < ActionMailer::Base

  def membership_invitation(membership)
    @project = membership.project
    @user    = membership.user

    mail( :subject => %(Invitation to join project #{@project.business_name}),
          :from    => %("App" <[email protected]>),
          :to      => @user.account.email,
          :content_type => "text/html" ) do |format|
        format.html
        format.text
      end
  end

end

My project_member_mailer views have two files: membership_invitation.html.haml and membership_invitation.text.erb (please note that the second file is using .erb, but even if I convert it to a .haml extension for consistency I get the same error)

Here is picture of that the output looks like when I attempt to send it using the code above. Please note that I removed some of the text. screen

Basically it looks like it's sending the text version above the html version of the file. Is there an alternate way to sending both plaintext and HTML emails without this happening? Or am I missing something--like, should these emails not be sent simultaneously? Any help would be greatly appreciated. Thank you so much for your time and help!

2
Don't set the content type explicitly - you need to let rails set it to multipart/mixedFrederick Cheung

2 Answers

0
votes

According to the Action Mailer Rails Guide you do not need to use the "format" method, and should remove the "content-type" parameter too.

mail will automatically detect that there are both html and text templates and will automatically create the email as multipart/alternative

Just try:

 mail( :subject => %(Invitation to join project #{@project.business_name}),
      :from    => %("App" <[email protected]>),
      :to      => @user.account.email)
0
votes

I had the exact same problem, and it can be fixed with just one simple thing. Place format.text over format.html

def membership_invitation(membership)
@project = membership.project
@user    = membership.user

mail( :subject => %(Invitation to join project #{@project.business_name}),
      :from    => %("App" <[email protected]>),
      :to      => @user.account.email,
      :content_type => "text/html" ) do |format|
    format.text
    format.html
  end
end