LewlSauce
LewlSauce

Reputation: 5892

ActionMailer sending attachment as the body

I am trying to send a PDF as an attachment to an email that contains some text in the body. However, I cannot seem to accomplish this because the email body includes the attachment. I have an extremely simply mailer within my rails application:

class QuickMailer < ApplicationMailer
    def send_mail(smtp_settings, mail_options, attachment=nil)
        unless attachment.nil?
            attachments[attachment[0]] = File.read(attachment[1])
        end
        mail(from: mail_options[:from],
            to: mail_options[:to],
            subject: mail_options[:subject],
            body: "<!DOCTYPE html><body>#{mail_options[:body]}</body>",
            content_type: "text/html"
            )
    end
end

If I remove this line:

content_type: "text/html"

then it sends the attachment as the attachment, but the body contains plaintext that needs to be HTML. I can't seem to accomplish both.

What am I doing wrong? I've seen others ask this question, but there doesn't seem to be any answers to this. Is this possible with the rails action mailer?

From what I can see you can either add an attachment, but forced to use plaintext in the body, or you can include the attachment within the body, and it will use HTML as the content type

Upvotes: 0

Views: 280

Answers (1)

LewlSauce
LewlSauce

Reputation: 5892

For anyone else that spent over 4 hours trying to figure this out, I just installed sendemail and moved on with my life.

Upvotes: 1

Related Questions