如何使用Zend_Mail发送附件?

时间:2013-11-26 07:51:04

标签: php email zend-framework attachment

<?php

$message=$_POST['feedback'];
$attachments=$_POST['file'];
$subject=$_POST['subject'];
$tosend=$_POST['to'];
$tocc=$_POST['cc'];

$mail = new Zend_Mail();

$mail->setFrom('user@example.com','Admin');
$mail->addTo($tosend, 'Some Recipient');
$mail->addCc($tocc);

$mail->setType(Zend_Mime::MULTIPART_RELATED);
$mail->setSubject($subject);
$mail->setBodyHtml($message);

$attachments = $mail->createAttachment(file_get_contents($attachments));

$attachments->type = '.txt';        
$attachments->filename = "";

$mail->send($transport);
?>

使用此代码我无法打开文件,它只取文件名而不是打开附件的整个路径,当我点击打开附件时它给出错误,因为无法打开流。

此代码与cc Bcc和所有

等其他功能一起正常运行

3 个答案:

答案 0 :(得分:3)

$mail = new Zend_Mail();
$mail->setBodyHtml("description");
$mail->setFrom('id', 'name');
$mail->addTo(email, name);
$mail->setSubject(subject);

$content = file_get_contents("path to pdf file"); // e.g. ("attachment/abc.pdf")
$attachment = new Zend_Mime_Part($content);
$attachment->type = 'application/pdf';
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->filename = 'filename.pdf'; // name of file

$mail->addAttachment($attachment);                  

$mail->send(); 

答案 1 :(得分:1)

您需要使用$_FILES数组来打开文件,而不是$_POST$_POST仅包含文件名,$_FILES包含实际文件。

请参阅http://php.net/manual/en/reserved.variables.files.php


此外,Zend_Mime_Part::type需要MIME类型,即$attachments->type需要是MIME类型。对于文本文件,这是text/plain。我也认为你不能设置一个空文件名。

答案 2 :(得分:0)

请试试这个,它会起作用。
     

 $file = "path of file";

 $mail = new Zend_Mail();
 $mail->setFrom("test@test.com", "test");
 $mail->addTo("test1@test.com", "test user");
 $mail->setSubject("file mail");
 $mail->setBodyHtml("html");   
 $at = new Zend_Mime_Part(file_get_contents($file));
 $at->type        = mime_content_type($file);
 $at->disposition = Zend_Mime::DISPOSITION_INLINE;
 $at->encoding    = Zend_Mime::ENCODING_BASE64;
 $at->filename    = basename($file);

 $mail->addAttachment($at); 

 if($mail->send())
 {
          echo " sent";
 }
 else
 {
      echo "not sent";
 }

 ?>