send dynamically generated file as email - ruby-on-rails

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.

Related

How to include a link in your email to your attached csv?

I want to include a link at the bottom of the email that a user can click and the attached csv will automatically be downloaded. Something like this:
Best,
Nick
orders.csv
In my mailer I have this:
def order_summary_sent(params)
attachments.inline["orders.csv"] = { mime_type: "text/csv", content: params[:csv_content] }
mail(subject: "Order summary", to: params["recipient"])
The file is successfully shown as an attachment but what erb code should I write to also include it directly in the email body?
I tried the following:
<%= link_to "orders.csv", attachments["orders.csv"].url %>
However, when I click on the link I get cid:635a746039b8b_213558301d83608a#nick.mail

PDF attachment in email is called 'Noname'

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

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