zend-framework – Zend Mail 2.0附件

前端之家收集整理的这篇文章主要介绍了zend-framework – Zend Mail 2.0附件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
任何人都可以提供一些关于如何向ZF2邮件组件添加附件的示例吗?

我喜欢:

  1. $message = new Message;
  2. $message->setEncoding('utf-8');
  3. $message->setTo($email);
  4. $message->setReplyTo($replyTo);
  5. $message->setFrom($from);
  6. $message->setSubject($subject);
  7. $message->setBody($body);

但在需要添加附件时卡住了.
谢谢.

添加附件,您只需创建一个新的MIME部分并将其添加到消息中.

例:

  1. // create a new Zend\Mail\Message object
  2. $message = new Message;
  3.  
  4. // create a MimeMessage object that will hold the mail body and any attachments
  5. $bodyPart = new MimeMessage;
  6.  
  7. // create the attachment
  8. $attachment = new MimePart(fopen($pathToAttachment));
  9. // or
  10. $attachment = new MimePart($attachmentContent);
  11.  
  12. // set attachment content type
  13. $attachment->type = 'image/png';
  14.  
  15. // create the mime part for the message body
  16. // you can add one for text and one for html if needed
  17. $bodyMessage = new MimePart($body);
  18. $bodyMessage->type = 'text/html';
  19.  
  20. // add the message body and attachment(s) to the MimeMessage
  21. $bodyPart->setParts(array($bodyMessage,$attachment));
  22.  
  23. $message->setEncoding('utf-8')
  24. ->setTo($email)
  25. ->setReplyTo($replyTo)
  26. ->setFrom($from)
  27. ->setSubject($subject)
  28. ->setBody($bodyPart); // set the body of the Mail to the MimeMessage with the mail content and attachment

以下是有关该主题的一些有用文档:ZF2 – Zend\Mail

猜你在找的PHP相关文章