Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer - Create new mail with attachments from original RFC822 message

I use an email delivery service to send emails (Sparkpost), and everytime one of these emails is replied, I receive a JSON that contains: the reply mail body as HTML (body_html), the reply mail body as text (body_text) and the original RFC822 (email_rfc822) for the reply message.

After receiving this JSON, I need to forward this email to another recipient. Currently, I use the following mailer to achieve that:

class ReplyMailer < ApplicationMailer
  def reply(body_html, body_text, options = {})
    mail(to: options[:to], from: options[:from], reply_to: options[:reply_to], subject: options[:subject], skip_premailer: true) do |format|
      format.html { render html: body_html.html_safe } if body_html.present?
      format.text { render plain: body_text } if body_text.present?
    end
  end
end 

The problem with this approach is that it does not forward the attachments of the original message.

How could change this mailer to also forward all the attachments from the original message (including inline images that are referenced on the html body)?

like image 331
felipeecst Avatar asked Nov 07 '22 05:11

felipeecst


1 Answers

The following snippet might help you

options[:attachments].each do |attachment| 
   attachments[attachment.original_filename] = File.read(attachment.tempfile)
end
like image 110
Abdul Wahed Avatar answered Nov 15 '22 06:11

Abdul Wahed