Sending email using the Zend Framework and PHP

I am working on a form in which when a user logs into their email account and clicks "Submit", the email will be sent to their email.

Everything worked out for me. It’s just that he is not sending an email to my account. Does anyone have any idea? Is there some kind of configuration that I forgot or something else?

This is a sample of 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'); } 
+6
php email zend-framework send
source share
4 answers

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){ //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(' 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. } 
+26
source share

First of all, I would switch to using Zend_Mail. Secondly, I would use a real mail account on the smtp server somewhere and send from it. Many times there are restrictions on sending from the server itself, but using the actual mail server usually fixes this.

+1
source share

There's a very useful screencast covering Zend_Mail, available on ZendCasts http://www.zendcasts.com/introduction-to-zend_mail/2010/02/

+1
source share

In the line $mail->setBody($message); change it to $mail->setBodyText($message);

+1
source share

All Articles