Friday, 17 June 2022

Sending Emails in PHP with PHPMailer

PHPMailer is perhaps the most popular open-source PHP library to send emails with.


first Download PhpMailer from  https://github.com/PHPMailer/PHPMailer


1)

Sending Email from a Local Web Server Using PHPMailer

Here’s the simplest example of sending an email from a local web server using PHPMailer:-



<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions

//From email address and name
$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

//To address and name
$mail->addAddress("recepient1@example.com", "Recepient Name");
$mail->addAddress("recepient1@example.com"); //Recipient name is optional

//Address to which recipient will reply
$mail->addReplyTo("reply@yourdomain.com", "Reply");

//CC and BCC
$mail->addCC("cc@example.com");
$mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo; 

} 


Sending an Email with Attachments

Here’s an example of how to send an email with attachments using PHPMailer:-

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->From = "from@yourdomain.com";
$mail->FromName = "Full Name";

$mail->addAddress("recipient1@example.com", "Recipient Name");

//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");        
$mail->addAttachment("images/profile.png"); //Filename is optional

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

try {
    $mail->send();
    echo "Message has been sent successfully";
} catch (Exception $e) {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
Here, we’re attaching two files — file.txt, which resides in the same directory as the script, and images/profile.png, which resides in images directory of the script directory. To add attachments to the email, we just need to call the function addAttachment of the PHPMailer object by passing the file path as argument. For attaching multiple files, we need to call it multiple times

Thursday, 16 June 2022

Social Media Marketing Package


 

Php Mailer

 step 1:-

download phpmailer library file form https://github.com/PHPMailer/PHPMailer


step 2:- signupform.html code :-


<html>

<head>

</head>

<body>

<form action="register.php" method="post">

<label> First name </label>

<input type="text" name="firstname">

<label> Last name </label>

<input type="text" name="lastname">

<label> Contact no </label>

<input type="text" name="contactno">

<label> Email </label>

<input type="text" name="email">

<labe> Password</label>

<input type="text" name="password">

<label> Confirm Password </label>

<input type="text" name="confirmpassword">


<input type="submit" name="send" value="signup">

</form>

</body>

</html>

</html>


step 3:- write register.php file code.


<?php


include ("connect.php");


use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\SMTP;

use PHPMailer\PHPMailer\Exception;

 

 $firstname=$_POST['firstname'];

 $lastname=$_POST['lastname'];

 $contactno=$_POST['contactno'];

 $email=$_POST['email'];

 $password=$_POST['password'];


 

 

 

 $v_code=bin2hex(random_bytes(16));

 

 function sendmail($email,$v_code)

 {

require "PHPMailer\PHPMailer.php"; 

require "PHPMailer\SMTP.php";

require "PHPMailer\Exception.php";

$mail = new PHPMailer(true);

try {

   

    $mail->isSMTP();                                            //Send using SMTP

    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through

    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication

    $mail->Username   = 'yourgmailid@gmail.com';                     //SMTP username

    $mail->Password   = 'yourgmailpassword';                               //SMTP password

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption

    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`


    //Recipients

    $mail->setFrom('yourgmailid@gmail.com', 'Musaib');

    $mail->addAddress($email);     //Add a recipient

    


    //Content

    $mail->isHTML(true);                                  //Set email format to HTML

    $mail->Subject = 'Email Verification from Musaib';

    $mail->Body    = "Thanks for Registration!

             Click the link below to verify the email address

<a href='http://localhost:8081/emailverify/verify.php?email=$$email&v_code=$v_code'>Verify </a>";

   


    $mail->send();

    return true; //mail sent

    } 

catch (Exception $e) {

    return false; //mail not sent

   }

 }

 

 $sql="insert into user (firstname,lastname,contactno,email,password,v_code,is_verified) values('$firstname','$lastname','$contactno','$email','$password','$v_code')";

 

 $result=mysqli_query($con,$sql);

 

 if($result&&sendmail($_POST['email'],$v_code))

 

{

echo "<script>

alert ('Registration Succesfully');

window.location.href='signupform.html';

</script>";

}

 

else

{

echo "<script>

alert ('Registration Failed');

</script>"; 

}

 

 


 

 



?>


step 4:- verify.php file code:-


<?php

include("connect.php");


$email=$_GET['email'];

$v_code=$_GET['v_code'];


if(isset($_GET['email')&& isset($_GET['v_code']))

  {

  

  $sql="select from user where email='$email' And v_code='$v_code'";

  

  $result=mysqli_query($con,$sql);

  $num=mysqli_num_rows($result);

  

   if($num>0)

   

   {

   $result_fetch=mysqli_fetch_array($result);

   if($result_fetch['is_verified']==0)

    {

$sql="update user set is_verified='1' where email='$result_fetch'";

if(mysqli_query($con,$sql)

{

echo "<script>

alert("email verification succesful");

</script>";

}

else{

echo "<script>

alert("email verification failed");

</script>";

}

}

   }

   else

   {

   echo "<script>

alert("email already registered");  

</script>";// not executed

   }

  

  }



?>