note 53669 added to function.mail

From: Date: Wed, 08 Jun 2005 18:35:54 +0000
Subject: note 53669 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-90134@lists.php.net to get a copy of this message
The PEAR package Mail_Mime makes sending attachments and multipart emails pretty painless. There is a nice little "gotcha" however: the headers() method MUST be called after get() which MUST be called after all the attachments have been added. This works: <?php // ... $body = $mime->get(); $head = $mime->headers(); ?> This DOES NOT WORK AS EXPECTED: <?php // ... $body = $mime->headers(); $head = $mime->get(); ?> The above results in sending the whole email including headers as plain text. Full example: <?php $to = 'santa@northpole.com'; $text_body = 'Hi, Santa! I want a bicycle! kthxbye.'; $html_body = '<h1>Dear Santa</h1><p>gimme bike.</p>'; $extraheaders = array( 'From' => 'webmaster@example.com', 'Subject' => 'Bikey 4 xmas', 'Reply-To' => 'webmaster@example.com', 'X-Mailer' => 'PHP/'. phpversion(), ); $mime =& new Mail_Mime("\n"); $mime->setTXTBody($text_body); $mime->setHTMLBody($html_body); $mime->addAttachment('spiffy_bike.pdf','application/pdf'); $body = $mime->get(); // get() must be before headers() $hdrs = $mime->headers($extraheaders); $mail =& Mail::factory('mail'); $mail->send($to,$hdrs,$body); ?> ---- Manual Page -- http://www.php.net/manual/en/function.mail.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53669 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53669&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53669&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53669&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53669&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53669&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53669&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90134) next »