使用ActionMailer :: Base在一行或两行中使用Rails 3.0中的附件发送邮件

har*_*rya 6 console actionmailer ruby-on-rails-3

这是我的第一个问题,但我要做的是使用一两行在rails控制台中发送带附件的邮件.我不想实例化类...

class Mailer <ActionMailer :: Base ...结束

我想这样试试:

m=ActionMailer::Base.mail(:to => "harry@example.com", :from => "test@example.com", :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
Run Code Online (Sandbox Code Playgroud)

这将发送一封电子邮件,但附件名为noname,无法解压缩.它似乎没有为附件正确解析数据.如果我查看原始电子邮件,附件内容如下所示:

--
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: <4f56264f16e82_498a46e93467093@ip-10-125-15-127.mail>

UEsDBBQAAAAIAE9iZUBSMYOwkKgZANRakgAQABUAbG9hbl9kZXRhaWxzLmNz
dlVUCQADlh9VT0QfVU9VeAQA6APoA8xdW3PiuLZ+37+Ch6ldZ1dZGUvyNW/c
EwKBQLiENze4gytgZ9tmMplff5YMlgQWmV1tk5qufiAkwV8trcu3bko/8sLa
m/+p9dmLJPXSfaI1oyR4Df21Non28crPvt+MfS/117Uo5C+9VKu/v8fRH4e3
O0HobWte9g68gHdaQfJjHyeHb4/9/+79JPu9XbQPU22y2kTRVuv74dqPa7G/
...
Run Code Online (Sandbox Code Playgroud)

1)甚至可以发送带有这样的附件的电子邮件,而不使用像pony gem 这样的东西

xxj*_*jnn 2

估计它不起作用的原因

根据 SO post Invalid filename in email (ActionMailer), ActionMailer 似乎想要从文件中自动收集信息,而控制台无法提供这些信息。

我注意到以下内容虽然很混乱,但可以从控制台运行(足以满足我的目的):

File.open("magical_elephant_potato.txt", 'w') {|f| f.write("Heyyyy youuu!") }
m=ActionMailer::Base.mail(:to => "rainbowpony@company.com", :from => "noreply@railsapp.com", :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')
Run Code Online (Sandbox Code Playgroud)

鉴于可以通过控制台写入和删除文件,也许可以写入、使用然后删除 ActionMailer 所需的文件?不过,我们正在进入棘手的解决方法领域。问题是 ActionMailer 将查找适当的邮件程序视图,但是我们如何以及如何告诉 ActionMailer 在哪里查找邮件程序文件?(如文件名)

至于信息未正确编码,我认为问题在于它被包含在带有一些标头信息的“noname”文件中。数据可能完好无损,就像我得到的示例一样:

--
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: <50bbfe4898ac_6d7febf6a312062@ws9.companydev.com.mail>

Heyyyy youuu!

----
Run Code Online (Sandbox Code Playgroud)

:当我用文本编辑器打开“noname”时。