title: Ruby on Rails: Why doesn't my embedded image display in Gmail or Office 365?
body: <p>I'm trying to embed images within HTML emails so that they display inline, but for some reason the image does show in Outlook software but not in Gmail (online) or Office 365 (online). I have been referring to <a href="http://api.rubyonrails.org/classes/ActionMailer/Base.html" rel="nofollow noreferrer">http://api.rubyonrails.org/classes/ActionMailer/Base.html</a> as well as the SO question <a href="https://stackoverflow.com/questions/4918414/what-is-the-right-way-to-embed-image-into-email-using-rails">What is the right way to embed image into email using Rails?</a></p>
  
  <p>And my code is the following:</p>
  
  <p>environments/development.rb and production.rb:</p>
  
  <pre><code>config.action_mailer.asset_host = "http://localhost:3000"
  </code></pre>
  
  <p>Email view:</p>
  
  <pre><code>&lt;%= image_tag('logo.jpg') %&gt;
  </code></pre>
  
  <p>This displays the image in Outlook:</p>
  
  <p><a href="https://i.stack.imgur.com/fr2Xo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fr2Xo.png" alt="enter image description here"></a></p>
  
  <p>But not in Gmail or Office 365:</p>
  
  <p><a href="https://i.stack.imgur.com/LjfPc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LjfPc.png" alt="enter image description here"></a></p>
  
  <p>In the log I get the following <code>img src</code> for this:</p>
  
  <p><code>&lt;img src="http://localhost:3000/assets/logo-9b88158fa35240340cc52aa5d01800b12e6e1de5ddb99e246bd0583c7a5611cd.jpg" alt="Logo" /&gt;</code></p>
  
  <p>I have tried a variety of approaches from different people, which include the SO questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/36499884/unable-to-render-inline-attachments-in-actionmailer">Unable to render inline attachments in ActionMailer</a>
  <a href="https://stackoverflow.com/questions/8356924/rails-attachments-inline-are-not-shown-correctly-in-gmail">Rails attachments inline are not shown correctly in gmail</a>
  <a href="https://stackoverflow.com/questions/3971939/send-html-emails-with-inline-images-with-actionmailer">Send HTML emails with inline images with ActionMailer</a></p>
  
  <p>But none of them seem to solve my issue.</p>
  
  <p>The code I have tried is:</p>
  
  <p>(Attempt 1)</p>
  
  <p>Mailer:</p>
  
  <pre><code>def send_email(email, unsubscribe)
      @url  = 'http://localhost:3000/users/login'
      @email = email
      @unsubscribe = unsubscribe
  
      attachments.inline['logo.jpg'] = File.read('C:/Sites/MySite/app/assets/images/logo.jpg')
      mail(from: "#{@email.from_name} &lt;#{@email.account.email}&gt;", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
    end
  </code></pre>
  
  <p>Email View:</p>
  
  <pre><code>&lt;%= image_tag attachments['logo.jpg'].url -%&gt;
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>&lt;img src="cid:57cd4b6a997ae_25703efc6dc786d@My-Laptop.mail" /&gt;
  
  ----==_mimepart_57cd4b6a9af1e_25703efc6dc78787
  Content-Type: image/jpeg;
   filename=logo.jpg
  Content-Transfer-Encoding: base64
  Content-Disposition: inline;
   filename=logo.jpg
  Content-ID: &lt;57cd4b6a997ae_25703efc6dc786d@My-Laptop.mail&gt;
  
  QzovU2l0ZXMvRWFzeU1haWwvYXBwL2Fzc2V0cy9pbWFnZXMvbG9nby5qcGc=
  
  ----==_mimepart_57cd4b6a9af1e_25703efc6dc78787--
  </code></pre>
  
  <p>(Attempt 2)</p>
  
  <p>Mailer:</p>
  
  <pre><code>def send_email(email, unsubscribe)
      @url  = 'http://localhost:3000/users/login'
      @email = email
      @unsubscribe = unsubscribe
  
      # THE BELOW LINE
      attachments.inline['logo.jpg'] = File.join(Rails.root, 'app/assets/images/logo.jpg')
      mail(from: "#{@email.from_name} &lt;#{@email.account.email}&gt;", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
    end
  </code></pre>
  
  <p>Email View:</p>
  
  <pre><code>&lt;%= image_tag attachments['logo.jpg'].url -%&gt;
  </code></pre>
  
  <p>(Attempt 3)</p>
  
  <p>Mailer:</p>
  
  <pre><code>def send_email(email, unsubscribe)
      @url  = 'http://localhost:3000/users/login'
      @email = email
      @unsubscribe = unsubscribe
      attachments.inline['logo.jpg'] = {
                                  :data =&gt; File.read('C:/Sites/MySite/app/assets/images/logo.jpg'),
                                  :mime_type =&gt; "image/jpg",
                                  :encoding =&gt; "base64"
      }
      mail(from: "#{@email.from_name} &lt;#{@email.account.email}&gt;", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
    end
  </code></pre>
  
  <p>Email View:</p>
  
  <pre><code>&lt;%= image_tag attachments['logo.jpg'].url -%&gt;
  </code></pre>
  
  <p>This gives me a UTF error</p>
  
  <p>(Attempt 4)</p>
  
  <p>Mailer:</p>
  
  <pre><code>  def send_email(email, unsubscribe)
      @url  = 'http://localhost:3000/users/login'
      @email = email
      @unsubscribe = unsubscribe
      attachments.inline["logo.jpg"] = {
          mime_type: "image/jpg",
          content: Base64.encode64(File.read(Rails.root.join("app/assets/images/logo.jpg")))
      }
      mail(from: "#{@email.from_name} &lt;#{@email.account.email}&gt;", to: @email.to, cc: @email.cc, bcc: @email.bcc, subject: @email.subject, message: @email.message)
    end
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;%= image_tag "data:image/jpg;base64,#{attachments['logo.jpg'].url}" %&gt;
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>&lt;img src="data:image/jpg;base64,cid:57cd4c32e96d0_25704cedd5878982@My-Laptop.mail" /&gt;
  
  ----==_mimepart_57cd4c32ea670_25704cedd5879035
  Content-Type: image/jpg
  Content-Transfer-Encoding: base64
  Content-Disposition: inline;
   filename=logo.jpg
  Content-ID: &lt;57cd4c32e96d0_25704cedd5878982@My-Laptop.mail&gt;
  
  aVZCT1J3bz0K
  
  ----==_mimepart_57cd4c32ea670_25704cedd5879035--
  </code></pre>
  
  <p>But none of these seem to work, its also worth adding that only the code at the top shows the image in Outlook, all the others stop me from seeing the image in Outlook, Gmail, and Office365.</p>
  
  <p>Can someone please help me with this issue as I can't make sense of it.</p>
  
  <p>I am using Rails 4.2.6</p>