note 53669 deleted from function.mail by sean
| From: | sean@php.net | Date: | Wed, 08 Jun 2005 18:39:13 +0000 |
| Subject: | note 53669 deleted from function.mail by sean | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-90135@lists.php.net to get a copy of this message | ||
Note Submitter: jameslee at cs dot nmt dot edu
Reason: useless
----
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);
?>