如何使用 Zend_Mail、sendmail 和 localhost 发送电子邮件?
我正在开发一个 zend 框架应用程序,其中包括一个简单的电子邮件功能。开发版本正在我的计算机上运行,该计算机运行 Ubuntu。生产版本将在生产服务器上运行。
当尝试向自己发送测试电子邮件时,我收到一条异常消息:“无法发送邮件”。不知道是环境问题还是代码问题。我没有使用传输,所以我认为它默认为 Zend_Mail_Transport_Sendmail
。这是我的代码:
public function sendtestAction()
{
$mail = new Zend_Mail();
$mail->setFrom('[email protected]', 'Test Email');
$mail->addTo('[email protected]', 'My Name');
$mail->setSubject('This is just a test.');
$mail->setBodyText('This is only a test.');
$mail->send();
}
更新: 我尝试了一种不同的方法,将 SMTP 传输设置为使用 localhost:
transport = new Zend_Mail_Transport_Smtp('localhost');
Zend_Mail::setDefaultTransport($transport);
这次我收到了不同的错误:“连接被拒绝” 不确定这意味着什么。也许我还没有设置什么?
更新:我想我没有安装/设置 SMTP 服务器。 本教程使我能够轻松运行 SMTP 服务器。现在上面的两个代码示例都可以工作了。
I'm developing a zend framework application that includes a simple email function. The development version is running on my computer, which is running Ubuntu. The production version is going to run on a production server.
When trying to send a test email to myself, I get an exception with the message: "Unable to send mail". I don't know if this is an environment issue, or a code issue. I'm not using a transport so I think it is defaulting to Zend_Mail_Transport_Sendmail
. Here's my code:
public function sendtestAction()
{
$mail = new Zend_Mail();
$mail->setFrom('[email protected]', 'Test Email');
$mail->addTo('[email protected]', 'My Name');
$mail->setSubject('This is just a test.');
$mail->setBodyText('This is only a test.');
$mail->send();
}
Update: I tried a different approach by setting the SMTP transport to use localhost:
transport = new Zend_Mail_Transport_Smtp('localhost');
Zend_Mail::setDefaultTransport($transport);
I got a different error this time: "Connection refused" Not sure what that means. Maybe I haven't set something up yet?
Update: I guess I didn't have an SMTP server installed/setup. This tutorial made it really easy for me to get an SMTP server up an running. Now both of the code samples above work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您需要配置一个 MTA,或者找到一个可以发送到的 MTA。 Ubuntu 桌面应该默认设置一个,可能是 exim 或 postfix,但如果你没有配置它,它就不太可能运行。
It sounds like you need to configure an MTA, or find one that you can send to. Ubuntu desktop should set one up by default, probably either exim or postfix, but if you haven't configured it, it will unlikely to be running.
如果您希望使用 sendmail(这是默认设置)并且 SMTP 不同,则不需要设置默认传输。
如果它不发送电子邮件,则表明您的服务器上的 sendmail 或 MTA 未安装/设置不正确。
You don't want to set the default transport if you wish to use sendmail (it is the default) and SMTP is different.
That it doesn't send the emails suggests that sendmail or the MTA on your server is not installed/not setup correctly.