Is it possible to embed images into an email with Cache?

306 views
Skip to first unread message

Stabbald

unread,
Jun 12, 2014, 1:34:35 PM6/12/14
to intersystems...@googlegroups.com
Sending a plain text email with Cache is pretty straight forward but the documentation seems to be silent on the issue of embedding an image. Does anyone know how I would go about doing this?

Dmitry Maslennikov

unread,
Jun 12, 2014, 1:46:01 PM6/12/14
to intersystems...@googlegroups.com
It is not a problem if you will send your mail in html format. And in this way so easy to put images in email.
But for images better to use, Data URI 


2014-06-12 21:34 GMT+04:00 Stabbald <stab...@gmail.com>:
Sending a plain text email with Cache is pretty straight forward but the documentation seems to be silent on the issue of embedding an image. Does anyone know how I would go about doing this?

--
--
Caché, Ensemble, DeepSee

---
You received this message because you are subscribed to the Google Groups "Caché, Ensemble, DeepSee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to intersystems-publi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Dmitry Maslennikov
Lead Programmer at LETOGRAF
Maintainer of NBStudio
Skype: DAiMor

Bert Sarens

unread,
Jun 13, 2014, 4:04:54 AM6/13/14
to intersystems...@googlegroups.com
MailMessage = ##class(%Net.MailMessage).%New()
s IsBinary = 1
objStream=##class(%FileBinaryStream).%New()
objStream.Filename = File
MailMessage.AttachStream(objStream,key,IsBinary

Preferably I will also put the body into html so i can link to the message As DAiMor pointed out

MailMessage.IsHTML = 1

Fabian Haupt

unread,
Jun 13, 2014, 11:27:38 AM6/13/14
to intersystems...@googlegroups.com

Hi Stabbald,

fancy email like the one you are describing are actually just a feature of mail clients. What you actually send is still just a text based message. However, the mime-standard (rfc 2045,2046,2047, extensions to 822) allows you to send a multipart emails. The mailclient then choses which of the parts to display. Nowadays email clients tend to prefer the html part as display option:

Content-Type: text/html; charset="us-ascii”

You can see this for example when looking at the sourcecode of your first email.
There are two parts in there, the first one being a plain ascii rendering of your email, the second one being a html document.

The debate about the pros/cons of this behaviour is big and often results in heated discussions. Many view the overloading of email with doubling the content as burden on the infrastructure and perversion of rfc822. Others feel there is a benefit of being able to format emails in more detail and include images (as apparently your customer does).
Depending on the settings of email clients, this sometimes even goes as far as not send a non-html version of an email at all. Matthew’s email is an example for that (have a look at the sourcecode).
The problem with those emails is, any client which either can’t render html or is not configured to do so, will display html sourcecode. This is of course less than comfortably readable.

Another consideration is security. HTML allows embedding of all sorts of content that you usually don’t want to have executed/accessed when it comes from unknown sources (Javascript, gif, flash…). That’s why email clients is a reasonable configuration will not display any possibly dangerous content at all (in turn rendering any attempts to have a ‘nice’ email with graphics moot altogether).

All that being said, the code to do that is quite simple. You just need to put the properly formatted body into your email. That doesn’t have anything to do with Caché functionality:


S SmtpServer = ""
S SmtpUserName = ""
S SmtpPassword = ""

S imgPath="C:\test.jpg"

set s=##class(%Net.SMTP).%New()
set s.smtpserver=SmtpServer
#; set auth=##class(%Net.Authenticator).%New() ; use default authentication list
#; set auth.UserName=SmtpUserName
#; set auth.Password=SmtpPassword
#; set s.authenticator=auth
Set objMail=##class(%Net.MailMessage).%New()
Set objMail.From="sen...@testhost.com"
Do objMail.To.Insert("reci...@testhost.com")
Set objMail.Subject="Test-Email"
Set objMail.Charset="iso-8859-1"

Set obj1 =objMail

Set obj1.IsHTML=1
Set obj1.IsBinary = 0
Set obj1.IsMultiPart = 1
Set obj1.MultiPartType ="related"
Do obj1.Headers.SetAt("inline","Content-Disposition")

//alternative container for the text-parts
#dim textbody as %Net.MailMessagePart
s textbody = obj1.AttachNewMessage()
s textbody.IsMultiPart=1
s textbody.IsHTML=0
s textbody.MultiPartType="alternative"

//html part
#dim text as %Net.MailMessagePart
//text part
#dim texttxt as %Net.MailMessagePart

s texttxt = textbody.AttachNewMessage()
//s texttxt.ContentType="text/plain"
d texttxt.TextData.Write("this is plain text")




s text = textbody.AttachNewMessage()
s text.IsHTML=1
s text.IsBinary=0
s text.IsMultiPart=0

Do text.TextData.Write("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">")
Do text.TextData.Write("<html><head><meta http-equiv=""content-type"" content=""text/html; charset=ISO-8859-1"">")
Do text.TextData.Write("</head><body text=""#000000"" bgcolor=""#ffffff"">")
Do text.TextData.Write("Das ist ein Test in html")
Do text.TextData.Write("<img src=""cid:test.jpg"" />")
Do text.TextData.Write("</body></html>")

// Image Message Part
#dim obj2 as %Net.MailMessagePart
Set obj2 = obj1.AttachNewMessage()
Set obj2.IsBinary = 1
Set obj2.IsMultiPart = 0
Set obj2.FileName="test.jpg"

Do obj2.BinaryData.LinkToFile(imgPath)
Do obj2.Headers.SetAt("inline","Content-Disposition")
Do obj2.Headers.SetAt("test.jpg","Content-ID")
set status=s.Send(objMail)

d $system.OBJ.DisplayError(status)
w status,!

Please let me know if you have any questions on this!

Best,
Fabian
signature.asc
Reply all
Reply to author
Forward
0 new messages