Home » Php » php – How to send an email using Zend_Mail, sendmail, and localhost?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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.