Add Inline Attachments

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
DreamWorlD
New user
New user
Posts: 18
Joined: 2011-10-10 00:27
Location: Sofia, Bulgaria
Contact:

Add Inline Attachments

Post by DreamWorlD » 2019-04-17 21:44

Good day,

Well, it is possible to add an attachment by using:
oMessage.Attachments.Add("Drive:\Path\Filename.ext")

But how to instruct the logic that this should be inline attachment?

I've the following scenario:

A mail is received via SMTP, then on a certain criteria, a HTML text with links should be added at the end of the Body.
If I use direct Body modifications like this:

Code: Select all

oMessage.HTMLbody = htmlText & oMessage.HTMLbody
the result is not nice.
Basically we'll have tags before <html> declaration. And this is a problem. If I exchange the places:

Code: Select all

oMessage.HTMLbody = oMessage.HTMLbody  & htmlText
Then we've tags after </html> declaration.

However, If we put the htmlText in another MIME part, then everything is okay... except that it'll not be visible on all e-mail clients. Namely the ones that do not display html attached files inline. So we need to instruct hMail to add inline attachment.

Could we modify MIME header somehow?

Another one. Is it possible to insert directly the htmlText as an attachment or in separate MIME container, instead of saving it as a file, and them reattaching it?

DreamWorlD
New user
New user
Posts: 18
Joined: 2011-10-10 00:27
Location: Sofia, Bulgaria
Contact:

Re: Add Inline Attachments

Post by DreamWorlD » 2019-04-18 11:32

Okay, so a brief answer to my questions based on through research:
Could we modify MIME header somehow?
No, not possible with the hMail API.

However it is possible to use oMessage.Filename , which will return the path and file name containing the eml data.
It is possible to open the eml file and edit it right away.

However this is possible scenario when the email is saved into the database. I am not sure what is happening with it during the transport, 'cause was unable to find a proper diagram with the mail Path during transport (like the one qmail has for example).
So we need to instruct hMail to add inline attachment.
What I saw after a bit of experiments (the file path name etc is for illustration purposes):

Using the oMessage.attachments.add("c:\attach.html")

Generates this

Code: Select all

--__=-_RandomBoundaryLine
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name="attached.html"
Content-Disposition: inline; filename="attached.html"

<ContentEncodedAsBase64>
--__=-_RandomBoundaryLine--
What I do not like here is the base64 encoding, and the content-type.
However written so it is universal. Mind the Content-Disposition: inline;
If the e-mail client could read it, it'll display it inline, otherwise will show it as an attachment. Which Content-Disposition: attachment; will do as well.

Since base64 encoding of text adds another 30% to the size of the attachment, I am strongly against this, especially having in mind that the attached data is html/text.

So here's a small scenario what a vbs should do (do not know how to write it, 'cause my vbs knowledge is very limited):

Code: Select all

' add empty file to the message, lenght only 1 byte containing a HEX Character for Space 0x20
oMessage.attachments.add("c:\attach.html")
'Save the message
oMessage.save
'open the message as text via oMessage.filename
WScript.[i]someMethod_orObject[/i](oMessage.filename)

' Find <Content-Type: application/octet-stream; name="attached.html">

' Change it to <Content-Type: text/html; charset="Windows-1251" name="attached.html">

' Goto one line up and change <Content-Transfer-Encoding: base64> to <Content-Transfer-Encoding:>

' find <IA==> and replace it with <htmlText> variable build before.

oMessage.save
Could I get some help in codding this scenario?

Thanks

DreamWorlD
New user
New user
Posts: 18
Joined: 2011-10-10 00:27
Location: Sofia, Bulgaria
Contact:

Re: Add Inline Attachments

Post by DreamWorlD » 2019-04-18 22:24

Okay,

Here's what I assembled from scattered codes around the web(please, do not judge me. I know it is very ugly).
Mind that attached.html should be single byte size file containing only 0x20 (or the Space character)

Code: Select all

oMessage.Attachments.Add("c:\attached.html")
oMessage.save

Set fso=CreateObject("Scripting.FileSystemObject")
Set ts=fso.OpenTextFile(oMessage.Filename,1)

predefinedText = "This is the HTML text to be attached<br>Might be replaced with your HTML tags, etc."
buf=ts.ReadAll: ts.Close
arr=Split(buf,vbCrLf)

Set ts=fso.OpenTextFile(oMessage.Filename,2)
For i=0 To UBound(arr)-1  
  If (arr(i) = "Content-Transfer-Encoding: base64" ) And (arr(i+1) = "Content-Type: application/octet-stream; name=""attached.html""" ) Then
     arr(i) = "Content-Transfer-Encoding:"
  End If
  If (arr(i) = "Content-Type: application/octet-stream; name=""attached.html""" ) Then
     arr(i) = "Content-Type: text/html; charset=""Windows-1251"" name=""attached.html"""
  End If
  If (arr(i) = "IA==" ) Then
     arr(i) = predefinedText
  End If
  ts.WriteLine arr(i)
Next 'i
ts.Close

Post Reply