Problem Mailing PDF using Mime
| From: | Brian Drexler | Date: | Fri, 09 Sep 2005 15:06:22 +0000 |
| Subject: | Problem Mailing PDF using Mime | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-20879@lists.php.net to get a copy of this message | ||
Ok, here's my issue. I'm sending a mail message with a PDF attachment using
a script I wrote probably a year or two ago. It worked fine on my RedHat
6.2 machine running PHP version 4.3.11. Now I'm running SUSE 9.1 with PHP
4.4.0. If I send the e-mail to anyone using Outlook/Mozilla Thunderbird for
a client it works fine, but when I try sending it to my gmail account for
instance, it mashes all the headers and everything into the body of the
message and my PDF becomes text in the body of the message. Anyone have any
ideas? Here is basically the script I'm using with a default PEAR/Mime/Mail
configuration.
<?php
require_once('/usr/local/lib/php/Mail.php'); // These two files are
par
t of Pear,
require_once('/usr/local/lib/php/Mail/mime.php'); // and are required
for th
e Mail_Mime class
$to = "myaddress@gmail.com";
$from = "myaddress@mywork.com";
$subject = "This is a test email";
$attachment = "/tmp/Quote14-63344.pdf";
// Next we must build an array of email headers for the email.
// This is structured slightly differently to the mail() function.
// The array key is the header name. Remember that subject
// is a header too!
$headers = array('From' => $from,
'Subject' => $subject);
// Here we create the plaintext version of the email to be sent
$textMessage = "Dear John,\n\nThis is a fake email, I hope you enjoy
it.\n\n
From Jane.";
// Here we create the HTML version of the email to be send, using
// the heredoc syntax
$htmlMessage = <<<EOF
<html>
<body bgcolor="#ffffff">
<p align="center">
<b>Hello World!</b>
</p>
</body>
</html>
EOF;
$mime = new Mail_Mime();
// create a new instance of the Mail_Mime class
$mime->setTxtBody($textMessage);
// set our plaintext message
$mime->setHtmlBody($htmlMessage);
// set our HTML message
$mime->addAttachment($attachment);
// attach the file to the email
$body = $mime->get();
// This builds the email message and returns it to $body.
// This should only be called after all the text, html and
// attachments are set.
$hdrs = $mime->headers($headers);
// This builds the corresponding headers for the plaintext,
// HTML and any other required headers. It also includes
// the headers we created earlier by passing them as an argument.
$mail = &Mail::factory('mail');
// Creates an instance of the mail backend that we can use
// to send the email.
$mail->send($to, $hdrs, $body);
// Send our email, according to the address in $to, the email
// headers in $hdrs, and the message body in $body.
?>