Send attachment file in outgoing mail body in rails - ruby-on-rails

I am trying to send S3 url file in mail body, but I am getting error
Errno::ENOENT: No such file or directory # rb_sysopen
I want something like this, but I am unable to achieve this one
#path = s3_url
attachments["output.pdf"] = {
mime_type: "application/pdf",
content: HTTParty.get(#path).response.try(:body)
}
mail(to: 'xyz#gmail.com', subject: "Test Attchment", body: File.read(URI.parse(#path)))

Fetch the PDF using Net::HTTP or HTTPary and read the response to set the attachment in the mailer.
Try it this way:
# change the file name if required
mail.attachments["output.pdf"] = {
mime_type: "application/pdf",
content: HTTParty.get(s3_path).response.try(:body)
}

Related

Attach active storage file to mailer

I have tried everything and still can't get this to work.
I have two types of application in my system that are pre-qualified and sent to lenders,
1) one generates a pdf
2) second should use active storage attachments and attach them to an ActionMailer
First one is working the second is giving me the following error:
[ActionMailer::DeliveryJob] [905177a5-b0e9-46f4-ba9a-fc4630e873f9]
Error performing ActionMailer::DeliveryJob (Job ID:
905177a5-b0e9-46f4-ba9a-fc4630e873f9) from Async(mailers) in 140.14ms:
Errno::ENOENT (No such file or directory # rb_sysopen -
https://funderhunt.co/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZ1lIIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--fa91a15681c23d47d767169c7821601aa15ed2b3/Statuses.pages?disposition=attachment):
The link is correct tho:
My mailer code for this part looks like this:
q = 0
statement.files.each do |file|
q += 1
bank_statement = File.read(rails_blob_url(file, disposition: "attachment"))
attachments["statement_#{q}.pdf"] = { :mime_type => 'application/pdf', :content => bank_statement }
end
What is wrong? Can you please help. Thanks in advance.
If somebody lands here looking for a general solution:
modelname.attachments.each do |file|
attachments[file.blob.filename.to_s] = {
mime_type: file.blob.content_type,
content: file.blob.download
}
end
You should be able to do something like,
statement.files.each_with_index do |file, q|
attachments["statement_#{q + 1}.pdf"] = { mime_type: 'application/pdf', content: file.blob.download }
end
file.blob.download will return the content of the file, similar to File.read.

Attach mail message to mail in Rails

In a Rails 5.1 ActionMailer, I want to attach a Mail object to an e-mail:
def attach_mail(original_email)
attachments['original-email.eml'] = { mime_type: 'message/rfc822', encoding: '7bit', content: original_email.to_s }
mail to: 'postmaster', subject: 'mail should be attached'
end
However, this does not produce valid e-mails. Thunderbird lists the attachment with size '0'. Horde lists the attachment with correct size, but does not recognize it as an e-mail.
I've tried variations of the attachments line:
attachments['original-email.eml'] = original_email
attachments['original-email.eml'] = { content: original_email.to_s }
attachments['original-email.eml'] = { mime_type: 'message/rfc822', content: original_email.to_s }
but none of these result in an e-mail with an e-mail attachment.
What's the solution?
Finally figured it out.
To attach an email (Mail object from the 'mail' gem) to an ActionMailer message, you need to specify the MIME type and encoding like so:
def attach_mail(original_email)
attachments['original-email.eml'] = { mime_type: 'message/rfc822',
encoding: '7bit',
content: original_email.to_s }
mail to: 'postmaster', subject: 'mail should be attached'
end
This creates a multipart/mixed message which is properly displayed in MUAs.
However, if you happen to add any inline attachment (e.g. to display a logo image in the ActionMailer e-mail body), the entire message will have a multipart/related mime type. The MUAs that I tried were unable to interpret a multipart/related message with an e-mail attachment.
Therefore, refrain from adding any inline attachments when attaching an e-mail to an e-mail.

Rails saving a pdf to Amazon S3

I have a Rails 3.2 app that uses gem 'wicked_pdf', and gem 'combine_pdf'.
They both work and I can create PDFs which get emailed.
But, I have run into a situation where the email would be too big.
So, I'm trying to save the created pdf to Amazon S3. The app already has the gem 'aws-sdk'.
This is my code:
def self.saveallpdf
#costprojects = Costproject.where("client_id = 2")
pdf = CombinePDF.new
#costprojects.each do |costproject|
#costproject = costproject
controller = CostprojectsController.new
controller.instance_variable_set(:"#costproject", #costproject)
pdf2 = controller.render_to_string(pdf: "Captital Projects.pdf",
template: "costprojects/viewproject",
encoding: "UTF-8")
pdf << CombinePDF.parse(pdf2)
end
#s3 = AWS::S3.new
#bucket = #s3.buckets['ndeavor3-pdf']
#obj = #bucket.objects['filename'].write(pdf, acl: :public_read)
end
The error I'm getting is:
:data must be provided as a String, Pathname, File, or an object that responds to #read and #eof?
/app/vendor/bundle/ruby/1.9.1/gems/aws-sdk-1.8.3.1/lib/aws/s3/data_options.rb:125:in `validate_data!'
/app/vendor/bundle/ruby/1.9.1/gems/aws-sdk-1.8.3.1/lib/aws/s3/data_options.rb:32:in `compute_write_options'
/app/vendor/bundle/ruby/1.9.1/gems/aws-sdk-1.8.3.1/lib/aws/s3/s3_object.rb:594:in `write'
/app/app/models/costproject.rb:167:in `saveallpdf
'
I guess was-sdk doesn't like the "pdf" as the file??
PS - I can email the "pdf" - if it was smaller in size.
Thanks for your help!
At this point in time:
#obj = #bucket.objects['filename'].write(pdf, acl: :public_read)
pdf is a CombinePDF object and not a File, String, or Pathname
pdf.to_s might work, or you will have to create a new file from the CombinePDF object
File.new(CombinePDF) # pseduo code only

What content_type to use - Getting a No Boundary error on Mandrill-api gem (Rails 4.1)

Emails without attachment are delivered fine, but when there is an attachment I get boundary missing error on mandril:
{
"code": -98,
"name": "ValidationError",
"message": "Invalid raw_message: Bad multipart message - no boundary"
}
Letter opener in dev model shows the email rendering perfectly
The Email is html (rendered via Mandrill without a problem) and will have a PDF attachment.
the call to mailer is:
# template variables
merge_vars = {"AMOUNT" => "100"}
invoice = Invoice.new(customer)
attachments[invoice.name] = {
data: invoice.invoice_data,
mime_type: 'application/pdf'
}
content_type = "multipart/mixed"
mail(to: user.email, cc: cc_emails.compact.uniq, subject: mail_subject, content_type: content_type) do |format|
# Template renders fine
format.html{mandrill_template('invoice_template', merge_vars)}
end
Mandril gem version 1.0.53 and Rails 4.1.10.

How to extract an uploaded file from a body that is StringIO

I'm trying out Typhoeus for the first time to upload a file to a Rails app, but I don't know how to turn that file into something I can use.
The upload is done like Typheous' example:
Typhoeus.put(
url,
body: {
title: "This should be the title",
file: File.open(file_path, "r")
}
)
In the controller, request.body.string is this:
"title=This%20should%20be%20the%20title&file=%5B%221-1381398552.zip%22%2C%20%22application%2Fzip%22%2C%20%22%2Fvagrant%2Fppc_reports%2Fspec%2Fdummy%2Ftmp%2F1381398547_qyforj%2F1-1381398552.zip%22%5D"
How can I get the file from the body and save it as a File or Tempfile to work with?
have you tried this? I think StringIO is supposed to behave like a file.
contents = params[:file].read
After that you'd just save it to another file.
File.open('/path/to/file', 'rw') do |f|
f.write contents
end
PUT doesn't default to application/x-www-form-urlencoded (in contrast to POST), you have to set it:
Typhoeus.put(
url,
body: {
title: "This should be the title",
file: File.open(file_path, "r")
},
headers: { "Content-Type" => "application/x-www-form-urlencoded" }
)

Resources