使用Zend Mail发送邮件时出现问题? [英] Problem when sending mail with Zend Mail?

查看:150
本文介绍了使用Zend Mail发送邮件时出现问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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);
?>

抛出该错误是我的错误,我真的不明白为什么:

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

这是我的最终工作代码

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

解决方案

正如您在堆栈跟踪中所看到的,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');

应该有帮助.

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

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

解决方案

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.

这篇关于使用Zend Mail发送邮件时出现问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