Jump to content

Recommended Posts

Posted (edited)

Hello,

I am trying to determine the size of attachments from .eml files. The scenario is that I have thousands of emails in .eml file type and need to determine the size of the attachment in some of the emails. I have already determined the number of emails in which there are attachments and the number of attachments per email. Also, emails without attachments.

Can anyone shed some light on how i can go about this? BTW, the attachments are now hashed/coded? I don't know how you call it but its just a bunch of characters.

File attached is a sample email.

Regards, 

ivan

sample.eml

Edited by ivananonymous
Posted

Which program do you use to read the mails - Outlook?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

The thing is I am not using any program to read it but created my own simple program to check if it has attachments via specific string. Do I really have to have a program to do this? I am open to suggestions.

Have a nice weekend! 

 

Posted

Outlook would be easy because my OutlookEX UDF has a function to do this: _OL_ItemAttachmentGet.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

I would definitely go with water's suggestion and find a program or library to do it. Doesn't have to be Outlook. There are a number of ways to include attachments and I'm not sure how reliable of a program you're going to get if you make a bunch of assumptions here. 

Just in case you want to do it anyway. First get this part:

boundary="----=_Part_75_17152415.1274866451564"

Use that to split the content in the file to get this:

Content-Type: image/jpeg; name="canada plane.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: INLINE; filename="canada plane.jpg"

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU
FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo
KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAGrAoADASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA

If you can reasonable assume base64, you can discard the header so you are left with this:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU
FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo
KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAGrAoADASIA

Either decode that with base64 decoding and count the number of bytes in the output or guesstimate based on the length of the base64 string if that's good enough.

Posted

You could try with CDO. It would be something like this

$sEML = "sample.eml"
$sAttachment = "canada plane.jpg"

$iSize = GetAttachmentSize($sEML, $sAttachment)

If @error Then
    $sErrDesc = "You can't use this method because "
    Switch @error
        Case 1
            $sErrDesc &= "CDO sucks for you."
        Case 2
            $sErrDesc &= "getting stream failed."
        Case 3
            $sErrDesc &= "loading EML failed."
        Case 4
            $sErrDesc &= "of some dumb error."
        Case 5
            $sErrDesc = "No such attachment exists."
    EndSwitch
    MsgBox(4096, "Error", $sErrDesc & @CRLF)
Else
    MsgBox(4096, "...", 'Attachment named "' & $sAttachment & '" is ' & $iSize & " bytes big" & @CRLF)
EndIf


Func GetAttachmentSize($sEMLPath, $sAttachmentName)
    Local $oErrHandler = ObjEvent("AutoIt.Error", GetAttachmentSize) ; Red Jon fucked up COM

    Local $oMessage = ObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}") ; with all the downsides
    If @error Then Return SetError(1, 0, -1)

    Local $oMessageStream = $oMessage.GetStream()
    If @error Then Return SetError(2, 0, -1)

    $oMessageStream.LoadFromFile($sEMLPath)
    If @error Then Return SetError(3, 0, -1)

    $oMessageStream.Flush() ; this is essential

    Local $iSize
    For $oAttachment In $oMessage.Attachments
        If $oAttachment.FileName = $sAttachmentName Then $iSize = $oAttachment.GetDecodedContentStream().Size
        If @error Then Return SetError(4, 0, -1)
        If $iSize Then Return $iSize
    Next

    Return SetError(5, 0, -1)
EndFunc

If you want the size of all attachments then just add up to $iSize inside the loop and remove second parameter.

♡♡♡

.

eMyvnE

  • 2 weeks later...
Posted (edited)

Try this:

