user1126946
user1126946

Reputation: 231

ActionMailer cannot attach a HTML file

I am not sure why the email that I send via ActionMailer with an attachment doesn't show me the attachment instead the contents of the attachment is just shown / output into the email.

class MyMailer < ActionMailer::Base
  default :from => "[email protected]"

  def test_email()
    attachments['testresult1.html'] = {
      :filename => 'testresult1.html',
      :body => File.read('C:\\testresult1.html')
    }

  mail(:to => '[email protected]',
    :subject => 'Cool great message'
  )
  end
end

The output of the email looks like this

Date: Tue, 17 Apr 2012 14:11:52 +1000
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8;
filename=testresult1.html
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=testresult1.html
filename: testresult1.html
Content-ID: <[email protected]>

and then the whole contents of the HTML file/attachment.

I'd expect the HTML file to be attached to the email as it would be attached to a normal email and I should be able to double click on the attachment and open it in a browser.

Email Client = Microsoft Outlook
Connection = SMTP
Rails = 3.2.1
ActionMailer = 3.2.1
Mail = 2.4.4

Upvotes: 2

Views: 905

Answers (3)

harsh4u
harsh4u

Reputation: 2600

I have a same issue for 'Noname' and corrupted.

I am using below code:

attachments['126539_statistics.pdf'] = File.read("app/assets/pdfs/126539_statistics.pdf")
mail(:to => email, :subject => subject, :body => message)

No any issue for send mail with attachment...

Upvotes: 1

Guru Prasad
Guru Prasad

Reputation: 21

Thanks a lot Philip :)

I was sending an attachment without any body attribute I was facing following two problem:

  1. name of PDF was always coming as "noname"
  2. sometimes PDF was getting corrupted

But after adding body attribute to mail it works.

So, the correct syntax is:

attachments['mypdf.pdf'] = File.read('#{Rails.root.to_s}/public/test.pdf')
mail(:to => guru****@gmail.com, :from => guru****@gmail.com, :subject => 'Attachment Test Mail', :body => 'Regards, Guru')

Upvotes: 2

Philip C
Philip C

Reputation: 691

You should try adding a body key to #mail.

Upvotes: 2

Related Questions