Problem when sending mail with Zend Mail?

…衆ロ難τιáo~ 提交于 2019-11-30 11:37:59
Benjamin Cremer

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.

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('test@example.com', 'Mr Example')
     ->addTo('contact@mypage.com', '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

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' => 'hassanalishahzad@gmail.com',
                '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('noreply@yourdomain.com','Hassan');
        $mail->addTo('xyz@yourdomain.com' );
        $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;
    }

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' => 'somemail@mysite.com',
                '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('somemail@mysite.com', 'Some Sender');
$mail->addTo('somemail@mysite.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!