Ask Question

Menu

Need MCA Or B.tech College Project Contact Us: +91 9437911966 (Whatsapp) Students Tutorial


How to send email In PHP


For sending mail in PHP here we use two file :

PHPMailer libraries need for send email.

So First of all download the PHPMailer library from this link :Download

Note :After run the code if it give error like “SMTP Error: Could not authenticate” then click on the below link and turn on the less secure apps.

https://www.google.com/settings/u/0/security/lesssecureapps

index.php

<!DOCTYPE html>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="process.php">
To:<br>
  <input type="email" name="email"><br>
 Subject:<br>
  <input type="text" name="subject"><br><br>
 <textarea rows="4" cols="40" name="message">
</textarea> 
 <input type="submit" name="btn_send">
               
           </form>
  </body>
</html>

process.php

<?php
  require_once 'mailer/class.phpmailer.php'; 
  // creates object
  $mail = new PHPMailer(true); 
  if(isset($_POST['btn_send']))
  {
   $email      = strip_tags($_POST['email']);
   $subject    = strip_tags($_POST['subject']);
   $text_message    = "Hello";      
   $message  = strip_tags($_POST['message']);
 try
   {
    $mail->IsSMTP(); 
    $mail->isHTML(true);
    $mail->SMTPDebug  = 0;                     
    $mail->SMTPAuth   = true;                  
    $mail->SMTPSecure = "ssl";                 
    $mail->Host       = "smtp.gmail.com";      
    $mail->Port        = '465';             
    $mail->AddAddress($email);
    $mail->Username   ="divyasundarsahu@gmail.com";  
    $mail->Password   ="gmail-password";            
    $mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu');
    $mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu");
    $mail->Subject    = $subject;
    $mail->Body    = $message;
    $mail->AltBody    = $message;
     
    if($mail->Send())
    {
     
     $msg = "Hi, Your mail successfully sent to".$email." ";
     
    }
   }
   catch(phpmailerException $ex)
   {
    $msg = "<div class='alert alert-warning'>".$ex->errorMessage()."</div>";
   }
  } 
  
?>

Output

To:

Subject:







Buy Server in very low Price