在mime smtp中添加pdf的附件

时间:2014-12-30 11:31:21

标签: php smtp mime

我正在使用mime smtp哟发送邮件。我的问题是邮件没有任何附件。我想发送带附件的pdf邮件。 我的代码如下

$a = $fpdf->writeHTML($fpdf, true, false, true, false, '')  ;
    //$a = $fpdf->Output()  ;


    $host     = 'smtp.ample.co.in';
$username = 'nasadnivas@aadsmple.co.in';
$password = 'asa3';

$to = "namadsivas@ampxle.co.in";
$from = "nadnivas@amsple.co.in";
$subject = "Attachments Test";
$filename = "test";

$headers = array(
  'To' => $to,
  'From' => $from,
  'Subject' => $subject
);

$smtp = Mail::factory('smtp', array('host'=>$host, 'auth'=>true, 'username'=>$username, 'password'=>$password));

$text = "Plain Text Email Content";
$html = "HTML Email Content\n";
//$attachment = "mail_serinv.php?tchyear=2014&tchfyear=14&tchcid=001&tninvid=105466&tcpflag=I&tcnflag=L";

$mime = new Mail_mime();
$mime -> setTXTBody($text);
$mime -> setHTMLBody($html);
$mime -> addAttachment($a, 'text/plain',$filename,false);

$body = $mime -> get();
$headers = $mime -> headers($headers);

$mail = $smtp -> send($to, $headers, $body);

if(PEAR::isError($mail)) {
  echo $mail->getMessage();
} else {
  echo 'Message sent';
}

我搜索了许多链接,发现没用。请帮助我找到解决方案

1 个答案:

答案 0 :(得分:0)

试试这个:

将PDF文件保存到服务器(确保您拥有该文件夹的777个写入权限!): - 请参阅:http://blog.startq.com/2011/10/25/fpdf-output-methods-for-pdf-files-in-php/#sthash.J5K9EQ4h.dpuf

$a = $fpdf->Output('directory/yourfilename.pdf','F');
...

$mime -> addAttachment($a, 'application/pdf',$filename,true);