Rails 5.0.1 - ActionMailer with MailGun, setting 'noname' for filename when directly attaching base64 string - ruby-on-rails

I have read the entire official guide here and about 5 questions on SO that describe this problem for Rails 3 and Rails 4.
This question is for Rails 5, but he is using an actual file, not an encoded string.
This is my mailer code (and yes I have both views setup). The email comes through perfectly fine, with the attached PDF.
def pdf_quote(proposal)
#proposal = proposal
email_with_name = %("#{#proposal.first_name} #{#proposal.last_name}" <#{#proposal.email}>)
filename = "QD-#{#proposal.qd_number}_#{#proposal.last_name}.pdf"
attachments[filename] = {
mime_type: 'application/pdf',
encoding: 'Base64',
content: #proposal.pdf_base64
}
mail(
to: email_with_name,
from: 'Floorbook UK <email address>',
subject: 'Your Personal Flooring Quote is attached',
sent_on: Time.now
)
end
In gmail the attachment is called 'noname' and in Postbox it is called 'pb_mime_attachment.pdf'
How can I get ActionMailer to use the filename I provide?
Note that I am using MailGun (mailgun-ruby gem 1.1.4) to send the email, so it could be the gem at fault here.

It turned out to be a bug with 3rd party mailgun-ruby gem.
I raised a bug with them, here are the details:
https://github.com/mailgun/mailgun-ruby/issues/82
It is fixed (I tested it) in version 1.1.5.

Related

Metadata appears blank in postmark response to rails

I have successfully setup the postmark API in my rails dev environment and am trying to embed metadata containing record ID so I can process the record in my webhook. I receive a response from postmark into my webhook URL upon email delivery, but the application log shows the metadata blank. I have tried entering the metadata in hash notation as well but to no avail:
Processing by WebhooksController#create as HTML
Parameters: {"MessageID"=>"b0c2673e-9628-4725-b9bb-xxxxxxxxxxxx", "Recipient"=>"nnnn#eee.ddd", "DeliveredAt"=>"2022-02-13T08:46:30Z",
"Details"=>"smtp;250 OK id=1nJAWV-001Lmg-Eu", "Tag"=>"", "ServerID"=>nnnnnnnnnn, "Metadata"=>{}, "RecordType"=>"Delivery",
"MessageStream"=>"outbound", "token"=>"xxxxxxxxxxxxx", "webhook"=>{"MessageID"=>"b0c2673e-9628-4725-b9bb-xxxxxxxxxxxx",
"Recipient"=>"nnnn#eee.ddd", "DeliveredAt"=>"2022-02-13T08:46:30Z", "Details"=>"smtp;250 OK id=1nJAWV-001Lmg-Eu", "Tag"=>"",
"ServerID"=>nnnnnnnnnn, "Metadata"=>{}, "RecordType"=>"Delivery", "MessageStream"=>"outbound"}}
My mail block is as follows:
mail(
from: #invoice.account.company_email,
to: #invoice.email,
cc: #invoice.also_email,
subject: "Invoice %s has been generated." % [#invoice.to_s],
metadata: { "invoice_id": "bd2533a8-c00b-4830-97dc-xxxxxxxxxx" },
message_stream: 'outbound',
body: email_body
) do |format|
format.html {email_body}
end
As can be seen, I'm presently hard coding the id into the metadata while I try to solve this issue.
Interestingly, when I use the Postmark test stream, and view the raw data of the email, the metadata is showing (excerpt only):
metadata: {:invoice_id=>"bd2533a8-c00b-4830-97dc-xxxxxxxxxx"}
Feedback-ID: snnnnnn-_:snnnnnn:a22nnnn:postmark
X-Complaints-To: abuse#postmarkapp.com
X-Job: 227735_860nnnn
In addition to the metadata issue, I am receiving only delivery responses. I have open tracking enabled at the server level but aren't receiving any.
Any help would be greatly appreciated and thanks in advance. Oh, and I'm using the postmark-rails gem in rails in conjunction with actionmailer.
Issue has been resolved. The metadata should be inserted outside the mail block as an array:
https://github.com/wildbit/postmark-rails/wiki/Email-sending

Rails mailer and sendgrid with heroku

I am working on adding email functionality to an app I am hosting on heroku.
Here are the relavant files:
app/mailers/user_actions_mailer.rb
def add_user_action(action_params)
#action_params = action_params
from = Email.new(email: 'marklocklear#blah.org')
subject = 'You have been notified'
to = Email.new(email: 'my_user#gmail.com')
content = Content.new(type: 'text/html', value: "test")
mail = Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
end
This is working fine and I am able to send emails with it. However, my issue is with the 'content' variable above. I want the content to use rails standard mailer template in app/views/user_actions_mailer/add_user_action.html.erb.
I have this file/folder created, but I'm not sure how to point content to this location. I'm tryed not passing content to Mail.new fuction hoping this might trigger rails default mailer wiring, that doesn't seem to work.
I was not able to come up with a solution for this, using the sendgrid-ruby gem which my example above uses.
I ended up refactoring using THIS PAGE from sendgrids ruby-on-rails specific documentation.

PDF corrupted on Outlook with AVG anti-virus software installed

we are having the problem of sending PDF attachment using ActionMailer.
The PDF file was combined from 2 PDF files using combine_pdf gem.
A normal PDF file with our logo.
A docx converted PDF using http://convertapi.com
The combined PDF file is stored on AWS S3. What we did is just attach it using open-uri
require 'open-uri'
class ApplicationMailer < ActionMailer::Base
def employer_notification application_id
#application = JobApplication.find_by_id application_id
return unless #application
attachments[#application.cv_name] = {
mime_type: 'application/pdf',
content: open(#application.cv_url(900)).read
}
mail(
from: "#{#application.fullname}<test#myapp.com>",
to: #application.job.employer_emails(", "),
reply_to: #application.email,
subject: "Application for #{#application.job.title}"
)
end
end
We used Mandrill to send out the emails over the internet and something strangely happened. Some PDF could be viewed properly, some contained font errors and the converted PDF part went blank.
But if we turned off AVG anti-virus, it worked perfectly.
Does anyone have the same problem before? May I know how a PDF file is considered malicious?
Thanks in advance.

Rails not sending (ical) attachments in emails

I'm trying to send an email with an ical attachment, I'm running rails v.3.2.13 and using the icalendar gem (to generate the ical string) see. (In development mode in case that might be a problem).
The relevant mailer code looks like this:
def mailme
ical = Icalendar::Calendar.new
...
attachments["meetings.ics"] = { mime_type: "text/calendar", content: ical.to_ical }
mail(from: email, to: recipient, ...)
end
there is also template file with the same name (mailme.html.erb)
The problem is the mail (html) is send without the attachment.
As usual any help would be much appreciated.
Thank you.
I've gotten them working with something like below:
mail.attachments['meeting.ics'] = { mime_type: 'application/ics',
content: ical.to_ical }
mail(from: email, to: recipient, ...)
So it's possible you need to call it on #attachments on the mail object instead of calling it on the current context. I'm not sure if your mime type needs to be application/ics, but that's worked fine for me in my systems.
In case someone else stumbles upon this.
If you are using delayed_job check out https://github.com/collectiveidea/delayed_job/wiki/Common-problems#wiki-Sending_emails_with_attachments
To fix this, remember to add this line to your mailer:
content_type "multipart/mixed"

Uploading a file to Facebook using Koala on Ruby on Rails

I followed the following blogpost to figure out how to create Facebook events remotely using my app. I've been having problems loading the images from my app, however, because I do not have images stored locally on my app, they are stored in AWS.
#graph = Koala::Facebook::GraphAPI.new(#token)
picture = Koala::UploadableIO.new(#event.photo.url(:small))
params = {
:picture => picture,
:name => 'Event name',
:description => 'Event descriptio
:start_time => datetime,
}
is the following code I am currently using to send pictures to Facebook when Facebook events are created on my app. The problem is, however, that Rails is throwing the error: No such file or directory - http://s3.amazonaws.com/ColumbiaEventsApp/photos/21/small.jpeg?1312521889.
Does anybody who's more experienced with Rails development know if there is a way for me to treat a URL like a path to a file? The UploadableIO class expects a path to a file, and I'm struggling to figure out if there's a way in Ruby to treat URL's like filepaths. The way that photos stored on the app can be loaded to Facebook is as follows:
picture = Koala::UploadableIO.new(File.open("PATH TO YOUR EVENT IMAGE"))
if that helps.
I appreciate any new insights into this issue.
Ok so I played around and figured out how to post pictures.
Basically what I did was use the 'open-uri' library to convert the image links into file objects, which can then be passed to UploadableIO and sent to Facebook. This is the code that worked:
require 'open-uri'
OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
OpenURI::Buffer.const_set 'StringMax', 0
picture = Koala::UploadableIO.new(open(#event.photo.url(:small)).path, 'image')
params = {
picture: picture,
name: #event.name,
description: #event.description,
location: #event.location,
start_time: datetime
}
#graph.put_object('me', 'events', params )
The OpenURI constant StringMax needed to be changed because the image files I was using were small enough that the files were being processed as Strings rather than File Objects.
Hope this helps anyone trying to fix this!
With Koala 1.2.1 it's a very elegant solution. Here is sample code for creating an album and uploading to it from a remote, AWS link (btw this took about 30 lines in PHP w/ the PHP SDK!
#foo = Foo.find(params[:foo_id])
albuminfo = #graph.put_object('me','albums', :name=>#foo.title)
album_id = albuminfo["id"]
#graph.put_picture(#foo.remote_image_path,{}, album_id)
Facebook recently released an update that lets you post pictures using publicly accessible URLs (http://developers.facebook.com/blog/post/526/). The Koala library you're using supports that (https://github.com/arsduo/koala/blob/master/lib/koala/graph_api.rb#L102), so you should be able to post the pictures you're hosting on S3 without having to use OpenURI::Buffer.
For Facebook Ad Images, you unfortunately currently cannot do it by URL, thus:
require 'open-uri'
img_data = open(my_post.image.url :medium).read
img = graph.put_connections('act_X', 'adimages', bytes: Base64.encode64(img_data))

Resources