PDF attachment in email is called 'Noname' - ruby-on-rails

When I send an email with a attached pdf file the email only shows a file called 'Noname'. The file itself is the multipart section of the email with the base64 attached pdf. How can I send the PDF so it comes up as an attachment and doesn't corrupt the email?
Here is my code:
attachments['126539_statistics.pdf'] = File.read("app/assets/pdfs/126539_statistics.pdf")
mail(:to => email, :subject => subject, :body => message, :content_type => 'application/pdf')

I had face same problem. I have corrected it by following way
1) As per ActionMailer Guide, You need to make view file into app/views/[action_mailer_model_name]/[method_name]
Here is reference of guide: http://api.rubyonrails.org/classes/ActionMailer/Base.html
2) Action Mailer is smart enough that it identify content-type automatically while reading file. So there is no need to pass explicitly 'application/pdf' in your mail function.
Hope, this information helps to solve your problem

Also experienced the same problem
solution is :
set Attachments before sending mail function.
code is attached below:
attachments["invoice_#{invoice.bill_date.strftime('%B-%Y')}.pdf"] = WickedPdf.new.pdf_from_string(
render_to_string pdf: 'project_report.pdf',
template: 'users/_invoice.html.erb'
)
mail(to: email , from: from, subject: subject) do |format|
format.html
end

Related

send dynamically generated file as email

I am currently allowing users to select certain parameters and based on those, I generate a csv file and push it as a download to the users. e.g.
send_data <generated csv data>, :disposition => 'attachment' :type => 'text/csv'
Sometimes as the data becomes too large to compute, I do not want to make the users wait for the file to be pushed as download. I want to send this file as an attachment in an email.
I can send an email normally. I can send an already present file as an attachment. I do not want to save this file. I want to email it directly to the user.
How do I do that?
#juanpastas - I did it the way you suggested. But that caused the file to be in displayed as a text in the email body.
This is how it appeared in the email.
Content-Type: text/csv;
charset=UTF-8;
filename=data.csv
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=data.csv
Content-ID: xyzxyz
[content of the csv file as text]
Then I included the message body and it worked.
mail(to: user.email, subject: 'XYZ', body: 'XYZ')
This caused the email to have the body and subject I provided and the file appeared as an attachment instead.
I have not tested this, but this should work:
class YourMailer < ActionMailer::Base
def csv_mail(user, csv_data)
attachments['a.csv'] = csv_data
mail(to: user.email)
end
end
And in your controller:
YourMailer.csv_mail(user, csv_data).deliver
See attachments and inline attachments.

Action Mailer attachments.inline showing as normal attachment, not inline

def simple_mail(to, subject, body)
attachments.inline['Logo.png'] = File.read( Rails.root.join("public", "Logo.png"))
mail(:to => to, :subject => subject, :body => body)
end
I receive an attachment to the message, but it is not an "inline" image. How can I attach inline, and how can I choose where it goes?
I'm seeing examples that have views for their mailer.. looks like I may need to set those up. If so, how do I link Action Mailer views to a message?
As you see in guides.rubyonrails, you can link a view to your mailer the same way you link a view to a controller's action. if your mailer is called my_mailer, and your mailer action is called simple_mail, then create a view in app/views/my_mailer/ and call it "simple_mail.html.erb". As well, you can create a "simple_mail.text.erb" to make sure that if the recipient can't receive HTML e-mails, they get the plaintext version (see action_mailer_basics.html below).
As for inline attachments, a different question was raised previously regarding views (see this link) and scaney answered with a couple links. Check out this link and search the text for "inline" and you should find the answer you're looking for!
http://guides.rubyonrails.org/action_mailer_basics.html
api.rubyonrails.org/classes/ActionMailer/Base.html
1) To Setup action-mailer views for the mails, you can do it as follows:
Create a view file in app/views/mailer_name/ with the same name as your mailer action (which in this case is "simple_mail" )
Alternatively if you want to use a different view altogether you can also do it as :
def simple_mail(to, subject, body)
attachments.inline['Logo.png']=File.read( Rails.root.join("public", "Logo.png"))
mail(:to => to, :subject => subject, :body => body) do |format|
format.html { render :partial => "/path/to/partial"}
end
end
In the above case, the content of the partial will be rendered as the body of the mail.
2) To attach a inline image you will also need to mention the attachment in the mailer action view file as follows:
<%= image_tag attachments['Logo.png'].url %>

