[mel-base-devel] Saving and finding attachments
Fred Gibson
fred at streamfocus.com
Fri Jan 29 01:14:48 UTC 2010
I've written a function to save an attachment:
(defun save-attachment-file (part filename)
(let* ((attach (mel.mime:part-body-string part))
(length (length
(string-trim '(#\newline #\return #\linefeed)
(read-line (make-string-input-stream attach)))))
(in (make-string-input-stream attach)))
(with-open-file (out filename :direction :output :element-type
'(unsigned-byte 8))
(loop with buffer = (make-array length :element-type 'character)
for count = (read-sequence buffer in)
while (> count 0)
do (write-sequence (mel.mime::decode-base64 buffer) out)
(flet ((peek ()(peek-char nil in nil :eof))
(consume () (read-char in nil :eof)))
(tagbody
start (let ((c (peek)))
(when (member c '(#\return #\linefeed #\newline))
(progn (consume) (go start))))))))))
and for finding attachments in a message:
(defun find-attachments (eml)
(flet ((eml-mixed? (e)
(multiple-value-bind (a b c) (mel.mime:content-type e)
(declare (ignore a c))
(when (eq b :mixed) t)))
(eml-part-base64? (part)
(when (eq (mel.mime:content-transfer-encoding part) :base64)
t)))
(when (eml-mixed? eml)
(let (attach)
(dolist (part (mel:parts eml))
(when (eml-part-base64? part)
(push part attach)))
attach))))
--
Fred Gibson
Founder / Software Developer
http://www.streamfocus.com
(c)2010 Organon Technologies LLC
More information about the mel-base-devel
mailing list