1

我遇到了一个奇怪的错误:
我使用 Zend\Mail\Message 和 Zend\Mime\Message 发送带有或不带附件的 HTML 格式的电子邮件。
一切正常,但现在 HTML 作为附件发送。

我会放一些代码(我创建了一个类来发送我的电子邮件):

<?php
class EmailManager
{   
    static protected $_instance;

    protected $sender      = null;
    protected $message     = null;
    protected $mimeMessage = null;
    protected $parts       = null;
    protected $subject     = null;

    .....

    protected function __construct() {
        self::init();           
    }

    protected function init()
    {
        $this->sender = new Sendmail();
        $this->message = new Message();
        $this->mimeMessage = new MimeMessage();
        $this->parts = array();
        $this->subject = DEFAULT_SUBJECT;
    } 

    protected function loadReset(...)
    {
            ...

        $subject = '...';
        $content = '
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                </head>
                <body>
                    SOM HTML CONTENT
                </body>
            </html>
        ';

        $body = new Part($content);
        $body->type    = 'text/html';
        $body->charset = 'UTF-8';
        $body->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;

        $this->parts[] = $body;
        $this->subject = $subject;
    }

    public function sendTo($receiver)
    {   
        $this->mimeMessage->setParts($this->parts);

        $this->message->setEncoding(self::ENCODING)
                      ->addFrom(self::FROM_ADRESS, self::FROM_NAME)
                      ->addTo($receiver)
                      ->setSubject($this->subject)
                      ->setBody($this->mimeMessage);

        $this->sender->send($this->message);
    }
    ....
    } 
?>

然后我这样称呼这个类:

$emailMng = EmailManager::getInstance();
$emailMng->load('reset', /* some params */ ));
$emailMng->sendTo($user->get('email'));

这真的很奇怪,因为即使使用也会发生这种情况$body->disposition = \Zend\Mime\Mime::DISPOSITION_INLINE;

4

1 回答 1

0

答案就在这里:在 ZF2 中发送带有附件的电子邮件 但没有 pdf 附件,如果您将消息合并并使用 multipart/alternative 创建一个新部分,它就可以工作。

对于这样一个常见的任务,这不是一个非常好的界面;)

于 2013-11-18T10:09:24.690 回答