我如何使用Zend_Mail发送附件?

4
<?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);
?>

使用这段代码时,我无法打开文件,它只获取文件名而不是完整路径以打开附件。当我点击打开附件时,会出现“无法打开流”的错误。

但是,这段代码在其他功能如抄送、密送等方面运行良好。

3个回答

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(); 

这是针对PDF类型的,您可以根据需要进行更改。 - Ronak K

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

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


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

我已经按照您的答案操作了,但仍然无法找到。$message = $ _POST ['feedback']; $attachments = $ _FILES ['file']; $subject = $ _POST ['subject']; $tosend = $ _POST ['to']; $tocc = $ _POST ['cc']; - user2366160

0

please try this, it will work.

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

 ?>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接