From 88d4c55ec38c68c07496ed059616b15622fc83e7 Mon Sep 17 00:00:00 2001 From: uckelman Date: Sun, 31 Oct 2010 20:53:21 +0000 Subject: Major refactoring to make bridge easier to test. git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@7431 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/build_email.php | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/build_email.php (limited to 'src/build_email.php') diff --git a/src/build_email.php b/src/build_email.php new file mode 100644 index 0000000..5301106 --- /dev/null +++ b/src/build_email.php @@ -0,0 +1,140 @@ + 'multipart/mixed'); + $mime = new Mail_mimePart('', $params); + + # Build the main body + build_text_part($mime, $text); + + # Build each attachment + foreach ($attachments as $a) { + $bytes = file_get_contents($a['path']); + if ($bytes === false) { + throw new Exception('failed to read file: ' . $a['path']); + } + + build_attachment( + $mime, + $adata['mimetype'], + $adata['real_filename'], + $adata['attach_comment'], + $bytes + ); + } + + # Build footer + build_text_part($mime, $footer); + + # Encode the message + $msg = $mime->encode(); + $headers = array_merge($headers, $msg['headers']); + $body = $msg['body']; + } + + return $body; +} + +function build_headers($userName, $userEmail, $to, $sender, $subject, $edit, + $time, $messageId, $forumURL, $inReplyTo, $references) { + + $from = sprintf('%s <%s>', utf8_quote_non_ascii($userName), $userEmail); + $subject = utf8_quote_non_ascii($subject); + $date = date(DATE_RFC2822, $time); + + if ($edit) { + $edit_header = 'Edit: '; + $subject = $edit_header . $subject; + } + + $headers = array( + 'To' => $to, + 'From' => $from, + 'Sender' => $sender, + 'Subject' => $subject, + 'Date' => $date, + 'Message-ID' => $messageId, + 'X-BeenThere' => $forumURL + ); + + if ($inReplyTo !== null) { + $headers['In-Reply-To'] = $inReplyTo; + } + + if ($references !== null) { + $headers['References'] = $references; + } + + return $headers; +} + +function build_text_part(Mail_mimePart $mime, $text) { + $params = array( + 'content_type' => 'text/plain', + 'charset' => 'utf-8', + 'encoding' => '8bit', + 'disposition' => 'inline' + ); + $mime->addSubPart($text, $params); +} + +function build_attachment(Mail_mimePart $mime, $type, + $filename, $descr, $data) { + $params = array( + 'content_type' => $type, + 'encoding' => 'base64', + 'disposition' => 'attachment', + 'dfilename' => $filename, + 'description' => $descr + ); + $mime->addSubPart($data, $params); +} + +function build_message_id($postId, $editId, $time, $forumHost) { + return "<$time.$postId.$editId.bridge@$forumHost>"; +} + +?> -- cgit v1.2.3