$sAttachmentFileName = $oAttachment.GetDecodedContentStream().FileName
$oAttachment.SaveToFile(@ScriptDir & "\"& $sAttachmentFileName)

 

or

Func SaveAttachment($sEMLPath, $sAttachmentName)
    Local $oErrHandler = ObjEvent("AutoIt.Error", GetAttachmentSize) ; Red Jon fucked up COM

    Local $oMessage = ObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}") ; with all the downsides
    If @error Then Return SetError(1, 0, -1)

    Local $oMessageStream = $oMessage.GetStream()
    If @error Then Return SetError(2, 0, -1)

    $oMessageStream.LoadFromFile($sEMLPath)
    If @error Then Return SetError(3, 0, -1)

    $oMessageStream.Flush() ; this is essential

    Local $iSize
    For $oAttachment In $oMessage.Attachments
        If $oAttachment.FileName = $sAttachmentName Then $oAttachment.SaveToFile(@ScriptDir & "\" & $sAttachmentName)
        If @error Then
            Return SetError(4, 0, -1)
        Else
            Return SetError(0, 0, 1)
        EndIf
    Next

    Return SetError(5, 0, -1)
EndFunc   ;==>SaveAttachment

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

and some other stuff:

#include "array.au3"

Local $sEMLPath = "EMAIL.eml"
$sAttachment = "image.jpg"

.....
Local $aAttachments = GetAttachmentList($sEMLPath)
_ArrayDisplay($aAttachments)

....
Func GetAttachmentList($sEMLPath)
    Local $oErrHandler = ObjEvent("AutoIt.Error", GetAttachmentSize) ; Red Jon fucked up COM

    Local $oMessage = ObjCreate("{CD000001-8B95-11D1-82DB-00C04FB1625D}") ; with all the downsides
    If @error Then Return SetError(1, 0, -1)

    Local $oMessageStream = $oMessage.GetStream()
    If @error Then Return SetError(2, 0, -1)

    $oMessageStream.LoadFromFile($sEMLPath)
    If @error Then Return SetError(3, 0, -1)

    $oMessageStream.Flush() ; this is essential

    Local $sAttachments = ''
    For $oAttachment In $oMessage.Attachments
        $sAttachments &= $oAttachment.FileName & '|'
    Next
    If $sAttachments <> '' Then
        StringTrimRight($sAttachments,1)
        Local $aAttachments = StringSplit($sAttachments,'|')
        Return SetError(0, 0, $aAttachments)
    Else
            Return SetError(4, 0, -1)
    EndIf



    Return SetError(5, 0, -1)
EndFunc   ;==>SaveAttachment

 

 

btw.

Good material for the new UDF.
If I find time, I'll do it.

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 1 year later...
Posted

Hi,

Any Chance of deleting or replacing an attachment with autoit? Extracting attachments works, but I want to replace the attachment afterwards in the eml File (or at least delete them).

$oAttachment.Delete or $oAttachment.Delete() won't work - found the .delte function on an MS Page about the Message CDO and thought I might try it, but had no luck...

Thanks in advance!

Posted

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

I get the attachments Count (+ 1, think this is the email itself) via $oMessage.BodyPart.BodyParts.Count, but I don't manage to delete the attachment as the delete function takes another parameter (number or reference - might be able to work with number once I figure out if the mail itself is the first or last object) and actually save the eml file.

On the other side I seem to be able to $oMessage.AddAttachment(@ScriptDir & "\1.pdf") (at least I don't get an error) - but https://msdn.microsoft.com/en-us/library/ms526130(v=exchg.10).aspx doesn't show a save method to write the .eml with the new attachment to disk?

Edit2: Deleting seems to work, but I can't get the File to Save :(

ConsoleWrite($oMessage.BodyPart.BodyParts.Count & @CRLF)
    $oMessage.BodyPart.BodyParts.Delete(2)
    ConsoleWrite($oMessage.BodyPart.BodyParts.Count & @CRLF)
    $oMessage.AddAttachment(@ScriptDir & "\1.pdf")
    ConsoleWrite($oMessage.BodyPart.BodyParts.Count & @CRLF)

shows 3 in the first line and 2 in the second line, so Item 2 is deleted and 3 on the last line so the 1.pdf file is added

Edit3:

Nevermind... after taking a quick nap I found the reason I can't seem to save - I forgot to get the stream after chaning the Object... After manipulating the Attachments this works

$oMessageStream = $oMessage.GetStream()
$oMessageStream.Flush() ; this is essential
$oMessageStream.SaveToFile(@ScriptDir & "\new.eml")

 

Edited by AIstarter
Posted

Would it be sensible to create a CDO UDF similar to - lets say - the OutlookEX UDF?
How many users would use it?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

 

  On 8/20/2015 at 7:41 AM, mLipok said:

Good material for the new UDF.
If I find time, I'll do it.

Expand  

@water  I also be happy :)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Ops, seems I missed that :>

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

So if you have some spare time, I'll be really very happy if you start ....

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

I started to read about CDO and am no longer sure it is a good idea to spend time creating a CDO UDF.

Which features would people expect from a CDO UDF?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

I was thinking about using it together with POP3ex.au3 UDF (when you get emails you should be able to parse them).

But on the other side after deliberation I'm also thinking about buying Chilkat component, and for that I'm not so hurry to have CDO UDF for handling EML files.

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...