Monday, 2 February 2015

Send Email from Gmail SMTP using Zend Framework

Send Email from Gmail SMTP using Zend Framework

In Website we need to send email on Registration, Forget , Order email to our Clients/Customer.
We want email should be deliver timely in inbox folder not in junk/spam folder.
For this we have to use SMTP Server.

GMAIL SMTP Server is free to use, if you have valid Gmail account.

Just the following code to send email from your Gmail Account.
/** Must fill Following detail * */
$yourGmailAccountUsername = 'youremail@gmail.com';
$yourGmailAccountPassword = '********';
$emailTo = 'arun.compute@gmail.com';
$emailToName = 'receiptsName';
/** Must fill Following detail * */

/** Optional Detail * */
$emailBody = 'This is body text';
$emailSubject = 'This is subject';
/** Optional Detail * */

try {
    $config = array('ssl' => 'tls',
        'auth' => 'login',
        'username' => $yourGmailAccountUsername,
        'password' => $yourGmailAccountPassword);

    $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);


    $mail = new Zend_Mail();

    $mail->setBodyHtml($emailBody);
    $mail->setFrom($yourGmailAccountUsername);
    $mail->addTo($emailTo, $emailToName);
    $mail->setSubject($emailSubject);
    if ($mail->send($transport)) {
        echo 'Sent successfully';
    } else {
        echo 'unable to send email';
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

If you get similar to following Error
Please log in via your web browser and then try again.
5.7.14 Please log in via your web browser and then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fr13sm3613053pdb.81

If Yes,
whether you have "turned on 2-Step Verification" OR Google need Verification that you want to use your gmail as SMTP credential.
you can fix this in couple of minutes using below link:
https://support.google.com/mail/answer/78754

If any issue, please comment below in comment box: