trying to understand example code for Mail_mimePart::addsubpart()
am 24.10.2007 17:43:26 von hmbscullyI am trying to send a complex mutli-part email with inline attachments
between html pieces.
I originally wrote the code in Perl with the MIME::Lite module, which
worked fine. My attempts to convert the code to PHP have ended in
frustration so far. It seems when I search, all roads point me to
PEAR's Mail_mimePart, but I am not understanding the example code
enough to try to use it.
Can someone help me start to figure out how this code works?
The example code is:
include 'Mail/mimePart.php';
....
$params['content_type'] = 'multipart/mixed';
$email = new Mail_mimePart('', $params);
// Here we add a text part to the multipart we have
// already. Assume $body contains plain text.
$params['content_type'] = 'text/plain';
$params['encoding'] = '7bit';
$text = $email->addSubPart($body, $params);
// Now add an attachment. Assume $contents is
// the contents of the attachment
$params['content_type'] = 'application/zip';
$params['encoding'] = 'base64';
$params['disposition'] = 'attachment';
$params['dfilename'] = 'example.zip';
$attach =& $email->addSubPart($contents, $params);
// Now build the email. Note that the encode
// function returns an associative array containing two
// elements, body and headers. You will need to add extra
// headers, (eg. Mime-Version) before sending.
$email = $email->encode();
$email['headers']['Mime-Version'] = '1.0';
....
?>
What I don't understand at all are the
$text = $email->addSubPart($body, $params);
and
$attach =& $email->addSubPart($contents, $params);
lines in that I don't understand why there are the $text and $attach
variables, because they never seem to be used again.
I also don't know how to actually send the message.
My sad attempt to finish the script had me adding this:
$email =& Mail::factory('mail');
$email->send($to_email, $email['headers'], $email['body'] );
Which throws errors about things not being objects.
Is there anywhere that there is example code for how to do a complex
multi-part message?
I've tried this so many ways, with and without the PEAR code and
nothing can replicate what the Perl code creates.