Convert message with text attachment

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
mikedibella
Normal user
Normal user
Posts: 230
Joined: 2016-12-08 02:21

Convert message with text attachment

Post by mikedibella » 2019-10-01 02:14

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;
	name="text_0.txt"
Content-Transfer-Encoding: 7bit
Content-ID: <0>
Content-Disposition: attachment;
	filename="text_0.txt"
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?

mikedibella
Normal user
Normal user
Posts: 230
Joined: 2016-12-08 02:21

Re: Convert message with text attachment

Post by mikedibella » 2019-10-01 04:45

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
	fAttachment.Close
	oFileSys.DeleteFile sTempFile
	oMessage.Attachments.Item(0).Delete
	' append attachment to message body
	oMessage.Body = oMessage.Body & sText
	oMessage.Subject = "SMS Message"
	oMessage.Save
End Sub

User avatar
mattg
Moderator
Moderator
Posts: 21103
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Convert message with text attachment

Post by mattg » 2019-10-01 05:15

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
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

palinka
Senior user
Senior user
Posts: 2172
Joined: 2017-09-12 17:57

Re: Convert message with text attachment

Post by palinka » 2019-10-01 13:10

Can you post an example of one of these messages? Maybe for no other reason than to satisfy my curiosity.

mikedibella
Normal user
Normal user
Posts: 230
Joined: 2016-12-08 02:21

Re: Convert message with text attachment

Post by mikedibella » 2019-10-01 17:48

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 stcceg-mtmta04.wnsnet.attws.com (stcotaapp-apps-ts2a.mobile.att.net [166.216.152.40])
 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 ([107.79.70.27]) by stcceg-mtmta04.wnsnet.attws.com
 with bizsmtp id 8BTK2100C0bJV4j01FWuDX; Tue, 01 Oct 2019 10:30:54 -0500
Message-ID: <8BTK2100C0bJV4j01FWuDX@txt.att.net>
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: *@mms.att.net
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; d=mms.att.net; s=EMG20171113; t=1569943854;
 bh=ErIhjy1Qzj0S2jzlBahK0VhHlknh7v88qll6Y2v4wWA=; h=In-Reply-To:To:From:Date;
 b=IdCziCvrSLoorw1Klo3Y/or9abgzLbbtkutHc6MiJl3ifV73KtpUZyj7uvYO12B89
 T/2bY7FW9Cq+YF2i4vIi5aiFRIbv3lelJsCiGH2HUzU6xTjTq73r/JHFdsby351etU
 wR6D+FL3Zsm2+oGtSBhAhprdrbTyBwfMdEH64XYmpq30wvkmhPHU7HP+j5T7Y6M1JX
 8x/wCNHacgfVEBUC6bHPRDCasDTgv9kejemQAGYxmaUR2ZO3vqYlltztK2qvTQic62
 U/996F4AD5qM6O+ZU2nZgOr+07vnoysq54w3AYcqJEeK6YXcn/hesFUhbDPj86hGBt
 4v3kaFdaBNzzQ==
X-hMailServer-LoopCount: 5

------=_Part_5557605_1126218263.1569943854510
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

VGhpcyBpcyBhIHNhbXBsZSBTTVMgbWVzc2FnZS4=
------=_Part_5557605_1126218263.1569943854510--
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: <*@mms.att.net>
To: "Mike DiBella"
In-Reply-To: 1531143657.278117281569372172715.JavaMail.nems@ZAKR1MMSC01NFE001
Date: Tue, 24 Sep 2019 17:42:52 -0700
Message-ID: <5P852100T0cc9Qz01cishc@txt.att.net>
MIME-Version: 1.0
Content-Type: text/plain;
	name="text_0.txt"
Content-Transfer-Encoding: 7bit
Content-ID: <0>
Content-Disposition: attachment;
	filename="text_0.txt"
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; d=mms.att.net;	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==

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

palinka
Senior user
Senior user
Posts: 2172
Joined: 2017-09-12 17:57

Re: Convert message with text attachment

Post by palinka » 2019-10-01 20:35

Ok cool. Looks like you took it the right direction fron the start.

Post Reply