How to send an email using Zend_Mail, sendmail, and localhost?

Andrew picture Andrew · Oct 27, 2009 · Viewed 8.6k times · Source

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.

Answer

David Snabel-Caunt picture David Snabel-Caunt · Oct 28, 2009

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.