This is my first quesiton, but what I'm trying to do is send mail with an attachment in rails console, using one or two lines. I dont want to instantiate a class like ..
class Mailer < ActionMailer::Base ... end
I want to try it this way:
m=ActionMailer::Base.mail(:to => "[email protected]", :from => "[email protected]", :subject=>"test from zip", :content_type=>"multipart/mixed")
m.attachments['file.zip']={:mime_type => "application/zip", :data=>File.read("#{Rails.root}/tmp/test.zip")}
m.deliver
This will send an email, but the attachment called noname, which can't be unzipped. It seems that its not parsing the data correctly for the attachment. If I look at the raw email the attachment contents looks something like this:
--
Date: Tue, 06 Mar 2012 06:59:42 -0800
Mime-Version: 1.0
Content-Type: application/zip;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=file.zip
Content-ID: <[email protected]>
UEsDBBQAAAAIAE9iZUBSMYOwkKgZANRakgAQABUAbG9hbl9kZXRhaWxzLmNz
dlVUCQADlh9VT0QfVU9VeAQA6APoA8xdW3PiuLZ+37+Ch6ldZ1dZGUvyNW/c
EwKBQLiENze4gytgZ9tmMplff5YMlgQWmV1tk5qufiAkwV8trcu3bko/8sLa
m/+p9dmLJPXSfaI1oyR4Df21Non28crPvt+MfS/117Uo5C+9VKu/v8fRH4e3
O0HobWte9g68gHdaQfJjHyeHb4/9/+79JPu9XbQPU22y2kTRVuv74dqPa7G/
...
1) is it even possible to send an email with an attachment like this, with out using something like the pony gem
An estimation to why it isn't working
According to SO post Invalid filename in email (ActionMailer) it seems to be ActionMailer wanting to automagically glean information from files, something which is unavailable from the console.
I noted that the following, albeit messy, works (sufficiently for my purposes) from the console:
File.open("magical_elephant_potato.txt", 'w') {|f| f.write("Heyyyy youuu!") }
m=ActionMailer::Base.mail(:to => "[email protected]", :from => "[email protected]", :subject=>"Behold my MEP attache", :content_type=>"multipart/mixed")
m.attachments['magical_elephant_potato.txt']=File.read("magical_elephant_potato.txt")
m.deliver
FileUtils.rm('magical_elephant_potato.txt')
Given that writing and removing files via console works, perhaps the files required by ActionMailer can be written, utilised then deleted? We're heading into sticky work-around territory here though. A problem is that ActionMailer will look for the appropriate mailer view, but how and can we tell ActionMailer where to look for the mailer files? (As in, the filename)
As for the information not being encoded correctly, I think the problem is that its being wrapped in the 'noname' file with some header info. The data is likely intact, as with my example I get:
--
Date: Tue, 08 Jan 2013 11:08:57 +0000
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8;
filename=magical_elephant_potato.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=magical_elephant_potato.txt
Content-ID: <[email protected]>
Heyyyy youuu!
----
:when I open 'noname' with a text editor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With