Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.8.9

Bug #16623 When sending mails with attachments, the same boundaries are used
Submitted: 2009-09-17 17:09 UTC Modified: 2009-12-18 14:49 UTC
From: markusknaup Assigned: alec
Status: Closed Package: Mail_Mime (version 1.5.2)
PHP Version: 5.2.5 OS: Debian Lenny
Roadmaps: 1.5.3    
Subscription  


 [2009-09-17 17:09 UTC] markusknaup (Markus Knaup)
Description: ------------ I had problems, when attaching a file to an e-mail: <code> $mime = new Mail_mime("\n"); $mime->setTXTBody($txtBody); $mime->setHTMLBody($htmlBody); if (is_array($attachments)) { foreach($attachments as $attachment) { $mime->addAttachment($attachment["file"], $attachment["contentType"], $attachment["name"], $attachment["isFile"], $attachment["encoding"]); } } $mailBody = $mime->get($_build_params); $mailHeaders = $mime->headers($mailHeaders); $mail =& Mail::factory($this->backend, $this->backendParams); $mail->send($this->recip, $mailHeaders, $mailBody); </code> In this use case the mail was sent, but nearly all webmailers and e-mail programs did not show the attachment(s). I analysed the source code of the sent e-mails and I saw that the same boundaries were used for different e-mail parts (here "_d188467fafaf9f24df2e4585a2d76019"): <code> To: [E-MAIL-ADDRESS] Subject: [SUBJECT] MIME-Version: 1.0 From: [ANOTHER E-MAIL-ADDRESS] Content-Type: multipart/mixed; boundary="=_d188467fafaf9f24df2e4585a2d76019" Date: Thu, 17 Sep 2009 17:37:41 +0200 (CEST) --_d188467fafaf9f24df2e4585a2d76019 Content-Type: multipart/alternative; boundary="=_d188467fafaf9f24df2e4585a2d76019" --=_d188467fafaf9f24df2e4585a2d76019 Content-Transfer-Encoding: base64 Content-Type: text/plain; charset="utf-8" [CONTENT] --=_d188467fafaf9f24df2e4585a2d76019 Content-Transfer-Encoding: base64 Content-Type: text/html; charset="utf-8" [CONTENT] --=_d188467fafaf9f24df2e4585a2d76019-- --=_d188467fafaf9f24df2e4585a2d76019 Content-Transfer-Encoding: base64 Content-Type: application/pdf; name="file.pdf"; Content-Disposition: attachment; filename="file.pdf"; [CONTENT] --_d188467fafaf9f24df2e4585a2d76019-- </code> So, I wrote a simple (not optimal) workaround, but this should be fixed somehow in Mail_mime (if it is a bug): <code> $mailBody = $mime->get($_build_params); $mailHeaders = $mime->headers($mailHeaders); // Workaround, because, if attachments exist the same boundary is used for several e-mail parts if (sizeof($this->attachments) > 0) { preg_match("/boundary=\"(.[^\"]*)\"/e", $mailHeaders["Content- Type"], $boundary); $boundary = $boundary[1]; $bounderyNew = $bounderyNew; $mailHeaders["Content-Type"] = preg_replace('/boundary="(.[^"]*)"/', 'boundary="' . $bounderyNew . '"', $mailHeaders["Content-Type"]); $mailBody = preg_replace("/^\-\-" . $boundary . "/s", "--" . $bounderyNew, $mailBody); $mailBody = preg_replace("/" . $boundary . "--$/s", $bounderyNew . "--", $mailBody); $mailBody = preg_replace("/" . $boundary . "--(\s*)--" . $boundary . "/s", $boundary . "--$1--" . $bounderyNew, $mailBody); } $mail =& Mail::factory($this->backend, $this->backendParams); </code> This changes the boundary of the toplevel e-mail parts.

Comments

 [2009-09-17 17:36 UTC] markusknaup (Markus Knaup)
There was a small bug in my workaround, now the correct version ($bounderyNew = md5($boundary);): <code> $mailBody = $mime->get($_build_params); $mailHeaders = $mime->headers($mailHeaders); // Workaround, because, if attachments exist the same boundary is used for several e-mail parts if (sizeof($this->attachments) > 0) { preg_match("/boundary=\"(.[^\"]*)\"/e", $mailHeaders["Content- Type"], $boundary); $boundary = $boundary[1]; $bounderyNew = md5($boundary); $mailHeaders["Content-Type"] = preg_replace('/boundary="(.[^"]*)"/', 'boundary="' . $bounderyNew . '"', $mailHeaders["Content-Type"]); $mailBody = preg_replace("/^\-\-" . $boundary . "/s", "--" . $bounderyNew, $mailBody); $mailBody = preg_replace("/" . $boundary . "--$/s", $bounderyNew . "--", $mailBody); $mailBody = preg_replace("/" . $boundary . "--(\s*)--" . $boundary . "/s", $boundary . "--$1--" . $bounderyNew, $mailBody); } $mail =& Mail::factory($this->backend, $this->backendParams); </code>
 [2009-12-18 11:35 UTC] alec (Aleksander Machniak)
-Status: Open +Status: Closed -Assigned To: +Assigned To: alec
This bug has been fixed in SVN. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.
 [2009-12-18 14:49 UTC] alec (Aleksander Machniak)
-Roadmap Versions: +Roadmap Versions: 1.5.3