lib/sendgrid_actionmailer.rb in sendgrid-actionmailer-0.1.0 vs lib/sendgrid_actionmailer.rb in sendgrid-actionmailer-0.1.1
- old
+ new
@@ -15,10 +15,11 @@
c.api_key = params[:api_key]
end
end
def deliver!(mail)
+ attachment_tempfiles = []
from = mail[:from].addrs.first
email = SendGrid::Mail.new do |m|
m.to = mail[:to].addresses
m.cc = mail[:cc].addresses if mail[:cc]
@@ -85,22 +86,28 @@
email.html = mail.html_part.decoded if mail.html_part
email.text = mail.text_part.decoded if mail.text_part
# This needs to be done better
mail.attachments.each do |a|
- begin
- t = Tempfile.new("sendgrid-actionmailer")
- t.binmode
- t.write(a.read)
- t.flush
- email.add_attachment(t, a.filename)
- ensure
- t.close
- t.unlink
- end
+ # Create a tempfile with the same file extension as the real file
+ # for sendgrid-ruby's mime type lookups.
+ t = Tempfile.new(["sendgrid-actionmailer", File.extname(a.filename)])
+ t.binmode
+ t.write(a.read)
+ t.flush
+ t.rewind
+ email.add_attachment(t, a.filename)
+ attachment_tempfiles << t
end
end
client.send(email)
+ ensure
+ # Close and delete the attachment tempfiles after the e-mail has been
+ # sent.
+ attachment_tempfiles.each do |file|
+ file.close
+ file.unlink
+ end
end
end
end