Rails ActionMailer attachments appear as garbled text

I am trying to send PDF attachments to one of my rail applications, but the application appears as text, and not as the correct pdf.

My mail is as follows:

class Notifications < ActionMailer::Base def contact(email_params, sent_at = Time.now) subject "" << email_params[:subject] recipients "" << email_params[:client] from "#{email_params[:name]} <#{email_params[:address]}>" attachments['invoice.pdf'] = File.read("#{Rails.root.to_s}/public#{email_params[:attach]}") unless email_params[:attach].blank? sent_on sent_at body :message => email_params[:body], :sender_name => email_params[:name] end end 

And what I get in my inbox:

 -- Date: Thu, 23 Feb 2012 13:50:58 -0800 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-ID: <[mailserver]> Message Body -- Date: Thu, 23 Feb 2012 13:50:58 -0800 Mime-Version: 1.0 Content-Type: application/pdf; charset=UTF-8; filename=invoice.pdf Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=invoice.pdf Content-ID: <[mailserver]> 

Following a very long string of ascii characters

Without any attachment, this header information is not sent, and I just get the โ€œMessage Bodyโ€ part in my inbox. I suspect that part of the File.read () file in the attachment line in my mailbox is somehow the culprit, but it points to a valid pdf file on my server that I can access through my browser. The logs only say that the mail was sent with the correct parameters, but it is sent as what looks like decompiled text or something like if you opened the PDF in a text editor.

In the mail view, contact.html.erb is simple: <%= @message %> If that helps.

Any idea what I can do wrong?

Thanks.

Update:

Apparently I needed both contact.erb and contact.html.erb in my views directory. Just copying the file and renaming it worked. However, the message body is now empty. Only the application appears.

+4
source share
4 answers

Well, this eventually ended:

 class Notifications < ActionMailer::Base def contact(email_params, sent_at = Time.now) subject "" << email_params[:subject] recipients "" << email_params[:client] from "#{email_params[:name]} <#{email_params[:address]}>" part "text/plain" do |p| p.part :content_type => "text/plain", :body => render("contact.erb") end @message = email_params[:body] attachments['invoice.pdf'] = File.read("#{Rails.root.to_s}/public#{email_params[:attach]}") unless email_params[:attach].blank? sent_on sent_at mail(:to => email_params[:client], :from => email_params[:address], :subject => email_params[:subject]) do |format| format.html { render 'contact' } format.text { render :text => email_params[:body] } end end end 

I am sure there are extraneous lines, but it works at least. I also renamed the view file from contact.erb to contact.text.erb in addition to having already copied this file to contact.html.erb.

And I call the method from my controller with:

  def send_mail Notifications.deliver_contact(params[:email]) redirect_to "/mail/success" #Notifications.contact(params[:email]).deliver end 
+3
source

Just for reference, I had the same problem, and in my case, the solution was to replace attachments and mail strings. Attach first, then call mail.

WRONG

 def pdf_email(email, subject, pdfname, pdfpath) mail(:to => email, :subject => subject) attachments[pdfname] = File.read(pdfpath) end 

OK

 def pdf_email(email, subject, pdfname, pdfpath) attachments[pdfname] = File.read(pdfpath) mail(:to => email, :subject => subject) end 
+6
source

Yes, I have encountered the same problem recently and after I put something in the body of the mail content, for example:

  mail(:to => reciever, :subject => "Hello, this is a mail from rails!") do |formate| formate.text {render :text => "mail content body "} end.deliver 

attachments begin to display normally.

+2
source

I had the same problem, but changing the order of attachment and mail delivery did not work for me.

What worked for me was adding the .pdf extension to the file name. Although my file name had the pdf extension, the action mailer simply did not read the extension. Adding it again worked fine.

 attachments[file_name + '.pdf'] = File.read(path) 
0
source

Source: https://habr.com/ru/post/1398137/


All Articles