<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harrison Broadbent</title>
    <description>The latest articles on DEV Community by Harrison Broadbent (@harrisonbroadbent).</description>
    <link>https://dev.to/harrisonbroadbent</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F177698%2Ff8d788d1-3a31-402a-91e5-7b143bc1b8b3.jpeg</url>
      <title>DEV Community: Harrison Broadbent</title>
      <link>https://dev.to/harrisonbroadbent</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harrisonbroadbent"/>
    <language>en</language>
    <item>
      <title>ActionMailer attachments in Ruby on Rails</title>
      <dc:creator>Harrison Broadbent</dc:creator>
      <pubDate>Sat, 28 Oct 2023 03:25:16 +0000</pubDate>
      <link>https://dev.to/harrisonbroadbent/actionmailer-attachments-in-ruby-on-rails-1hf1</link>
      <guid>https://dev.to/harrisonbroadbent/actionmailer-attachments-in-ruby-on-rails-1hf1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;ActionMailer makes it easy to attach files to your emails. In this article, I show you how to attach single or multiple files, set custom encodings and &lt;code&gt;mime_types&lt;/code&gt;, and attach images as inline attachments to display in your email body.&lt;/p&gt;

&lt;p&gt;This post was originally published on the [RailsNotes UI Blo&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you use ActionMailer to send a lot of emails, at some point, you'll probably want to attach files to them. You might want to send a &lt;code&gt;.pdf&lt;/code&gt; invoice to a customer, or attach custom images inline inside a newsletter email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fortunately, ActionMailer makes attaching files really easy!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this article, I'll show you how to send emails with attachments using ActionMailer.&lt;/strong&gt; We'll cover a few examples, including —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;attaching a &lt;code&gt;.pdf&lt;/code&gt; file to an email,&lt;/li&gt;
&lt;li&gt;attaching multiple files,&lt;/li&gt;
&lt;li&gt;adding inline attachments for things like images,&lt;/li&gt;
&lt;li&gt;and specifying custom encodings for your attachments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic File Attachments
&lt;/h2&gt;

&lt;p&gt;ActionMailer makes it easy to attach files. Each mailer method has access to an &lt;code&gt;attachments&lt;/code&gt; hash, which stores attachments and automatically includes them in your emails.&lt;/p&gt;

&lt;p&gt;You assign attachments directly to &lt;code&gt;attachments&lt;/code&gt;, specifying the file name and content.&lt;/p&gt;

&lt;p&gt;To attach a single file (for example, a PDF invoice file) we can write something like this —&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/mailers/invoice_mailer.rb&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InvoiceMailer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionMailer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_invoice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# attach a file to our email&lt;/span&gt;
    &lt;span class="c1"&gt;# should reference a binary blob, like from File.read&lt;/span&gt;
    &lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'invoice.pdf'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'path/to/invoice.pdf'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;subject: &lt;/span&gt;&lt;span class="s1"&gt;'Your Invoice'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, we read a PDF file from the disk and assign it to the &lt;code&gt;attachments&lt;/code&gt; hash. When we send our email, ActionMailer will attach the file, and call it &lt;code&gt;invoice.pdf&lt;/code&gt; (the name we specified).&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;#mail&lt;/code&gt; method inside &lt;code&gt;InvoiceMailer#send_invoice&lt;/code&gt; is triggered, a multipart email with an attachment is sent. ActionMailer will automatically guess the &lt;code&gt;mime_type&lt;/code&gt; for the file and set the &lt;code&gt;encoding&lt;/code&gt;, plus handle attaching the file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For more info, you can read the &lt;a href="https://guides.rubyonrails.org/action_mailer_basics.html#complete-list-of-action-mailer-methods"&gt;official Ruby on Rails/ActionMailer docs&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Attaching multiple files
&lt;/h2&gt;

&lt;p&gt;ActionMailer also supports attaching &lt;em&gt;multiple&lt;/em&gt; files to your emails, in a very similar way to the above. We just need to assign multiple values to the &lt;code&gt;attachments&lt;/code&gt; hash, and then ActionMailer will automatically attach them —&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/mailers/invoice_mailer.rb&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InvoiceMailer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionMailer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_invoice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'invoice.pdf'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'path/to/invoice.pdf'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'receipt.pdf'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'path/to/receipt.pdf'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'refund-policy.pdf'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'path/to/refund-policy.pdf'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;subject: &lt;/span&gt;&lt;span class="s1"&gt;'Your Invoice'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no limit to the number of files you can attach, but you will need to keep in mind &lt;a href="https://www.google.com/search?q=email+attachment+limit"&gt;email attachment size limits&lt;/a&gt;. For Gmail, there's a 25MB total file size limit across all attachments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting custom encodings for attachments
&lt;/h2&gt;

&lt;p&gt;By default, ActionMailer will &lt;code&gt;Base64&lt;/code&gt; encode your files. If the default encoding doesn't suit your needs though, you can encode your content differently. Pass the encoded content and encoding to the &lt;code&gt;attachments&lt;/code&gt; hash, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;encoded_content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SpecialEncode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/path/to/filename.jpg'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'filename.jpg'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;mime_type: &lt;/span&gt;&lt;span class="s1"&gt;'application/gzip'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;encoding: &lt;/span&gt;&lt;span class="s1"&gt;'SpecialEncoding'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;content: &lt;/span&gt;&lt;span class="n"&gt;encoded_content&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By specifying an encoding, ActionMailer will assume that your content is already encoded and won't attempt to Base64 encode it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inline Attachments
&lt;/h2&gt;

&lt;p&gt;Rails and ActionMailer also support &lt;em&gt;inline attachments&lt;/em&gt; — inline attachments are files to be displayed in the body of your email, typically images or videos.&lt;/p&gt;

&lt;p&gt;To create an inline attachment, call &lt;code&gt;#inline&lt;/code&gt; on the &lt;code&gt;attachments&lt;/code&gt; hash within your mailer —&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# welcome_mailer.rb&lt;/span&gt;
&lt;span class="c1"&gt;#&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;welcome&lt;/span&gt;
  &lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inline&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'image.jpg'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/path/to/image.jpg'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in your mailer view, reference &lt;code&gt;attachments&lt;/code&gt; as a hash, specify the attachment you want to show, and pass the result to the &lt;code&gt;image_tag&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;# welcome.html.erb
#
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello there, this is our image&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;image_tag&lt;/span&gt; &lt;span class="n"&gt;attachments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'image.jpg'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;alt: &lt;/span&gt;&lt;span class="s1"&gt;'My Photo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;class: &lt;/span&gt;&lt;span class="s1"&gt;'photos'&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;ActionMailer in Ruby on Rails makes it easy to send emails with attachments, whether it's attaching single or multiple files or inline attachments.&lt;/p&gt;

&lt;p&gt;You can also check out the &lt;a href="https://guides.rubyonrails.org/action_mailer_basics.html#complete-list-of-action-mailer-methods"&gt;official ActionMailer docs&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;If you use ActionMailer a lot to send emails in your Ruby on Rails apps, you'll also probably love &lt;a href="https://railsnotesui.xyz"&gt;RailsNotes UI, a collection of email templates and components for ActionMailer.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
