3

I am sending emails using the Zend_Mail_Transport_Smtp. The sending part works fine, however, I am struggling trying to copy the email to the 'Sent' folder of the sending email account. When generating the message from the Zend_Mail, I keep getting Call to a member function getContent() on a non-object.

Here is what I am doing:

$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');

I'm not sure if I am missing anything or doing this completely the wrong way. Any help would be great.

Emtres
  • 31
  • 2
  • Can't see the `getContent()` method int your code and that's where your problem is. There's something like `$obj->getContent()` and the **$obj** for whatever reason is not an object. – Adrian World Mar 19 '13 at 13:19
  • The exception for getContent() is thrown in Zend_Mime_Message.php (which is included in the Zend_Mail.php). It appears that when I create and send the Zend_Mail message, Zend_Mail doesn't actually create the mime parts. After $mail->send(); when I call '$mail->getPartCount()' it returns 0. – Emtres Mar 19 '13 at 13:56
  • This question was also unanswered in http://stackoverflow.com/questions/10853413/zend-mail-generatemessage-for-further-saving-in-imap-folder. Is there anyway that I can get the email parts as strings? – Emtres Mar 19 '13 at 14:39
  • 1
    There's even open bug for this. Looking at Zend_Mail whatever you expect to get with `generateMessage()` has to be added first with `addPart()` as a Zend_Mime_Part object. The only exception are attachments. Cannot see the reasoning behind all this tho. – Adrian World Mar 19 '13 at 14:55
  • Ok, Ive found a way round the problem. My solution may not be the most elegant, but at least its working solution. I ended up using Swift Mailer- which is very elegant at (it only deals with sending emails- not the IMAP stuff) sending emails. Once you create a message using swift- call the toString() method- then you can use with Zend_mail's appendMessage(). I got the solution from rixtsa's post in http://php.net/manual/en/function.imap-append.php. Maybe ZF2 doesnt have this issue, but if your on ZF1 you can use this method. – Emtres Mar 22 '13 at 11:11
  • FYI - ZF2 has a toString() method on the ``Zend\Mail\Message`` object which gives you what you want. – AlasdairC Nov 03 '14 at 17:59

2 Answers2

0

Ok, Ive found a way round the problem. Zend_mail is buggy/incomplete in that it doesnt actually create a string from the Zend_mail object (at least with ZF1 anyway).

My solution may not be the most elegant, but at least its working solution. I ended up using Swift Mailer- which is very elegant at (it only deals with sending emails- not the IMAP stuff) sending emails. Once you create a message using swift- call the toString() method- then you can use with Zend_mail's appendMessage(). I got the solution from rixtsa's post in http://php.net/manual/en/function.imap-append.php. Maybe ZF2 doesnt have this issue, but if your on ZF1 you can use this method.

 // 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);

I hope someone can find a better and more elegant solution- or even better- the bug gets sorted out. But for now, after a lot of reading and searching, I ended up using Swift Mailer to bridge the gap.

Emtres
  • 31
  • 2
0

You could build the message string by yourself

//...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');