ZF2邮件 - 作为附件发送的HTML

时间:2013-07-23 09:24:50

标签: email zend-framework2

我面对一个奇怪的错误:
我使用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;

也会发生这种情况

1 个答案:

答案 0 :(得分:0)

答案在这里:send email with attached files in ZF2 但是如果没有pdf附件,如果你将消息组合起来并使用multipart / alternative创建一个新的部分就可以了。

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