Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# 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!

Thanks a million!

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