Problem when sending mail with Zend Mail?
Asked Answered
B

4

18

I'm trying to send an e-mail with ZendMail ( this simple script sums it up )

<?php
require_once 'Zend/Mail.php';

$mail = new Zend_Mail();
$mail->setBodyText('My Nice Test Text');
$mail->setBodyHtml('My Nice Test Text');
$mail->setFrom('[email protected]', 'Mr Example');
$mail->addTo('[email protected]', 'Mr Test');
$mail->setSubject('TestSubject');
$mail->send();
?>

However I get this stack trace:

Fatal error:
Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable to send mail. ' in /usr/share/php/libzend-framework-php/Zend/Mail/Transport/Sendmail.php:137

Stack trace:
#0 /usr/share/php/libzend-framework-php/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Sendmail->_sendMail()
#1 /usr/share/php/libzend-framework-php/Zend/Mail.php(1178): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#2 /var/www/hexreaction/mail/index2.php(11): Zend_Mail->send()
#3 {main} thrown in /usr/share/php/libzend-framework-php/Zend/Mail/Transport/Sendmail.php on line 137

EDIT:

I'm not trying to use SMTP to send my e-mail and I'm having a less horrible problem, but still a problem.

<?php
require_once 'Zend/Mail.php';
$config = array('auth' => 'login',
                'username' => '[email protected]',
                'password' => 'secretpass');

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('[email protected]', 'Some Sender');
$mail->addTo('[email protected]', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
?>

This throw's this error, I don't really get why:

Fatal error:
Class 'Zend_Mail_Transport_Smtp' not found in /var/www/hexreaction/mail/index3.php on line 7

EDIT 2:

This is my final working code

require_once('Zend/Mail/Transport/Smtp.php');
require_once 'Zend/Mail.php';
$config = array('auth' => 'login',
                'username' => '[email protected]',
                'password' => 'somepass',
                'ssl' => 'tls');

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('[email protected]', 'Some Sender');
$mail->addTo('[email protected]', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
Bahamas answered 11/8, 2010 at 6:4 Comment(2)
Updated Goles answer need to add 'ssl' => 'tls', at top to avoid error see my answerAyana
this is incredible; thanks for the update; I'm gonna do that for my questions in the future as well.Boss
R
17

As you can see in the Stack trace Zend_Mail uses Zend_Mail_Transport_Sendmail as transport adapter.
So make sure a sendmail-compatible MTA (e.g. Postfix) is running on your system.

As an alternative you could use the Zend_Mail_Transport_Smtp transport adapter and use an external SMTP-Server like so

$tr = new Zend_Mail_Transport_Smtp('mail.example.com', array(
    'auth'     => 'login',
    'username' => $username,
    'password' => $password,
    'port'     => $port,
));
Zend_Mail::setDefaultTransport($tr);

Edit: For your 2nd Problem: a

require_once('Zend/Mail/Transport/Smtp.php');

should help.

Romaine answered 11/8, 2010 at 6:13 Comment(2)
I'm trying to use SMTP too, ( edited my post ) , and I'm getting a less horrible error, but still an error.Bahamas
Thanks!, also GMAIL expects SSL, so I added my final working code :-)Bahamas
A
3

Another great thing on Zend_Mail is that's chainable, so you can do this:

$mail = new Zend_Mail();
$mail->setBodyText('My Nice Test Text')
     ->setBodyHtml('My Nice Test Text')
     ->setFrom('[email protected]', 'Mr Example')
     ->addTo('[email protected]', 'Mr Test')
     ->setSubject('TestSubject')
     ->send();

Don't know for sure if 'chainable' is the right word, but I hope you got the point. This is just a free tip. The answer is given (right) by Benjamin

All answered 11/8, 2010 at 8:33 Comment(1)
See MethodChaining / Fluent Interface for further information martinfowler.com/dslwip/MethodChaining.htmlRomaine
A
1

Updated Goles answer need to add 'ssl' => 'tls', at top to avoid errors

require_once('Zend/Mail/Transport/Smtp.php');
require_once 'Zend/Mail.php';
$config = array(
                'ssl' => 'tls',
                'auth' => 'login',
                'username' => '[email protected]',
                'password' => 'somepass'
                );

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('[email protected]', 'Some Sender');
$mail->addTo('[email protected]', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
Ayana answered 7/7, 2016 at 16:29 Comment(0)
A
1

Also if you want to seand mail in magento with attachment have a look on the following snippet

$config = array(
                'ssl' => 'tls',
                'auth' => 'login',
                'username' => '[email protected]',
                'password' => 'yourPassword'
                );

        $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);


        $bodytext = "Please see attachment for customers detail.";
        $mail = new Zend_Mail();
        $mail->setFrom('[email protected]','Hassan');
        $mail->addTo('[email protected]' );
        $mail->setSubject('Customers info');
        $mail->setBodyText($bodytext);

        $file = $mail->createAttachment(file_get_contents($path.$fileName));
        $file ->type        = 'text/csv';
        $file ->disposition = Zend_Mime::DISPOSITION_INLINE;
        $file ->encoding    = Zend_Mime::ENCODING_BASE64;
        $file ->filename    = $fileName;

        if(!$mail->send($transport)) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
        echo "File Completed";exit;
    }
Ayana answered 7/7, 2016 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.