1) Didn't include my file when I posted - oops
2) When I realised 1 and followed up with another, my mail seems to have
dissappeared into the ether

So I'll include my script in the body of this message.

REBOL [
    Title:  "Import attachments into a email object field."
    Date:   13-June-2000
    Author: "Brett Handley"
    Email:  [EMAIL PROTECTED]
    File:   %import-attachments.r
    Purpose: {
        To extract the email attachments of an email object and
        place them as a block in a field of the object.
    }
    Note: {Code borrowed heavily from %detach.r by Bohdan Lechnowsky.}
    Category: 'email
]

multi-replace: function [str tags][ptr][
    foreach [tag symbol] tags [
        str: head str
        while [ptr: find str tag][
            str: back insert (remove/part ptr (length? tag)) symbol
        ]
    ]
    head str
]

get-content-type: function [ msg [object!]
][type][
    ; Determine the content-type.
    load content-type: pick parse msg/content-type none 1
]

decode-file-type: function [ attachment [object!]
][atch content-spec filename][

    content-spec: parse attachment/content-type {";}
    filename: select content-spec "name="

    Print ["Attachment type:" content-spec/1 "name:" filename]

    atch: copy attachment/content

    while [find ["^/" " " "^-" "-"] back tail atch][remove back tail atch]
    if value? 'attachment/content-transfer-encoding [
        if find attachment/content-transfer-encoding "64" [
           atch: do append insert head atch "64#{" "}"
        ]
        if find attachment/content-transfer-encoding "quoted-printable" [
           atch: multi-replace atch ["=3D" "=" "=^/" "" "=20" " "]
        ]
    ]
    reduce[to-file filename atch]
]


decode-multipart-type: function [ msg [object!] /flat
][
boundary part parts delimiter
close-delimiter parse-result
attachment-in-part return-block]
[

    ; Get the boundary

    Prin "Begin multipart using boundary "

    boundary: msg/content-type
    either string? boundary [
        if boundary: find/tail boundary {boundary=} [
            boundary: trim/with boundary {"}
        ]
    ][
        if start: find msg/content "^/--" [
            boundary: copy/part next start find next start newline
        ]
    ]

    print [boundary]

    ; Get the parts

    parts: make block! 2
    delimiter: rejoin ["--" boundary newline]
    close-delimiter: rejoin ["--" boundary "--" newline]
    parse-result: parse m/content [
        copy prologue to delimiter
        some [ thru delimiter copy part [to delimiter | to close-delimiter]
            (append parts part)]
        thru close-delimiter copy epilogue to end
    ]
    if not parse-result [print "Error: Parse parts assumption failed"]

    ; Now process each part.

    return-block: make block! 2
    foreach part parts [
        attachment-in-part: extract-attachments parse-header none part
        if attachment-in-part [
            either flat [append return-block attachment-in-part]
                [append/only return-block attachment-in-part]
        ]
    ]

    Print ["End multipart using boundary " boundary]
    return-block

]

extract-attachments: function [ "Return a block of attachments"
   msg [object!]
   /nest "Preserve nested multipart message using nested blocks."
][return-block][
    switch/default pick get-content-type msg 1 [
       image     [return-block: decode-file-type msg]
       multipart [ return-block: either nest [decode-multipart-type
msg][decode-multipart-type/flat msg] ]
    ] [Print ["Skipping content-type: " get-content-type msg] ]
    return-block
]

import-attachments: function [ "Import any attachments as a field of object"
    msg [object!]
][x][
    x: make msg [ attachments: none ]
    x/attachments: extract-attachments msg
    x
]


Reply via email to