【问题标题】:Zend Mail 2.0 AttachmentsZend Mail 2.0 附件
【发布时间】:2012-04-28 19:07:23
【问题描述】:

谁能提供一些关于如何向 ZF2 Mail 组件添加附件的示例?

我确实喜欢:

$message = new Message;
$message->setEncoding('utf-8');
$message->setTo($email);
$message->setReplyTo($replyTo);
$message->setFrom($from);
$message->setSubject($subject);
$message->setBody($body);

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

【问题讨论】:

    标签: zend-framework zend-mail zend-framework2


    【解决方案1】:

    要添加附件,您只需创建一个新的 MIME 部分并将其添加到邮件中。

    例子:

    // create a new Zend\Mail\Message object
    $message  = new Message;
    
    // create a MimeMessage object that will hold the mail body and any attachments
    $bodyPart = new MimeMessage;
    
    // create the attachment
    $attachment = new MimePart(fopen($pathToAttachment));
    // or
    $attachment = new MimePart($attachmentContent);
    
    // set attachment content type
    $attachment->type = 'image/png';
    
    // create the mime part for the message body
    // you can add one for text and one for html if needed
    $bodyMessage = new MimePart($body);
    $bodyMessage->type = 'text/html';
    
    // add the message body and attachment(s) to the MimeMessage
    $bodyPart->setParts(array($bodyMessage, $attachment));
    
    $message->setEncoding('utf-8')
            ->setTo($email)
            ->setReplyTo($replyTo)
            ->setFrom($from)
            ->setSubject($subject)
            ->setBody($bodyPart);  // set the body of the Mail to the MimeMessage with the mail content and attachment
    

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

    【讨论】:

    • 我还没有验证它,但它似乎是一个可行的解决方案,因为我还认为应该与 Mime 进行一些交易。谢谢。
    • 我不知道为什么决定从 Mail 类中删除 addAttachment 方法,在 ZF1 中它只会为您创建一个新的 mime 部分并将其添加到消息中,现在它是更明确一点。
    • 我认为这里值得一提的是,ZF 2 中的新 MimePart 和 MimeMessage 现在位于 Zend\Mime\Part 和 Zend\Mime\Message 中 - 因此您必须相应地使用它们的命名空间。
    猜你喜欢
    • 2012-03-28
    • 2014-11-26
    • 2013-10-30
    • 2011-04-22
    • 1970-01-01
    • 2011-09-10
    • 2010-10-31
    • 1970-01-01
    • 2012-01-24
    相关资源
    最近更新 更多