1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Updating ActionMailer basics guide to have inline attachments

This commit is contained in:
Mikel Lindsaar 2010-06-07 23:25:49 -04:00
parent 3762362eab
commit 80a044edb6
2 changed files with 32 additions and 0 deletions

View file

@ -565,6 +565,7 @@ Action Mailer has been given a new API with TMail being replaced out with the ne
* All mailers are now in <tt>app/mailers</tt> by default.
* Can now send email using new API with three methods: +attachments+, +headers+ and +mail+.
* ActionMailer now has native support for inline attachments using the <tt>attachments.inline</tt> method.
* Action Mailer emailing methods now return <tt>Mail::Message</tt> objects, which can then be sent the +deliver+ message to send itself.
* All delivery methods are now abstracted out to the Mail gem.
* The mail delivery method can accept a hash of all valid mail header fields with their value pair.

View file

@ -211,6 +211,37 @@ attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
NOTE: If you specify an encoding, Mail will assume that your content is already encoded and not try to Base64 encode it.
h5. Making Inline Attachments
Inline attachments are now possible in ActionMailer. While previously in the pre 3.0 version of Rails, you could do inline attachments, it involved a lot of hacking and determination to pull it off.
ActionMailer now makes inline attachments as trivial as they should be.
* Firstly, to tell Mail to turn an attachment into an inline attachment, you just call <tt>#inline</tt> on the attachments method within your Mailer:
<ruby>
def welcome
attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
end
</ruby>
* Then in your view, you can just reference <tt>attachments[]</tt> as a hash and specify which attachment you want to show, calling +url+ on it and then passing the result into the <tt>image_tag</tt> method:
<erb>
<p>Hello there, this is our image</p>
<%= image_tag attachments['image.jpg'].url %>
</erb>
* As this is a standard call to +image_tag+ you can pass in an options hash after the attachment url as you could for any other image:
<erb>
<p>Hello there, this is our image</p>
<%= image_tag attachments['image.jpg'].url, :alt => 'My Photo',
:class => 'photos' %>
</erb>
h4. Mailer Views
Mailer views are located in the +app/views/name_of_mailer_class+ directory. The specific mailer view is known to the class because it's name is the same as the mailer method. So for example, in our example from above, our mailer view for the +welcome_email+ method will be in +app/views/user_mailer/welcome_email.html.erb+ for the HTML version and +welcome_email.text.erb+ for the plain text version.