Skip to content

Instantly share code, notes, and snippets.

@steve500002
Created February 1, 2014 10:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve500002/bfc4b027d93c0c04f126 to your computer and use it in GitHub Desktop.
Save steve500002/bfc4b027d93c0c04f126 to your computer and use it in GitHub Desktop.
# iOS devices do not display html emails with attachments as you would expect / like. This code will send emails that iOS devices will correctly display
# iOS devices do not display html emails with attachments as you would expect / like
# This code will send emails that iOS devices will correctly display
# Written by Steve Brett based on code by Chris Parsons
# Worked for me with Ruby 2 and mail 2.5.4
require 'rubygems'
require 'mail'
Mail.defaults do
# Set up mail delivery
delivery_method :smtp,
#address: 'mailserver',
#port: 999,
#user_name: 'xxxxxx',
#password: 'xxxxx',
#domain: 'domain.com',
#authentication: :plain
end
mail=Mail.new do |mail|
to 'reipient@example.com'
from 'Fred Flintstone <fred@flintstones.com>'
subject 'Multipart email with attachment'
end
# Remember to end the plain text with a few blank lines. iOS devices often display the content of the attachments so you want to force them to go below the text
# Create the text and html as separate mail parts
text_body = Mail::Part.new do
body "This is the message\n\n"
content_type 'text/plain; '
end
html_body = Mail::Part.new do
body "<h1>This is HTML</h1>"
content_type 'text/html; charset=UTF-8'
end
# Create a mail part to hold the html and plain text
bodypart = Mail::Part.new
bodypart.text_part = text_body
bodypart.html_part = html_body
# Add the html and plain text to the email
mail.add_part bodypart
# Add the attachment(s)
attachment = "../path/to/doc.pdf"
mail.attachments["#{File.basename(attachment)}"] = File.read(attachment)
# Send the email
mail.deliver!
@micahlisonbee
Copy link

Thanks a million!

@zaidan
Copy link

zaidan commented Apr 27, 2023

Thanks, this solution is still required today! This gist should be an example in the documentation as this is the only proper solution that supports Apple clients!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment