MIME文件附件未打开

时间:2015-05-08 05:38:32

标签: php email pear mime

我的代码是

<?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>");
}
?>

即使在下载附件后也无法打开。

邮件进入收件箱,附件也在那里。但附件似乎是txt文件。但我实际上附加了一个pdf文件。我使用了SMTP邮件和MIME邮件功能。

请帮助我。

1 个答案:

答案 0 :(得分:2)

邮件中的相关代码是:

Content-Transfer-Encoding: base64
Content-Type: application/octet-stream;
 name=abc.pdf
Content-Disposition: attachment;
 filename=abc.pdf;
 size=4997278

内容类型是通用类型;尝试使用application/pdf作为addAttachment()的第二个参数。

此外,切勿在任何地方发布您的真实邮件地址和密码。