如何发送带有Zend_Mail的附件


<?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(和所有

$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(); 
您需要

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

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


此外,Zend_Mime_Part::type期望MIME类型,即 $attachments->type必须是 MIME 类型。对于文本文件,这是text/plain 。我也不认为您可以设置空文件名。

请尝试此操作,它将起作用。

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

相关内容

  • 没有找到相关文章

最新更新