instance method attachments

Ruby on Rails 7.0.10

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

attachments()

Allows you to add attachments to an email, like so:

mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg')

If you do this, then Mail will take the file name and work out the mime type. It will also set the Content-Type, Content-Disposition, and Content-Transfer-Encoding, and encode the contents of the attachment in Base64.

You can also specify overrides if you want by passing a hash instead of a string:

mail.attachments['filename.jpg'] = {mime_type: 'application/gzip',
                                    content: File.read('/path/to/filename.jpg')}

If you want to use encoding other than Base64 then you will need to pass encoding type along with the pre-encoded content as Mail doesn’t know how to decode the data:

file_content = SpecialEncode(File.read('/path/to/filename.jpg'))
mail.attachments['filename.jpg'] = {mime_type: 'application/gzip',
                                    encoding: 'SpecialEncoding',
                                    content: file_content }

You can also search for specific attachments:

# By Filename
mail.attachments['filename.jpg']   # => Mail::Part object or nil

# or by index
mail.attachments[0]                # => Mail::Part (first attachment)
Source
# File actionmailer/lib/action_mailer/base.rb, line 749
    def attachments
      if @_mail_was_called
        LateAttachmentsProxy.new(@_message.attachments)
      else
        @_message.attachments
      end
    end

Defined in actionmailer/lib/action_mailer/base.rb line 749 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionMailer::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close