Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send HTML emails with inline images with ActionMailer

I have a simple ActionMailer class like this:

class MyMailer < ActionMailer::Base
  def mail(from, to, cc, bcc, subject, message, sent_at = Time.now)
    @subject = subject
    @recipients = to
    @from = from
    @cc = cc
    @bcc = bcc
    @sent_on = sent_at
    @body["message"] = message
    @headers = {}
  end
end

And I ue it like this from a controller:

MyMailer.deliver_mail(mail.from, mail.to, mail.cc, mail.bcc, mail.subject, mail.message)

I prefer to keep it simple with no templates or such, and it is a webservice with no views.

How do I change it to be able to send HTML mails with embedded images (in img tags i mean)? I need to attach the images woth the correct mime-type and also set the body with the correct mime-type, but how?

Thank you

like image 467
Neigaard Avatar asked Mar 08 '26 22:03

Neigaard


2 Answers

If you are using rails3, it is dead easy, as explained in section 2.3.3 "Making Inline Attachments" in this ruby on rails guide

like image 78
Don Law Avatar answered Mar 11 '26 13:03

Don Law


have you seen: http://edgeguides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments for rails3

and:

http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments

for rails2

like image 29
scaney Avatar answered Mar 11 '26 12:03

scaney