How to send Mail in PHP using SMTP with Gmail?

If you want to send E-mail using PHP with your Gmail then you can use SMTP because it is fast and secure then PHP built-in PHP mail function and it also working on localhost(xamp or wamp, etc).


So first, you download this file -: 


After Download, you can place it in your project folder and use it.
  •     require 'phpMailer/PHPMailerAutoload.php'; //path
  •     $mail = new PHPMailer;

  •     $email = sending@email.com; //email where you want to send

  •     $mail->isSMTP(); //for localhost not liveserver
  • //if you using latest version of PHP 
  • $mail->SMTPOptions = array(
  • 'ssl' => array(
  • 'verify_peer' => false,
  • 'verify_peer_name' => false,
  • 'allow_self_signed' => true
  • )
  • );

  •     $mail->Host = 'smtp.gmail.com';
  •     $mail->port = 587;
  •     $mail->SMTPAuth = true;
  •     $mail->SMTPSecure = 'tls';

  •     $mail->Username = 'your@gmail.com'; //your email id
  •     $mail->Password = 'youremailpassword'; //your email password

  •     $mail->setFrom('youremail', 'yourcompanyname');
  •     $mail->addAddress($email);
  •     $mail->addReplyTo('no-reply'); // if you want to reply by user then you can place your email id

  •     $mail->isHTML(true); //for html
  •     $mail->Subject = 'Subject'; //subject
  •     
  • $mail->Body = '<h1 align="center"> hello, your message body </h1>';

  • if(!$mail->send()) {
  •         //mail not send
  •         echo "Mail not send";
  •     }
  •     else {
  •        //mail send
  •         echo "Mail send";
  •     }



              No comments