5

I need to send simple mail with an attachment. Right now it sends an email but attachment is in the e-mail body like this. enter image description here

--
Content-Type: text/csv;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename=errors_in_address_table.csv
Content-ID: <5b0e7d6b4abff_10c12ac224c8b0d4994d@development.mail>

Current code to send email

saved_file = Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')
mailer = ActionMailer::Base.mail(from: 'no-replay@test.com', to: 'test@test.com', subject: 'Errors in database', body: '', content_type: 'multipart/mixed')
mailer.attachments['errors_in_address_table.csv'] = { mime_type: 'text/csv', content: File.read(saved_file) }
mailer.deliver

I tried to get this to work correctly for hours. Maybe some more experienced coder can help me.

3 Answers3

4

For anyone else stumbling across this, this should work (at least in Rails 6+) from the Rails console:

mailer = ActionMailer::Base.new
mailer.attachments['errors_in_address_table.csv'] = File.read(saved_file)
mailer.mail(from: 'from@test.com', to: 'to@test.com', subject: 'Errors', body: '').deliver
Mark
  • 41
  • 2
0

I have some different bits of code for attachment in my app, I am not sure this could work for you but you may give it a try.

Try to replace your block

saved_file = Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')
mailer = ActionMailer::Base.mail(from: 'no-replay@test.com', to: 'test@test.com', subject: 'Errors in database', body: '', content_type: 'multipart/mixed')
mailer.attachments['errors_in_address_table.csv'] = { mime_type: 'text/csv', content: File.read(saved_file) }
mailer.deliver

with the following which is a bit simpler:

attachments['errors_in_address_table.csv'] = open(Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')).read
mail(from: 'no-replay@test.com', to: 'test@test.com', subject: 'Errors in database', body: '')

This is really a trial and error approach. I will delete if it doesn't work.

Also do you have a view for your mailer ?

Maxence
  • 2,029
  • 4
  • 18
  • 37
-1

As per the description shared, it seems like you need to modify attachments code

saved_file = Rails.root.join('db', 'fix_db', 'errors_in_address_table.csv')
mailer = ActionMailer::Base.mail(from: 'no-replay@test.com', to:    'test@test.com', subject: 'Errors in database', body: '', content_type: 'multipart/mixed')
mailer.attachments['errors_in_address_table.csv'] = saved_file
mailer.deliver
Rohan
  • 2,681
  • 1
  • 12
  • 18
  • 1
    Can you describe what you changed and why? – siegy22 May 30 '18 at 11:58
  • mailer.attachments['errors_in_address_table.csv'] = saved_file #changed this line – Rohan May 30 '18 at 12:03
  • The API does not say anything about this: http://api.rubyonrails.org/v5.0/classes/ActionMailer/Base.html#method-i-attachments – siegy22 May 30 '18 at 12:13
  • As stated in http://api.rubyonrails.org/v5.0/classes/ActionMailer/Base.html#method-i-attachments , mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg') Need to specify the file path in attachments – Rohan May 30 '18 at 12:26
  • yes..I have also written the same things.. mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg') – Rohan May 30 '18 at 13:02