I recommend using Zend_Mail instead of mail() . It handles a lot of things automatically and works just fine.
Do you have an SMTP server? Attempting to send mail without its own SMTP server may result in mail not being sent.
This is what I use to send emails using Zend_Mail and Gmail:
In Bootstrap.php , I configure the default mail transport:
protected function _initMail() { try { $config = array( 'auth' => 'login', 'username' => ' username@gmail.com ', 'password' => 'password', 'ssl' => 'tls', 'port' => 587 ); $mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config); Zend_Mail::setDefaultTransport($mailTransport); } catch (Zend_Exception $e){
Then, to send an email, I use the following code:
//Prepare email $mail = new Zend_Mail(); $mail->addTo($email); $mail->setSubject($subject); $mail->setBody($message); $mail->setFrom(' username@gmail.com ', 'User Name'); //Send it! $sent = true; try { $mail->send(); } catch (Exception $e){ $sent = false; } //Do stuff (display error message, log it, redirect user, etc) if($sent){ //Mail was sent successfully. } else { //Mail failed to send. }
Matt way
source share