How to Send Mail from Gmail.com  with PhpMailer Example 

Today we are going to send mail from google account with the help of php and PHPMailer Library.
I have attached code below download and extract in your server root directory.

  Download Working code here

It has four files

  1. index.php
  2. phpmail.php
  3. class.phpmailer.php
  4. class.smtp.php 
First file index.php is a form for sending mail.

index.php 
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Send Mail Using PhpMailer</title>
    <!-- Bootstrap -->
   <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body><div class="row">
   <div class="col-sm-2"></div>
   <div class="col-sm-8">
       <h1>Send Mail From Gmail.com</h1>
<form action="phpmail.php" method="post">
<br>
<input type="text" name="from" size="120" placeholder="Youremail@gmail.com"><br>
<br>
<input type="text" name="password" size="120" placeholder="Password of your email "><br>
<br>
<input type="text" name="port" size="120" placeholder="port number 587 in case of gmail"><br>
<br>
<input type="text" name="to" size="120" placeholder="recipent email address yourfriend@gmail.com"><br>
<br>
<input type="text" name="subject" size="120" placeholder="Subject of mail hii friend"><br>
<br>
<textarea  name="message" rows="20" cols="122">Your Message</textarea ><br>
<input type="submit" value="Send email" class="btn btn-primary"/>
</form>
   <div class="col-sm-2"></div>
  </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   <!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
  </body>
</html>
In the first text field mention your email address from where you want to send mail.
Second column password of your mail .
Third Port Number ,When you are sending mail from gmail use port number 587.
forth Recipient email address
Fifth message subject
In the text area write message.

phpmail.php

<?php
require_once "class.phpmailer.php";
require_once "class.smtp.php";
//if ($_SERVER ["REQUEST_METHOD"]=="POST"){
if ($_SERVER ["REQUEST_METHOD"]=="POST"){
 $from=$_POST['from'];
 $password=$_POST['password'];
 $port=$_POST['port'];
 $to=$_POST['to'];
 $subject=$_POST['subject'];
 $message=$_POST['message'];
 $mail=new PHPMailer;
 $mail->SMTPDebug = 0;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username =$from;                 // dtlmailtestacc@gmail.com SMTP username
        $mail->Password = $password;                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = $port;   //port no 587 when you use gmail and tls  
 $mail->setFrom($from, 'your name');
 $mail->addAddress($to,"recipent name");
 $mail->AddReplyTo($from,"yourname");
 $mail->WordWrap=50;
 $mail->isHTML(true);
 $mail->Subject=$subject;
 $mail->Body=$message;
 $mail->AltBody="This is body in the palin text for non html mail clients";
 if(!$mail->send()){
  echo "Mailer error:".$mail->ErrorInfo;
  }else{
   echo"Messgae has been sent successfully";
   }
}
?>


No comments:

| Designed by Harhiktech