HTML Emails

I’ve seen a few users over the years ask about how to create HTML emails and stumbled onto an article about it tonight that I thought might be interesting to someone.

8 Likes

Thanks for this! This is awesome! I have tried a little bit to figure it out, but haven’t made any progress. What’s the best way to send an HTML email through Xojo? Obviously it’s not easy to make the HTML a string for the Xojo code, and I haven’t succeeded in finding a way to pull it from a file and include the images. Any advice?

Use CURL with the MBS plugin or the Chilkat plugin.

2 Likes

In writing my app (an email client) I started with the RFCs which describe the structure of an email, and how you can send a mail that has a text part, an html part, and inline and attached files too.

Here is an incomplete list of the RFCs that may be consulted:

a) RFC 5034 POP3
b) RFC 2821 SMTP
c) RFC 5322 Internet Message Format
d) RFC 2045, 2046, 2047, 2048, 2049 and 2557 (MIME), 2183, and 2231
e) RFC 2392 Content-Id URL

HTH.

With CURLEMailMBS class in MBS Xojo CURL Plugin, you can just pass HTML to the plugin and we build the whole structure around for you.

Firstly: HTML designed Emails will be the first ones to be sorted out. Secondly: Many Clients won’t display them because all included base64 contents for embedded images or even more worse: external references will be stripped out leaving the mails quite broken and usable. Thridly: Many Coorporations store such eMails in their long-time-archieves and create tags or parse them for projects or departments by keywords. HTML Mails are the most ones obvious usual suspects, sorted out straight into “uncategorized” what means, nobody will find anything nor will search for them.

It’s a best practise to use simple, unformated text-mails. Stick to it!

Rather than using fancy, funky styled emails try to improve your mailservers or email infrastructure.
A good starting point is hardenize.com. Feel free to compare my mailserver with something else:

As for me, mailing letters should be primarily interesting to recipients. Nobody likes spam. Layout is just as important. Nobody needs a bare wall of text, but a million images that take a long time to load on the mobile Internet are not very good. So, the point is not even in choosing a service, but in building the right strategy.

I contract for a company with a Xojo console application that automatically sends email to suppliers when their certificates have expired. They used to be sent as plain text and were often (accidentally) ignored by the supplier or treated as spam.

When I updated them to use pretty HTML emails with a linked company logo and formatted text their reply rate went so high they no longer need to chase the supplier for the new certificates.

2 Likes

Depends really. I tend to ignore most html emails. A text one is more likely to have been generated by a real human.

Has anyone figured out a way to send HTML emails using Mail.app via AppleScript? It seems like it used to work years ago but has been broken in macOS.

A demo applescript that doesn’t work for me in 10.14:

set theHTML to "<html><body><h1>Hi there (HTML)</h1><body></html>"
set theBody to "Hi There (text)"
set eSender to "test@example.com"
set eSubject to "Testing HTML"
set eName to "Alice Alison"
set eMail to "test@example.com"

tell application "Mail"
	##Create the message
	set theMessage to make new outgoing message with properties {sender:eSender, subject:eSubject, visible:true}
	
	##Set a recipient
	tell theMessage
		set html content to theHTML
		set content to theBody
		make new to recipient with properties {name:eName, address:eMail}
		##Send the Message
		## send
	end tell
end tell

When run, I get a new mail message, but only the plain text content shows up.

You can use NSSharingServiceItemsMBS with an NSAttributedStringMBS object like this:

// compose email
Dim items As New NSSharingServiceItemsMBS

// styled text from text area control
Dim tv As NSTextViewMBS = TextArea1.NSTextViewMBS
items.AddAttributedString tv.textStorage

// find service
Dim service As New NSSharingServiceMBS("com.apple.share.Mail.compose")

service.subject = "Test Email"
service.setRecipients Array("test@test.test")

service.performWithItems items
1 Like

Thank you, Greg.

I’ve made quite a few of these over the years but it’s always nice to have a more recent writeup.

I personally am not a fan of HTML emails but that’s irrelevant, since it’s the way all emails work by default and the way companies and people expect email to behave (I mean being able to style it, people don’t normally know what “html email” means).

Not having nor using HTML email has become more and more a sign of “something is wrong” for normal users. Explaining potential downsides of HTML email is wasted time and effort when they use it every single day continuously without facing most of them.

It’s a good thing, though, that it has more or less standardized by now (with the caveats and limitations forced by the medium). It was a lot more of the wild west and “styled email” could mean HTML or RTF, and “HTML” didn’t mean the same for all clients either. By now it’s pretty safe that HTML email generated in one place will be readable in another.

I also prefer the older style of quoting and replying but top-replying also has become the norm. We old farts need to just accept it and move on.

This is not true. The emails I receive on the SQLite Users Mailing List, for example, are text only and contain no HTML.

Success! I figured out a way to get it to work, using only Applescript.

The trick is to create a Data URL and open that in Safari, then copy & paste the Safari window back into Mail.app.

Here’s a sample Applescript demo:

tell application "Safari"
	make new document at end of documents
	activate
	delay 1
end tell
tell application "Safari"
	activate
	-- IMPORTANT: HTML in the next line should actually be URL encoded for best results
	set html to "<html><body><h1>This is a test of html</h1><br><a href='https://example.com'>Example.com</a></body></html>"
	open location "data:text/html," & html
	delay 1
	tell application "System Events"
		keystroke tab
		delay 0.1
		keystroke "a" using command down
		delay 0.1
		keystroke "c" using command down
		delay 0.1
	end tell
	-- comment out the next line to leave the Safari widnow open for debugging
	close window 1
end tell
tell application "Mail"
	activate
	set toList to {"Alice <alice@example.com>", "Bob <bob@example.com>"}
	set ccList to {}
	set bccList to {}
	set attachmentList to {}
	-- if using attachment list, set it to a list of full paths to the files:
	-- set attachmentList to {"/Volumes/Macintosh HD/Desktop/File1.docx"}
	set eMail to make new outgoing message with properties {sender:"Charlie <charlie@example.com", subject:"TEST HTML Email", visible:true}
	repeat with toPerson in toList
		tell eMail
			set eName to extract name from toPerson
			set eAddress to extract address from toPerson
			make new to recipient at end of to recipients with properties {name:eName, address:eAddress}
		end tell
	end repeat
	repeat with ccPerson in ccList
		tell eMail
			set eName to extract name from ccPerson
			set eAddress to extract address from ccPerson
			make new cc recipient at end of cc recipients with properties {name:eName, address:eAddress}
		end tell
	end repeat
	repeat with bccPerson in bccList
		tell eMail
			set eName to extract name from bccPerson
			set eAddress to extract address from bccPerson
			make new bcc recipient at end of bcc recipients with properties {name:eName, address:eAddress}
		end tell
	end repeat
	repeat with toFile in attachmentList
		set fNam to (toFile as string)
		set theFile to POSIX file fNam as alias
		tell content of eMail
			make new attachment with properties {file name:theFile as alias} at after last paragraph
		end tell
		delay 1
	end repeat
	-- replace this next line with signature name, or leave it commented out for default signature
	-- NOTE: when using attachments, the signature may upset the HTML format, so you may want to ignore it
	-- set message signature of eMail to signature "XXX" 
	delay 0.5
	tell application "System Events"
		tell process "mail"
			keystroke tab
			delay 0.1
			keystroke tab
			delay 0.1
			keystroke tab
			delay 0.1
			keystroke tab
			delay 0.1
			keystroke "v" using {command down}
		end tell
	end tell
end tell

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.