Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 676
Andrew Avatar asked Oct 27 '09 22:10

Andrew


People also ask

Can I send email using localhost?

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost. For example, you can configure C:\xampp\php\php. ini and c:\xampp\sendmail\sendmail.

Can I send email from localhost PHP?

The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.


2 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.

like image 179
staticsan Avatar answered Sep 19 '22 16:09

staticsan


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.

like image 31
David Snabel-Caunt Avatar answered Sep 21 '22 16:09

David Snabel-Caunt