Here is my mailer:
class MailIt < ActionMailer::Base
def funny(sender)
#sender = sender
attachments['funny.pdf'] = File.read("#{Rails.root}/app/assets/funny.pdf")
mail(:to => "amail#example.com",
:from => "amail#example.com",
:subject => "TESST")
end
end
I get the e-mail with the attached pdf but it is damaged and the file size is only 1/4 of the original file.
if you have images in your pdf, you have to send it as binary.
attachments['funny.pdf'] = File.read("#{Rails.root}/app/assets/funny.pdf", :mode => 'rb')
I did find a solution.
It seems like there is a problem with File.read
I tried this and it worked:
attachments['funny.pdf'] = File.open("#{Rails.root}/app/assets/funny.pdf").read
Related
Here after called the controller.rb, one file(chart.png) will save in my rails app folder, so how to take this and will attach with mail?
controller.rb
def mail
#imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=5&choe=UTF-8"
open(#imageURL) do |chart|
File.open('chart.png', 'wb') {|f| f.write chart.read }
end
UserMailer.welcome_email(#imageURL, #mailID).deliver
end
how can i pass that image into welcome_email method for attaching with the mail? need some help to solve this?
user_mailer.rb
def welcome_email(imageURL, mailID)
mail(:to => mailID,
:subject => "code",
:body => "Code for the branch "+imageURL+"")
end
end
If you want to attach it to the email, you'd have to download the image, and then attach it from the file-system.
Creating attachment is easy :
attachments["filename"] = File.read("/path/to/file")
If I were you, I'd just add the image in an image_tag in the body of the email
Edit : I didn't see you were already writing the file.
So here is the complete solution :
def mail
#imageURL = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=5&choe=UTF-8"
path_image = "/tmp/chart-#{#imageUrl.hash}.png" #Avoid filename collision
open(#imageURL) do |chart|
File.open(path_image, 'wb') {|f| f.write chart.read }
end
UserMailer.welcome_email(#imageURL,#mailID, path_image).deliver
File.delete(path_image)
end
def welcome_email(imageURL,mailID, path_image)
attachments["charts.png"] = File.read(path_image)
mail(:to => mailID,
:subject => "code",
:body => "Code for the branch "+imageURL+"")
end
How to render prawn pdf as attachment in ActionMailer? I use delayed_job and don't understand, how could I render pdf-file in action mailer (not in controller). What format should I use?
You just need to tell Prawn to render the PDF to a string, and then add that as an attachment to the email. See the ActionMailer docs for details on attachments.
Here's an example:
class ReportPdf
def initialize(report)
#report = report
end
def render
doc = Prawn::Document.new
# Draw some stuff...
doc.draw_text #report.title, :at => [100, 100], :size => 32
# Return the PDF, rendered to a string
doc.render
end
end
class MyPdfMailer < ActionMailer::Base
def report(report_id, recipient_email)
report = Report.find(report_id)
report_pdf_view = ReportPdf.new(report)
report_pdf_content = report_pdf_view.render()
attachments['report.pdf'] = {
mime_type: 'application/pdf',
content: report_pdf_content
}
mail(:to => recipient_email, :subject => "Your report is attached")
end
end
I followed the RailsCasts for PRAWN. Taken what has already been said and what I was trying to similarly accomplish, I set the attachment name and then created the PDF.
InvoiceMailer:
def invoice_email(invoice)
#invoice = invoice
#user = #invoice.user
attachments["#{#invoice.id}.pdf"] = InvoicePdf.new(#invoice, view_context).render
mail(:to => #invoice.user.email,
:subject => "Invoice # #{#invoice.id}")
end
My solution:
render_to_string('invoices/show.pdf', :type => :prawn)
PDF was corrupted because I didn't write block for mail function and multi-part email was incorrect.
In rails3 w ActionMailer, I want to send a .txt file attachment. The challenge is this txt file does not exist but rather I want to create the txt file given a large block of text that I have.
Possible? Ideas? Thanks
It's described for files in the API documentation of ActionMailer::Base
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end
But that doesn't have to be a File, it can be a string too. So you could do something like (I'm also using the longer Hash-based form where you can specify your own mimetype too, you can find documentation for this in ActionMailer::Base#attachments):
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['filename.jpg'] = {:mime_type => 'application/mymimetype',
:content => some_string }
mail(:to => recipient, :subject => "New account information")
end
end
First the method to send email
class ApplicationMailer < ActionMailer::Base
def welcome(user, filename, path)
attachments[filename] = File.read(path)
mail(:to => user.email, :subject => "New account information")
end
end
Call the method with the params
UserMailer.welcome(user, filename, path).deliver
My application creates a .pdf file when it is rendered by passing it to the URL (for example, domain.com/letter/2.pdf)
It doesn't get saved anywhere.
How can I make that actual pdf an attachment in an outbound email.
Here is my mailer:
def campaign_email(contact,email)
subject email.subject
recipients contact.email
from 'Me <me#me.com>'
sent_on Date.today
attachment = File.read("http://localhost:3000/contact_letters/#{attachment.id}.pdf")
attachment "application/pdf" do |a|
a.body = attachment
a.filename = "Othersheet.pdf"
end
end
This is the controller that creates/renders the PDF:
def create
#contact_letter = ContactLetter.new(params[:contact_letter])
#contact = Contact.find_by_id(#contact_letter.contact_id)
#letter = Letter.find_by_id(#contact_letter.letter_id)
if #contact_letter.save
flash[:notice] = "Successfully created contact letter."
#redirect_to contact_path(#contact_letter.contact_id)
redirect_to contact_letter_path(#contact_letter, :format => 'pdf')
else
render :action => 'new'
end
end
NOTE: I hardcoded localhost:3000/ how can I substitute that with a variable so that on dev it is localhost:3000 and on production is it the correct domain? Is there a way to include routing in this?)
ERROR: I get an
Invalid argument -
http://localhost:3000/contact_letters/9.pdf
Here's an example for rails 2
class ApplicationMailer < ActionMailer::Base
# attachments
def signup_notification(recipient, letter)
recipients recipient.email_address_with_name
subject "New account information"
from "system#example.com"
attachment :content_type => "image/jpeg",
:body => File.read("an-image.jpg")
attachment "application/pdf" do |a|
a.body = letter
end
end
end
in your view or wherever your calling your method:
ApplicationMailer.deliver_signup_notification(letter)
one quick an easy solution would be fetch the url content using net/http and open-uri, to get the attachment
attachments['free_book.pdf'] = open("http://#{request.host}/letter/#{id}.pdf")
eg:
def campaign_email(contact,email)
subject email.subject
recipients contact.email
attachments['free_book.pdf'] = open("http://#{request.host}/letter/#{id}.pdf")
from 'Me <me#me.com>'
sent_on Date.today
body :email => email
end
or, call the PDF generation inside your mailer controller action
I got it to work by passing the pdf object directly into the campaign_email method and then assigning an attachment.
I am using the following code to send an email with a pdf attachment:
class StudyMailer < ActionMailer::Base
def notify_office(study, sent_at = Time.now)
subject "Email Subject Goes Here"
recipients 'user#domain.come'
from "#{study.sender.full_name} <#{study.sender.email}>"
sent_on sent_at
body :study => study
for document in study.documents
attachment :content_type => "application/pdf", :body => File.read(document.document.path) #absolute path to .pdf document
end
end
end
When the email is sent, the attachment seems to render inline as binary code rather than as a .pdf attachment.
How do I render the .pdf as a typical attachment, rather than inline?
attachment :content_type => "application/pdf",
:content_disposition => "attachment",
:filename => File.basename(fattach),
:body => File.new(fattach,'rb').read()
Notice the content-disposition line.
I believe you have to indicate the multipart nature of the email, so add this line under the from line:
content_type "multipart/alternative"
Does your email have a template? If the email does not have a template, the attachment shows up inline even if everything else is set up correctly. Create an attachment email template.
views/notifier/attachment.html.erb
<p> Please see attachment </p>
Then in Notifier, specify to use this template.
notifier.rb
def my_email_method
...
mail(:template_name => 'attachment', :from => from_address, ...)
end