quantumpotato
quantumpotato

Reputation: 9767

Action Mailer attachments.inline showing as normal attachment, not inline

  def simple_mail(to, subject, body)
   attachments.inline['Logo.png'] = File.read( Rails.root.join("public", "Logo.png"))
   mail(:to => to, :subject => subject, :body => body)
 end

I receive an attachment to the message, but it is not an "inline" image. How can I attach inline, and how can I choose where it goes?

I'm seeing examples that have views for their mailer.. looks like I may need to set those up. If so, how do I link Action Mailer views to a message?

Upvotes: 2

Views: 2478

Answers (2)

Crash
Crash

Reputation: 331

As you see in guides.rubyonrails, you can link a view to your mailer the same way you link a view to a controller's action. if your mailer is called my_mailer, and your mailer action is called simple_mail, then create a view in app/views/my_mailer/ and call it "simple_mail.html.erb". As well, you can create a "simple_mail.text.erb" to make sure that if the recipient can't receive HTML e-mails, they get the plaintext version (see action_mailer_basics.html below).

As for inline attachments, a different question was raised previously regarding views (see this link) and scaney answered with a couple links. Check out this link and search the text for "inline" and you should find the answer you're looking for!

http://guides.rubyonrails.org/action_mailer_basics.html

api.rubyonrails.org/classes/ActionMailer/Base.html

Upvotes: 1

Aaditi Jain
Aaditi Jain

Reputation: 7027

1) To Setup action-mailer views for the mails, you can do it as follows:

Create a view file in app/views/mailer_name/ with the same name as your mailer action (which in this case is "simple_mail" )

Alternatively if you want to use a different view altogether you can also do it as :

def simple_mail(to, subject, body)
 attachments.inline['Logo.png']=File.read( Rails.root.join("public", "Logo.png"))
 mail(:to => to, :subject => subject, :body => body) do |format|
      format.html { render :partial => "/path/to/partial"}
   end
end

In the above case, the content of the partial will be rendered as the body of the mail.

2) To attach a inline image you will also need to mention the attachment in the mailer action view file as follows:

<%= image_tag attachments['Logo.png'].url %> 

Upvotes: 0

Related Questions