Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email using Zend Framework and PHP

I working on a form whereby when the user enter in their email account and click on send, an email will be sent to their email account.

I have everything worked out. Just that it doesnt send the email to my account. Anyone have any ideas? Is there a configuration that I left out or something?

This is the sample from my controller:

public function retrieveemailAction(){

    $users = new Users();
    $email = $_POST['email'];                
    $view = Zend_Registry::get('view'); 

    if($users->checkEmail($_POST['email'])) {

        // The Subject
        $subject = "Email Test";

        // The message
        $message = "this is a test";            

        // Send email
        // Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
        // Use if command to display email message status
        if(mail($email, $subject, $message, $headers)) {
            $view->operation = 'true';
        }            
    } else {
         $view->operation = 'false';
    }

    $view->render('retrieve.tpl');
}
like image 993
Ryan Avatar asked Feb 17 '10 03:02

Ryan


People also ask

Is Zend framework in PHP?

Zend is an open source PHP framework. It is pure object-oriented and built around the MVC design pattern. Zend framework contains collection of PHP packages which can be used to develop web applications and services. Zend was started by Andi Gutmans and Zeev Suraski.

What is the use of Zend framework in PHP?

Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.


1 Answers

I recommend you use Zend_Mail instead of mail(). It handles a lot of stuff automatically and just works great.

Do you have a SMTP server? Trying to send mail without your own SMTP server could be causing the mail to not be sent.

This is what I use for sending mails using Zend_Mail and Gmail:

In Bootstrap.php, I configure a default mail transport:

protected function _initMail()
{
    try {
        $config = array(
            'auth' => 'login',
            'username' => '[email protected]',
            'password' => 'password',
            'ssl' => 'tls',
            'port' => 587
        );

        $mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
        Zend_Mail::setDefaultTransport($mailTransport);
    } catch (Zend_Exception $e){
        //Do something with exception
    }
}

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('[email protected]', '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.
}
like image 76
Matt Way Avatar answered Oct 16 '22 13:10

Matt Way