Showing posts with label Email. Show all posts
Showing posts with label Email. Show all posts

Thursday 6 June 2019

How to send email from zend framework 3?

How to send email from zend framework 3?

How to send email from zend framework3?


$transport = new Mail\Transport\Smtp();
$options   = new Mail\Transport\SmtpOptions(array(
    'name'              => 'emailName',
    'host'              => MR_MAIL_HOST,
    'connection_class'  => 'login',
    'connection_config' => array(
        'username' => (MR_MAIL_USERNAME),
        'password' => (MR_MAIL_PASSWORD),
         'port' => MR_MAIL_PORT,

$mail = new Mail\Message();
$mail->setSubject('This is simple subject');
$mail->setBody('this is html body text');

Monday 29 January 2018

phpmailer gmail - PHP MAILER setting to send email through Gmail

PHP MAILER setting to send email through Gmail

Step 1:
Download a recent version of PHPMailer.

Step 1:
Use Following code to send an email from Gmail Account.

// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = ';';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Email Sender
    $mail->setFrom('', 'Mailer');

    $mail->addAddress('', 'Joe User');     // Add a recipient
    $mail->addAddress('');               // Name is optional
    $mail->addReplyTo('', 'Information');

    //Attachments (IF there is attachment then only include)
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    $mail->isHTML(true);                                  // Set email format to HTML (true) 
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;

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 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:

If any issue, please comment below in comment box:

Wednesday 6 September 2017

Free Testing Email Address Without Phone Number

Free Testing Email Address Without verification

Need Email Account in 1 Second, For this
  1. No Registration Required
  2. No Primary Email Required
  3. No Phone Number Required
  4. Thousand of Free Email address is Ready!
During the website development, many times we need to check the Registration Process, Forget Password and Newsletter Functionalists  etc, to Perform all these things we need number of number address. 

Its very hard to do registration in OR OR everytime. 

You can get email address and at Free of cost within a second.
Following are list of websites who provide email account and at free of cost.

Saturday 2 September 2017

How to attach a CSV file to email - Zend

How to attach a CSV file to email - Zend

To send CSV in email, you must have following.
  1. Receiver email address
  2. CSV file that want to send as attachment.

Script Code
    $toEmail=''; //Receiver email address
    $subject='Email with attachment';//Email subject
    $htmlEmail='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eget nibh consectetur, mattis mauris vitae, aliquam orci. Nunc pretium tristique metus eget pharetra. Ut condimentum purus in ex varius, quis bibendum ex venenatis. Praesent leo lacus, feugiat vel neque ac, finibus ultricies velit. Nunc venenatis sem eu ullamcorper interdum. Fusce a pulvinar risus, quis viverra nulla. Donec tempus ex congue mauris elementum, eget fermentum orci dapibus. Proin erat lacus, lacinia eu volutpat quis, sagittis suscipit erat. Maecenas nulla enim, tempor ac urna id, tincidunt consectetur metus. Maecenas tincidunt, tortor quis sodales rutrum, purus mi accumsan libero, id maximus purus leo id ante. In posuere erat ipsum, sed mattis est placerat eget. Phasellus consectetur diam augue, sed luctus libero consequat quis. Praesent vel mauris nec ligula malesuada faucibus. Suspendisse ultrices nunc velit, in ultricies nibh tempus id. Integer quis nisi ipsum.'
            . 'This is html email.';

    //Email Configuration
    $config = array(
        'auth' => 'login',
        'ssl' => 'tls',
        'port' => '587',
        'username' => 'MAIL_USERNAME',
        'password' => 'MAIL_PASSWORD'
    $tr = new Zend_Mail_Transport_Smtp('MAIL_HOST', $config);
    $mail = new Zend_Mail('UTF-8');

    //Attachment Email
    //Get the contents of csv file
    $csvContent=  file_get_contents('');
    $at = new Zend_Mime_Part($csvContent);
    $at->type        = 'text/csv';
    $at->disposition = Zend_Mime::DISPOSITION_INLINE;
    $at->encoding    = Zend_Mime::ENCODING_BASE64;
    $at->filename    = 'csv-file.csv'; 

    //Attach the attachment in Email

    //Send Email

Question: How to send image as attachment in Email?
Following are attachment code.
$myImage=  file_get_contents('');
$at = new Zend_Mime_Part($myImage);
$at->type        = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding    = Zend_Mime::ENCODING_BASE64;
$at->filename    = 'imagename.gif'; 


Monday 13 March 2017

IMAP Gmail read messages details from inbox

IMAP Gmail read messages details

Check that IMAP is turned on
If Not, Please enable.

Library Code

/** functions used for getting message **/
function flattenParts($messageParts, $flattenedParts = array(), $prefix = '', $index = 1, $fullPrefix = true) {

    foreach($messageParts as $part) {
        $flattenedParts[$prefix.$index] = $part;
        if(isset($part->parts)) {
            if($part->type == 2) {
                $flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix.$index.'.', 0, false);
            elseif($fullPrefix) {
                $flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix.$index.'.');
            else {
                $flattenedParts = flattenParts($part->parts, $flattenedParts, $prefix);

    return $flattenedParts;

function getPart($con, $messageNumber, $partNumber, $encoding) {
    $data = imap_fetchbody($con, $messageNumber, $partNumber);
    switch($encoding) {
        case 0:
            return $data;
            break; // 7BIT
        case 1:
            return $data;
            break;// 8BIT
        case 2:
            return $data; // BINARY
        case 3:
            return base64_decode($data);  // BASE64
        case 4:
            return quoted_printable_decode($data); // QUOTED_PRINTABLE
        case 5:
            return $data; // OTHER

function getFilenameFromPart($part) {

    $filename = '';
    if($part->ifdparameters) {
        foreach($part->dparameters as $object) {
            if(strtolower($object->attribute) == 'filename') {
                $filename = $object->value;

    if(!$filename && $part->ifparameters) {
        foreach($part->parameters as $object) {
            if(strtolower($object->attribute) == 'name') {
                $filename = $object->value;
    return $filename;

/** functions used for getting message **/

How to read specific message
$username = ''; //Gmail address
$password = 'gma12#@c'; //Gmail password

 $bodyId=35; //This is message Id

 //Read Message from indebox
    $hostname = '{}INBOX';
   //Read Message from Trash
    // $hostname = '{}[Gmail]/Trash';
   //Read Message from Sent
 //$hostname = '{}[Gmail]/Sent Mail';

$con = imap_open($hostname, $username, $password);
$messageNumber = $bodyId;
$structure = imap_fetchstructure($con, $messageNumber);

 $flattenedParts = flattenParts($structure->parts); 
 foreach($flattenedParts as $partNumber => $part) {  
  switch($part->type) {
   case 0:
    // the HTML or plain text part of the email
    $message = getPart($con, $messageNumber, $partNumber, $part->encoding);
    // now do something with the message, e.g. render it
   case 1:
    // multi-part headers, can ignore
   case 2:
    // attached message headers, can ignore
   case 3: // application
   case 4: // audio
   case 5: // image
   case 6: // video
   case 7: // other
   //echo "
    $filename = getFilenameFromPart($part);
    if($filename) {
     // it's an attachment
     $attachment = getPart($con, $messageNumber, $partNumber, $part->encoding);
        if (empty($filename))
      $filename = $filename;
     if (empty($filename))
      $filename = time() . ".dat";
     $folder = "attachment";
     if (!is_dir($folder)) {
     $fp = fopen("./" . $folder . "/" . $messageNumber . "-" . $filename, "w+");
     fwrite($fp, $filename);
     // now do something with the attachment, e.g. save it somewhere

' . $messageNumber . '-' . $filename . '
'; } break; } } }else{ $message = getPart($con, $messageNumber, 1, $structure->encoding); } if($message){ $message=$message; } echo $message; imap_close($con, CL_EXPUNGE);