Emails With Embedded Images Using Realbasic

Recently on the Real Software NUG somebody asked how to embed images (not image links) directly in HTML emails using Realbasic. I thought this would be easy at first and set out to provide an answer. Like many things in programming, it took a little longer than expected. But I did finally arrive at a solution and I’m offering it to the community as open source. This new class, EmailMessageTD (download) is handy for any situation where you need to customize an email message in a manner not directly supported by the Realbasic classes before sending it via SMTPSocket.

My search for an answer brought me first to this blog post. This technique is simple to implement in Realbasic. All you need to do is write a few lines of code like this:

Dim s As String = imgTest.GetData(Picture.FormatJPEG)
s = "data:image/jpeg;base64," + EncodeBase64(s)
mail.BodyHTML = "<img src=""" + s + """>"

One problem is that this technique only works with a few email clients. So I read their follow up post. This looked like something that would be easy to do as well. Unfortunately I ran into a little problem: the Realbasic EmailMessage and EmailAttachment classes do not support the attachment Content-ID header.

First I tried to get, modify, and set EmailMessage.Source, but this doesn’t work. EmailMessage parses Source, fills its properties, and generates Source again when you access it later. So the Content-ID header I was adding was lost.

Then on a hunch I tried overriding EmailMessage.Source and discovered that it is not implemented as a property but as two methods. And the solution became rather simple.

EmailMessageTD overrides the EmailMessage Source methods and allows you to customize the raw message source before sending your email. It includes another method, SetContentID, which allows you to add a Content-ID header to an attachment. If the attachment is an image you can embed the image in your BodyHTML using a line like this: <img src="cid:MyImageAttachmentId">

This class is useful for anyone who needs to modify an email message directly before sending it via SMTPSocket and avoids the tremendous hassle of having to implement full replacements for SMTPSocket and EmailMessage just to send a customized message. This was discussed on the list and it sounds like a few people have taken this rather tedious route.

I’ve tested EmailMessageTD by sending emails with embedded images using Real Studio 2011r3 and 2011r4.3. The emails I sent were correct in Mail.app and Gmail.

3 comments

  1. Tobias Bußmann

    Nice wrap-up and workaround! I’d like to point you to some Feedback cases and another workaround to set custom Header fields currently not supported: use a kind of string injection like att.Name = f.name + chr(34) + “; Content-ID=” + chr(34) + “MyImageAttachmentId”

    Setting Attachment header:
    Getting Attachment header:

    Problem with re-parsing (wrongly) the Source on access (even _read_ access!) and not beeing able to read original source (incomming mail) or set custom source (outgoing mail):

    have not yet tested in detail, but your workaround should be affected by the latter MIME-scrambling bug as well 🙁

    • Tobias Bußmann

      …links seem to be eaten by wordpress…

      Setting Attachment header: feedback://showreport?report_id=7119
      Getting Attachment header: feedback://showreport?report_id=11103

      Problem with re-parsing Source: feedback://showreport?report_id=11318

    • I’ve been unable to reproduce the feedback report bug 11318 (change on read access). What is the MIME-scrambling bug?

      I should note that even if my work around is affected by a bug, you can clean up the bug in the custom source before sending the message. Once you’ve gone into “custom” mode, you can do what you want with the source and it will not be reinterpreted or changed by the parent class. But changes in your custom source will also not reflect in the properties of the parent class.

Trackbacks/Pingbacks

  1. Envoyer un e-mail avec des images intégrées depuis RealStudio | Blog sur RealStudio - [...] Cliquez ici pour retrouver l’article orignal. [...]

Leave a Reply

Your email address will not be published. Required fields are marked *