MIME file attachments won't open
My code
<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "<meenu.spiralbean@gmail.com>";
$to = "<meenu.spiralbean@gmail.com>";
$subject = "Hi!";
$host = "ssl://smtp.googlemail.com";
$port = "465";
$username = "example@gmail.com";
$password = "example";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
// attachment
$file = '/var/www/html/formsubmit/TodoList/upload/abc.pdf';
$crlf = "n";
$text="Helllooooo";
$html = "<html> <head> <title>Mail test</title> </head> <body>something</body></html>";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
if($mime->addAttachment($file))
{
echo "Success";
}
else
{
echo "Failed";
}
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
The attachment cannot be opened even after downloading it.
The mail goes to the Inbox and the attachment is there as well. But the attachment is like txt file.but. I am actually attaching a pdf file. I have used SMTP and MIME mail mail.
Help me.
+3
source to share
1 answer
Relevant code in the mail:
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream;
name=abc.pdf
Content-Disposition: attachment;
filename=abc.pdf;
size=4997278
The content type is generic; try using application/pdf
as second parameter for addAttachment()
.
Also, NEVER send your real email address and password anywhere.
+2
source to share