Send mail with an attachment in Rails 3.0 using ActionMailer :: Base in one or two lines

This is my first quesiton, but what I'm trying to do is send mail with the application in the rails console using one or two lines. I do not want to instantiate a class like ..

class Mailer <ActionMailer :: Base ... end

I want to try it like this:

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 

This will send an email, but the attachment is called noname, which cannot be unzipped. It seems that it does not correctly parse the data for the attachment. If I look at the raw email, the contents of the attachment look 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: < 4f56264f16e82_498a46e93467093@ip-10-125-15-127.mail > UEsDBBQAAAAIAE9iZUBSMYOwkKgZANRakgAQABUAbG9hbl9kZXRhaWxzLmNz dlVUCQADlh9VT0QfVU9VeAQA6APoA8xdW3PiuLZ+37+Ch6ldZ1dZGUvyNW/c EwKBQLiENze4gytgZ9tmMplff5YMlgQWmV1tk5qufiAkwV8trcu3bko/8sLa m/+p9dmLJPXSfaI1oyR4Df21Non28crPvt+MfS/117Uo5C+9VKu/v8fRH4e3 O0HobWte9g68gHdaQfJjHyeHb4/9/+79JPu9XbQPU22y2kTRVuv74dqPa7G/ ... 

1) you can even send an email with such an attachment using pony gem

+6
source share
1 answer

Assessing why it is not working

According to SO post Invalid file name in email (ActionMailer) , it seems that ActionMailer wants to automatically extract information from files, which is not available from the console.

I noted that from the console you can use the following, albeit messy, jobs (enough for my purposes):

 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') 

Given that writing and deleting files through the console works, maybe the files needed by ActionMailer can be written, used and deleted? However, we are heading for a sticky workaround. The problem is that ActionMailer will look for a suitable view for the mailer, but how can we tell ActionMailer where to search for mailer files? (As in, file name)

As for the incorrect encoding of the information, I think the problem is that it is wrapped in a "noname" file with some header information. The data is probably not corrupted since in 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: < 50bbfe4898ac_6d7febf6a312062@ws9.companydev.com.mail > Heyyyy youuu! ---- 

: when I open 'noname' with a text editor.

+2
source

Source: https://habr.com/ru/post/910104/


All Articles