Example : How to send email in Php using mail() fucnction [with Static data].

NB: To send email through mail() function  in Php, first of all re-configured these files as mention below -

(i) Php.ini file :[Open Xampp folder in C drive - open php folder - open php.ini file]

  [in 'mail function' area of the php.ini file, set]
     http://php.net/smtp
     SMTP=smtp.gmail.com
     http://php.net/smtp-port
     smtp_port=587
     sendmail_from = type email address of sender here such as [email protected](optional)
     sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

(ii) Sendmail.ini file :[Open Xampp folder in C drive - open sendmail folder - open sendmail.ini file]

   [in 'sendmail'area of the sendmail.ini file,set]

     smtp_server=smtp.gmail.com
     smtp_port=587
     smtp_ssl=auto

     error_logfile=error.log
     auth_username=type email address of sender here such as [email protected]
     auth_password=type password of mention email

     force_sender=type email address of sender here such as [email protected](optional)


(iii) Change in Sender gmail :

    Open sender gmail account - go to 'Manage your google account' option present signout pop box(right top logged in     
    icon) - choose 'Securty' menu from appeared left sidebar menu - make 'On' 'Less secure app access' option from  
    bottom area of page.) 

------------------------------------------------------------
Page.php [After above 3 settings, Execute this php file using Xampp Server Application, result obtains.]
------------------------------------------------------------

<?php 

  $receiver = "Type receiver email here";
  $subject = "Type Subject of email here" ; 
  $body = "Type message here";

  $sender = "From : Type sender email here";

     if (mail ($receiver , $subject , $body , $sender)) 
      { 
	echo " Email sent successfully to $receiver " ;
      }
     else
      {
	echo "Sorry, Failed while sending mail!";
      }
?>

Example : How to send email in Php using mail() fucnction [with Dynamic data].


------------------------------------------------------------
Page1.php [After making 3 settings which is necessary as mention in above example, Execute this php file using Xampp Server Application, result obtains.]
------------------------------------------------------------

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>www.Codershelpline.com</title>      
      <!-- bootstrap cdn link -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
   </head>
   <body>
      <div class="container">
         <div class="row">
            <div class="col-md-4 offset-md-4 mail-form">
               <h2 class="text-center">
                  Send Message through Php mail()
               </h2>
               
               <!-- starting php code -->
               <?php
                  
                  $recipient = "";
                  
                  if(isset($_POST['send'])){                      
                     $recipient = $_POST['email'];
                     $subject = $_POST['subject'];
                     $message = $_POST['message'];
                     $sender = "From: [email protected]";//optional, sender email address
                     //if user leave empty field among one of them
                     if(empty($recipient) || empty($subject) || empty($message)){
                         ?>
               <!-- display an alert message if one of them field is empty -->
               <div class="alert alert-danger text-center">
                  <?php echo "All inputs are required!" ?>
               </div>
               <?php
                  }else{
                      // PHP function to send mail
                     if(mail($recipient, $subject, $message, $sender)){
                      ?>
               <!-- display a success message if once mail sent sucessfully -->
               <div class="alert alert-success text-center">
                  <?php echo "Your mail successfully sent to $recipient"?>
               </div>

               <?php
                  $recipient = "";
                  }else{
                   ?>
               <!-- display an alert message if somehow mail can't be sent -->
               <div class="alert alert-danger text-center">
                  <?php echo "Failed while sending your mail!" ?>
               </div>
               <?php
                  }
                  }
                  }
                  ?> <!-- end of php code -->
               <form action="" method="POST">
                  <div class="form-group">
                     <input class="form-control" name="email" type="email" placeholder="Recipients" value="<?php echo $recipient ?>">
                  </div>
                  <div class="form-group">
                     <input class="form-control" name="subject" type="text" placeholder="Subject">
                  </div>
                  <div class="form-group">
                     
                     <textarea cols="30" rows="5" class="form-control textarea" name="message" placeholder="Compose your message.."></textarea>
                  </div>
                  <div class="form-group">
                     <input class="form-control button btn-primary" type="submit" name="send" value="Send" placeholder="Subject">
                  </div>
               </form>
            </div>
         </div>
      </div>
   </body>
</html>

Example : How to send email in Php by mail() function using PHPMailer library file [with Dynamic data].

NB : First of all download Phpmailer library file(from https://github.com/PHPMailer/PHPMailer) - Extract this file - Put this extract file in the folder where page2.php file is stored. 


page2.php [Execute this php file using Xampp Server Application, result obtains. No file setting is required as does above.]

<?php 
 
// Import PHPMailer classes into the global namespace 
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 
 
require 'PHPMailer-Master/PHPMailer-Master/src/Exception.php'; 
require 'PHPMailer-Master/PHPMailer-Master/src/PHPMailer.php'; 
require 'PHPMailer-Master/PHPMailer-Master/src/SMTP.php'; 
 
$mail = new PHPMailer; 
 
$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 = 'Type sender email here';   // SMTP username 
$mail->Password = 'Type sender email password here';   // SMTP password 
$mail->SMTPSecure = 'tls';            // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 587;                    // TCP port to connect to 
 
// Sender info 
$mail->setFrom('Sender email address', 'Any message'); 
$mail->addReplyTo('recipient email ', 'xyz Kumar'); 
 
// Add a recipient 
$mail->addAddress('Type receiver email address here'); 
 
$mail->addCC('Type CC receiver email address here'); 
$mail->addBCC('Type BCC receiver email address here'); 
 
// Set email format to HTML 
$mail->isHTML(true); 
 
// Mail subject 
$mail->Subject = 'Email from Localhost by Codershelpline'; 
 
// Mail body content 
$bodyContent = '<h1>How to Send Email from Localhost using PHP mail()function by Codershelpline</h1>'; 
$bodyContent .= '<p>This HTML email is sent from the localhost server using PHP by <b>Codershelpline</b></p>'; 
$mail->Body    = $bodyContent; 
 
// Send email 
if(!$mail->send()) { 
    echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo; 
} else { 
    echo 'Message has been sent.'; 
} 
 
?>

Loading

Categories: Php Proj

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.