Sending an html attachment with ActionMailer 3

I am trying to send an email which contains a single HTML attachment. The problem is that the attachment html is appearing in the body of the email, and the view is showing up as the attachment. Pretty much the opposite of what I expected.
I am able to send attachments of other types properly, but when trying to send a single attachment that is HTML, it consistently is displayed rather than 'attached'.
class Notifier < ActionMailer::Base
default :from => "from#example.com"
def welcome(email)
attachments['this is an html file.html'] = "<b>yeah this is html!</b>"
mail(:to => email, :subject => "Attempting an attachment")
end
end
And my app/views/notifier/welcome.html.erb
Hi there! This is <b>html</b> within a view
The resulting email looks like this:
(notice the attachment html is actually displayed in the body of the email)
This is a feature of many email clients that if they can display attachments inline they will, so the user doesn't have to open a separate program just to see what's inside. To be honest though it's more meant for images.
If you want to send an html file, you're going to need to add it to a zip or other archive and send that instead.

Rails - how to Create a Mail object ,Mail.new

using rails 3, I want to learn how to create a ActionMailer Mail object and can't much docs online. I have the following work:
message_all = Mail.new(:from => 'frooooom', :to => 'tooooo', :cc => 'cccccccc', :subject => 'SUBJECTasddsadsadasdsa')
Question is now, how do I add a HTML and TEXT Body to that? Bonus points for also showing an example of how to add attachements.
Thank you!
Have you tried the mail gem's documentation? This example is mostly pulled from there:
message_all = Mail.new do
to 'nicolas#test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel#test.lindsaar.net.au>'
subject 'First multipart email sent with Mail'
text_part do
body 'This is plain text'
end
html_part do
content_type 'text/html; charset=UTF-8'
body '<h1>This is HTML</h1>'
end
end
I believe you can still pass the headers in a hash as you were doing before, but the text_part/html_part business should go in the block.
Info on attachments is in that link I provided above.
Google '"rails 3" email templates" yields this:
http://edgeguides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails
Check out section 2.7 on sending multi-part emails.

Prawn PDF with Rails mailer?

I have successfully created an email that sends on creation of a Kase, but now I need to attach a PDF that is created on the fly by Prawn and Prawno. Basically when you visit a kase such as application.com/kase/1 you just append the URL with .pdf i.e. application.com/kase/1.
I spent ages getting the PDF to work and look how I wanted, but I can't figure out how to add the PDF to an auto sending email - mainly because I cannot work out how to give it a link as it's auto generated.
Has anyone ever managed to get this to work?
Thanks,
Danny
I suppose it would be better if you store generated pdf somewhere - for caching purposes, etc.
But with current configuration, you can read generated page with Net::HTTP and attach response:
require 'net/http'
def your_mailer_method(record)
#...
attachment "application/pdf" do |a|
a.body = Net::HTTP.get('yourdomain.com', "/kase/#{record.id}.pdf")
a.filename="your_pdf_name.pdf"
end
end
You really should consider just not using Prawnto, and creating a subclass of Prawn::Document to do what you need. Then, in both your controller and your mailer code, it should just be:
MyReport.new.render
See the Prawn documentation on this:
http://wiki.github.com/sandal/prawn/using-prawn-in-rails
For the newer ones, you dont really need to send a request again, when you can ::
mail.attachments["invoice.pdf"] = {:mime_type => "application/pdf" , :content => pdf_generator}
Instead of doing this ::
send_data pdf.render , :filename => file_name_here , :type => "application/pdf"
just do this ::
pdf.render , :filename => file_name_here , :type => "application/pdf"
Do not send_data, just render that pdf in your email attachment as mentioned in the first snippet.
In fact, i just wrote a Gist on github.
This code works for me
def send_file(file, subject, text, to_email)
#subject = subject
#text = text
attachments["#{invoice.invoice_number}.pdf"] = file
from_email = abc#xyz.com
mail(:to => to_email, :from => from_email, :subject=> subject)
end

Resources