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?


define('MR_MAIL_HOST','smtp.sendgrid.net');
define('MR_MAIL_PORT','587');//457,25
define('MR_MAIL_USERNAME','username');  
define('MR_MAIL_PASSWORD','password');  

$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,
    ),
));
$transport->setOptions($options);

$mail = new Mail\Message();
$mail->setFrom('from@web-technology-experts-notes.in');
$mail->setSubject('This is simple subject');
$mail->setBody('this is html body text');
$mail->addTo('to@web-technology-experts-notes.in');
$transport->send($mail);

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 = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // 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('from@example.com', 'Mailer');

    //Recipients
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

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

    //Content
    $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';

    $mail->send();
    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 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:



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 gmail.com OR yahoo.com OR hotmail.com 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.
  3. SMTP Credentials like SMTP USRNAME, SMTP PASSWORD, SMTP HOST, SMTP PORT



Script Code
    $toEmail='toemailaddress@domain.com'; //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);
    Zend_Mail::setDefaultTransport($tr);
    $mail = new Zend_Mail('UTF-8');
    $mail->setFrom(MAIL_SENDER_EMAIL, MAIL_SENDER_NAME);
    $mail->addTo($toEmail);
    $mail->setSubject($subject);
    $mail->setBodyHtml($htmlEmail);


    //Attachment Email
    //Get the contents of csv file
    $csvContent=  file_get_contents('http://example.com/projects/export-csv.csv');
    $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
    $mail->addAttachment($at);

    //Send Email
    $mail->send(); 




Question: How to send image as attachment in Email?
Following are attachment code.
$myImage=  file_get_contents('http://example.com/images/powered-by-georama-dark.png');
$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'; 

$mail->addAttachment($at);



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);
            }
            unset($flattenedParts[$prefix.$index]->parts);
        }
        $index++;
    }

    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
            break;
        case 3:
            return base64_decode($data);  // BASE64
            break;
        case 4:
            return quoted_printable_decode($data); // QUOTED_PRINTABLE
            break;
        case 5:
            return $data; // OTHER
            break;
    }   
   
}


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 = 'mygmail@gmail.com'; //Gmail address
$password = 'gma12#@c'; //Gmail password
error_reporting(0);

 $bodyId=35; //This is message Id

 //Read Message from indebox
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
   
   //Read Message from Trash
    // $hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Trash';
 
   //Read Message from Sent
 //$hostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail';




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

$message='';
if(!empty($structure->parts)){
 
 $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
      
   break;
    
   case 1:
    // multi-part headers, can ignore
    
   break;
   case 2:
    // attached message headers, can ignore
   break;
    
   case 3: // application
   case 4: // audio
   case 5: // image
   case 6: // video
   case 7: // other
   //pr($part);
   //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)) {
      mkdir($folder);
     }
     $fp = fopen("./" . $folder . "/" . $messageNumber . "-" . $filename, "w+");
     fwrite($fp, $filename);
     fclose($fp);
     // now do something with the attachment, e.g. save it somewhere
     $message.='

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