1

I'm sending an email with one or more attachments, I create the email using Mail gem, here's the code

mail = Mail.new
mail.to = to
mail.subject = subject
mail.body = email_body
mail.content_type = 'text/html'
# attaching a temp file on the rails server
mail.add_file(params["file"].tempfile.path) # path e.g "/tmp/RackMultipart-some-name-text.png"
message_to_send = Google::Apis::GmailV1::Message.new(raw: mail.to_s)
response = @gmail_service.send_user_message("me", message_to_send)

but instead of sending the attached file, I get the email as a raw text with the attachment file coming as Base64 encoded string in the body of the email. Here is an example of what the received email looks like when I send attachment/s.
The mail gem documentation for attaching files doesn't say anything in particular about making any other changes except for just adding file to the mail object. Any idea about what is going on here?

Masroor
  • 1,484
  • 3
  • 14
  • 23

1 Answers1

1

I think that in your case, the email body and attachment file are required to be sent as multipart/alternative. So when "Mail gem" is used, how about the following modification?

Modified script:

mail = Mail.new
mail.to = to
mail.subject = subject

# I added below script.
mail.part content_type: 'multipart/alternative' do |part|
  part.html_part = Mail::Part.new(body: email_body, content_type: 'text/html')
  part.text_part = Mail::Part.new(body: email_body)
end

mail.add_file(params["file"].tempfile.path)
message_to_send = Google::Apis::GmailV1::Message.new(raw: mail.to_s)
response = @gmail_service.send_user_message("me", message_to_send)

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Tanaike thanks for your reply. Doing this I could still not send the attachment, however there was no encoded text in the email after I did this. So, I received the subject and email `multipart/alternative` stuff only and there was no attachment in any form. – Masroor Oct 22 '20 at 14:31
  • [here is the response](https://0bin.net/paste/++V0rtVA#LArnktR4LwD-xetN/WO2TzG6timiHHW8h4emPheR8n+) that I get for the sent message with 2 image attachments. – Masroor Oct 22 '20 at 15:11
  • @Prime Thank you for replying. I apologize for the inconvenience. Unfortunately, I cannot replicate your situation. Because in my environment, I can confirm that the script worked. I apologize for this. So in order to correctly understand about your current situation, can you provide your script for replicating your issue? By this, I would like to confirm it. By the way, I have tested in the modified script in my answer, because of your script. If you can cooperate to resolve your issue, I'm glad. – Tanaike Oct 22 '20 at 22:25
  • Please check my script and data [here](https://0bin.net/paste/-r-Bs1x5#Yvg3YuwK78RkrIfd8BBg50IPS-igOya4WJRRhQ60FQu). Thanks for your consideration. – Masroor Oct 23 '20 at 07:42
  • 1
    @Prime Thank you for replying. From your initial question, my sample script supposes the file is `mail.add_file(params["file"].tempfile.path) # path e.g "/tmp/RackMultipart-some-name-text.png"`. I deeply apologize my proposal script cannot be used for all of your situations. This is due to my poor skill. I would like to study more. So can you test the script using the path like `/tmp/RackMultipart-some-name-text.png`? Because in my environment, when the paths are used, no error occurs and the files can be attached to email. By the way, in your script, can you remove `mail.body = email_body`? – Tanaike Oct 23 '20 at 08:09
  • no the script you've provided is accurate. It was my blunder for that `mail.body = email_body` part. This was the main culprit. I removed that line and it worked perfectly. Thank you very much! – Masroor Oct 23 '20 at 08:24
  • 1
    @Prime Thank you for replying and testing it again. I'm glad your issue was resolved. Thank you, too. – Tanaike Oct 23 '20 at 08:24