php 使用 Zend Mail 发送邮件时出现问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3455674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:50:28  来源:igfitidea点击:

Problem when sending mail with Zend Mail?

phpzend-frameworkzend-mail

提问by Goles

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

我正在尝试使用 ZendMail 发送电子邮件(这个简单的脚本总结了它)

<?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.

我没有尝试使用 SMTP 来发送我的电子邮件,而且我遇到了一个不那么可怕的问题,但仍然是一个问题。

<?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:

编辑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);

采纳答案by Benjamin Cremer

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

正如你可以在堆栈跟踪看到Zend_Mail用途Zend_Mail_Transport_Sendmail为传输适配器。
因此,请确保您的系统上正在运行与sendmail 兼容的MTA(例如 Postfix)。

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

作为替代方案,您可以使用Zend_Mail_Transport_Smtp传输适配器并像这样使用外部 SMTP-Server

$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

编辑:对于您的第二个问题:a

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

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

should help.

应该有帮助。

回答by Ronn0

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

Zend_Mail 的另一个优点是它是可链接的,所以你可以这样做:

$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

不确定“chainable”是否是正确的词,但我希望你明白这一点。这只是一个免费提示。本杰明给出了答案(右)

回答by Hassan Ali Shahzad

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

另外,如果您想在 magento 中发送带有附件的邮件,请查看以下代码段

$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;
    }

回答by Hassan Ali Shahzad

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

更新的 Goles 答案需要在顶部添加'ssl' => 'tls'以避免错误

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