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

Gol*_*les 16 php zend-framework 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();
?>
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

编辑:

我不是试图使用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);
?>
Run Code Online (Sandbox Code Playgroud)

抛出这个错误,我真的不明白为什么:

Fatal error:
Class 'Zend_Mail_Transport_Smtp' not found in /var/www/hexreaction/mail/index3.php on line 7
Run Code Online (Sandbox Code Playgroud)

编辑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);
Run Code Online (Sandbox Code Playgroud)

Ben*_*mer 16

正如你可以在堆栈跟踪看到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);
Run Code Online (Sandbox Code Playgroud)

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

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

应该有所帮助