【问题标题】:Problem when sending mail with Zend Mail?使用 Zend Mail 发送邮件时出现问题?
【发布时间】:2011-03-28 04:59:05
【问题描述】:

我正在尝试使用 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('test@example.com', 'Mr Example');
$mail->addTo('contact@mypage.com', 'Mr Test');
$mail->setSubject('TestSubject');
$mail->send();
?>

但是我得到了这个堆栈跟踪:

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

编辑:

我没有尝试使用 SMTP 发送电子邮件,我遇到的问题不那么严重,但仍然是个问题。

<?php
require_once 'Zend/Mail.php';
$config = array('auth' => 'login',
                'username' => 'contact@mypage.com',
                '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('contact@mypage.com', 'Some Sender');
$mail->addTo('contact@mypage.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);
?>

这个 throw 就是这个错误,我真的不明白为什么:

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

编辑 2:

这是我最后的工作代码

require_once('Zend/Mail/Transport/Smtp.php');
require_once 'Zend/Mail.php';
$config = array('auth' => 'login',
                'username' => 'somemail@mysite.com',
                '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('somemail@mysite.com', 'Some Sender');
$mail->addTo('somemail@mysite.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);

【问题讨论】:

  • 更新的 Goles 答案需要在顶部添加 'ssl' => 'tls', 以避免错误,请参阅我的答案
  • 这太不可思议了;感谢更新;我将来也会为我的问题这样做。

标签: php zend-framework zend-mail


【解决方案1】:

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

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

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

编辑: 对于你的第二个问题:一个

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

应该有帮助。

【讨论】:

  • 我也在尝试使用 SMTP,(编辑了我的帖子),我得到了一个不那么可怕的错误,但仍然是一个错误。
  • 谢谢!,GMAIL 也需要 SSL,所以我添加了我的最终工作代码 :-)
【解决方案2】:

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

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

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

【讨论】:

【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    另外,如果您想使用带有附件的 magento 发送邮件,请查看以下 sn-p

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

    【讨论】:

      猜你喜欢
      • 2010-12-24
      • 2011-04-22
      • 2012-05-09
      • 2015-09-16
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      相关资源
      最近更新 更多