Page 1 of 1

Convert message with text attachment

Posted: 2019-10-01 02:14
by mikedibella
I need to convert email received from several SMS gateways into more convenient format. I know I can select these messages for processing using a rule, and handle the processing using a script, but I am soliciting advice for the most efficient way to process the conversion.

Reviewing the headers of these messages, the text of the SMS appears as plain text immediately after the blank line delimiting the message body from the headers.

The header section include these lines:

Code: Select all

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-ID: <0>
Content-Disposition: attachment;
In Outlook, these messages appear with an empty body and a single text file attachment named text_0.txt. The message text is contained in the text file.

The hMailServer API doesn't provide a way to access an attachment's contents directly, so I'd have to save the attachment to a temporary file, read the contents, append that text to the Message.Body, and delete the temporary file. Seems like an inelegant way to handle the conversion.

I'd prefer to read the attachment text directly from the message content but I don't see a method or property that gives me access all sections of the message as string data without reading the message file from disk.

Any other ideas?

Re: Convert message with text attachment

Posted: 2019-10-01 04:45
by mikedibella
This is what I came up with.

Code: Select all

Sub AppendAttachment(oMessage)
	' save attachment to temporary file
	set oFileSys = CreateObject("Scripting.FileSystemObject")
	sTempFile = oFileSys.GetSpecialFolder(2) & oFileSys.GetTempName
	oMessage.Attachments.Item(0).SaveAs sTempFile
	' read attachment to string
	set fAttachment = oFileSys.OpenTextFile(sTempFile, 1)
	sText = fAttachment.ReadAll
	oFileSys.DeleteFile sTempFile
	' append attachment to message body
	oMessage.Body = oMessage.Body & sText
	oMessage.Subject = "SMS Message"
End Sub

Re: Convert message with text attachment

Posted: 2019-10-01 05:15
by mattg
check if there is a HTMLbody component...

I've seen some messages where there is only HTML part not a plain text part
Otherwise I'd dump the attachment then read it, and change bodytext as you suggest

Re: Convert message with text attachment

Posted: 2019-10-01 13:10
by palinka
Can you post an example of one of these messages? Maybe for no other reason than to satisfy my curiosity.

Re: Convert message with text attachment

Posted: 2019-10-01 17:48
by mikedibella
Well, your request prompted me to dig deeper. It looks like the content received by hMailServer is actually MIME encoded.

Code: Select all

Received: from ( [])
 by mail.*.com with ESMTPS (version=TLSv1.2 cipher=DHE-RSA-AES128-SHA
 bits=128) ; Tue, 1 Oct 2019 08:30:55 -0700
Received: from ZAKR1MMSC01NFE002 ([]) by
 with bizsmtp id 8BTK2100C0bJV4j01FWuDX; Tue, 01 Oct 2019 10:30:54 -0500
Message-ID: <>
In-Reply-To: 1022300288.360393871569943854511.JavaMail.nems@ZAKR1MMSC01NFE002
X-Mms-Message-Type: m-send-req
X-Mms-Transaction-Id: 1569943852-1
X-Mms-MMS-Version: 1.2
To: mike@*.com
From: *
Date: Tue, 1 Oct 2019 15:30:54 +0000 (UTC)
X-Mms-Sender-Visibility: Show
Content-Type: multipart/mixed;  boundary="----=_Part_5557605_1126218263.1569943854510"
MIME-Version: 1.0
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;; s=EMG20171113; t=1569943854;
 bh=ErIhjy1Qzj0S2jzlBahK0VhHlknh7v88qll6Y2v4wWA=; h=In-Reply-To:To:From:Date;
X-hMailServer-LoopCount: 5

Content-Type: text/plain; charset=UTF-8
Content-Disposition: Attachment; Filename=text_0.txt; Charset=US-ASCII
Content-ID: 0
Content-Location: text_0.txt
Content-Transfer-Encoding: BASE64

But when the message is exported from Outlook as .EML, the attachment just appears as the message body itself. Note the two examples are different test messages.

Code: Select all

From: <*>
To: "Mike DiBella"
In-Reply-To: 1531143657.278117281569372172715.JavaMail.nems@ZAKR1MMSC01NFE001
Date: Tue, 24 Sep 2019 17:42:52 -0700
Message-ID: <>
MIME-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-ID: <0>
Content-Disposition: attachment;
X-Mailer: Microsoft Outlook 15.0
Thread-Index: AQH080KBp+ef9vQ4TjyumBt5pRWzhQ==
Content-Language: en-us
x-ms-exchange-organization-authsource: VM-MERCURY.*.net
x-ms-exchange-organization-authas: Anonymous
dkim-signature: v=1; a=rsa-sha256; c=relaxed/simple;;	s=EMG20171113; t=1569372172;	bh=KHWJCVKMlUvj9VdRzLTbixLoUOxhj9NKEeROIlRiuy0=;	h=In-Reply-To:To:From:Date;	b=AKozRD9wzOWUJYDpL2pTcjYzNIEb/YE4laxz4QQRClUvuV1VqUJiFIoEk6WF3WA6M	 fI70sFPWD0F0Qy3VdDOvkpCPOXB0ThZRTP+ZzL0Cu5p4Mi++JMhK0RFhq+YPRnk5mF	 dUUipd3s67Lu/izAyrXi0a20ytAJq3XmxJUEH3FogWNh5VedkLP7/pY/wl5P+RuQPT	 Xqedv1lteJA/HLxI82+2UHiNXtTA1qyE8Wwt2SB3PFE2USbZcQANS995wPGFvYR2vp	 vE5pkWdd0ViINGR7hlIESz3pr9SSYHMAk/ZJow4/6SAWc+L3FedfD7k2hiiNLG+P43	 DmjGvUtHUkSXA==

In any case, the script is handling the conversion quicker than I expected, so I'm just going to run with it for now.

Re: Convert message with text attachment

Posted: 2019-10-01 20:35
by palinka
Ok cool. Looks like you took it the right direction fron the start.