3

我正在使用Zend_Mail_Transport_Smtp. 发送部分工作正常,但是,我正在努力尝试将电子邮件复制到发送电子邮件帐户的“已发送”文件夹。从 Zend_Mail 生成消息时,我不断收到Call to a member function getContent() on a non-object.

这是我正在做的事情:

$config = array(
            'auth' => 'login',
            'username' => $from,
            'password' => $password);

$transport = new Zend_Mail_Transport_Smtp('smtp.123-reg.co.uk', $config);
Zend_Mail::setDefaultTransport($transport);
$mail = new Zend_Mail('utf-8');

$mail->addTo('foo@bar.com');
$mail->setSubject('Test');
$mail->setFrom('baz@bar.com', 'Baz');
$mail->setBodyText('This is the email');

$mail->send();

**$message = $mail->generateMessage(); <----- here is the problem**

*This is where I would append the message to the sent folder.*
$mail = new Zend_Mail_Storage_Imap
            array('host' => 'imap.123-reg.co.uk',
            'user' => 'baz@bar.com',
            'password' => 'p'
        ));
$mail->appendMessage($message,'Sent');

我不确定我是否遗漏了任何东西或完全错误地这样做。任何帮助都会很棒。

4

2 回答 2

0

好的,我找到了解决问题的方法。Zend_mail 是错误的/不完整的,因为它实际上并没有从 Zend_mail 对象创建一个字符串(至少在 ZF1 中是这样)。

我的解决方案可能不是最优雅的,但至少是它的工作解决方案。我最终使用了 Swift Mailer——它在发送电子邮件方面非常优雅(它只处理发送电子邮件——而不是 IMAP 的东西)。一旦你使用 swift 创建了一条消息——调用 toString() 方法——那么你就可以使用 Zend_mail 的 appendMessage()。我从 rixtsa 在http://php.net/manual/en/function.imap-append.php的帖子中得到了解决方案。也许ZF2没有这个问题,但是如果你在ZF1上你可以使用这个方法。

 // create the message
        $message = Swift_Message::newInstance()
                ->setSubject('The subject')
                ->setFrom(array('a@b.com'=> 'Alfa Beta'))
                ->setTo(array('recipient1@r.com','recipient2@r.com'))
                ->setBody('The body is here= could be')
                ->addPart('<q>If you want html body use this method/q>', 'text/html')
        ;
   //send the message
        $transport = Swift_SmtpTransport::newInstance('smtp.123-reg.co.uk', 25)
                ->setUsername($user)
                ->setPassword($password)
        ;
    $mailer = Swift_Mailer::newInstance($transport);
    $result = $mailer->send($message);

   // generate the string from the message
   $msg = $message->toString();

   // use the string with Zend_Mail_Storage_Imap's appendMessage()
   $mail = new Zend_Mail_Storage_Imap(
                    array('host' => 'imap.123-reg.co.uk',
                        'user' => $user,
                        'password' => $password
            ));
    $mail->selectFolder('Sent');
    $mail->appendMessage($msg);

我希望有人能找到一个更好、更优雅的解决方案——甚至更好——解决这个错误。但是现在,经过大量阅读和搜索,我最终使用 Swift Mailer 来弥补差距。

于 2013-03-22T11:23:03.260 回答
0

您可以自己构建消息字符串

//...send the e-mail, then copy it on Sent folder using this code:
$mail = new Zend_Mail_Storage_Imap(array(
        'host'     => 'imap.123-reg.co.uk',
        'user'     => 'baz@bar.com',
        'password' => 'p'
));
$mail->appendMessage($transport->header . Zend_Mime::LINEEND . $transport->body,'Sent');
于 2015-10-22T12:14:07.450 回答