PHP Sending Email with attachments using PHPMailer

PHP Sending Email with attachments using PHPMailer

In this Post We Will Explain About is PHP Sending Email with attachments using PHPMailer With Example and Demo.Welcome on – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to Send email using PHPMailer Example

In this post we will show you Best way to implement Send Email via SMTP Server in PHP using PHPMailer, hear for send email using Gmail SMTP server through PHP Mailer with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Installing the PHPMailer using Composer

1. Create a main root folder of your project, in my case We have created my project inside our server htdocs dir of my xampp and name it phpmailer.

2. make a file named in your root dir composer.json inside your project dir and add the follwing scripts. This will says php cmd composer to install some data functions phpmailer to simple project.

Also Read :   Mysql left outer join example where clause

     "phpmailer/phpmailer": "~6.0"

3. And then Open your (CMD)command prompt, Set path or navigate to your Project and type the follwing command and run.

      composer require phpmailer/phpmailer

This file will install simple PHPMailer to simple PHP Projects.
4. And then Lst installation, you should be more able to display vendor any scripts folder.

Creating simple Send Email Form

And then, I am going to make a form to send PHP through email using PHPMailer functions and this will be simple main file like as index.php.

<!DOCTYPE html>
	<meta charset="utf-8">
	<title>Simple Sending Email using PHPMailer Examples</title>
	<link href="bootstrap.min.css" rel="stylesheet">
<div class="container">
	<h1 class="page-header text-center">PHP Simple Sending Email using PHPMailer Example</h1>
	<div class="live24u row">
		<div class="live24u col-md-4 col-md-offset-4">
					<div class="alert alert-info text-center">
						<?php echo $_SESSION['my_message']; ?>
			<form method="POST" action="do_send.php" enctype="multipart/form-data">
				<div class="live form-group semail">
					<input type="email" class="inpt form-control dctrl" name="email" required>
				<div class="live form-group semail">
					<input type="text" class="inpt form-control dctrl" name="subject" required>
				<div class="live form-group semail">
					<label>User Message:</label>
					<textarea class="inpt form-control dctrl" name="my_message" required></textarea>
				<div class="live form-group semail">
					<label>Add File Attachment:</label>
					<input type="file" name="file_attachments">
				<button type="submit" name="send" class="btn btn-success">Mail Send</button>

Creating simple Send Email Code

This Data contains simple Scripts that send email using PHP Base PHPMailer and I am going to name it as do_send.php.

Also Read :   Create Contact Form Sending Email via SMTP PHPMailer

//import libs of phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $my_message = $_POST['my_message'];
    $livef_name = $_FILES['file_attachments']['name'];
    $location = 'file_attachments/' . $livef_name;
    move_uploaded_file($_FILES['file_attachments']['tmp_name'], $location);
    //live Load composer's autoloader
    require 'vendor/autoload.php';
    $email_obj = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Live Server settings
        $email_obj->isSMTP(); //Live Set mailer to use SMTP
        $email_obj->Host = '';//Live Specify main and backup SMTP servers
        $email_obj->SMTPAuth = true; //Live Enable SMTP authentication
        $email_obj->Username = '<a href="" rel="nofollow"></a>';//Live Your Email/ Server Email
        $email_obj->Password = 'mylive24u@#4887'; //Live Your Password
        $email_obj->SMTPOptions = array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        $email_obj->SMTPSecure = 'ssl';  //tls or ssl                         
        $email_obj->Port = 465;                                   
        //Your Send Email
        $email_obj->setFrom('<a href="" rel="nofollow"></a>');
        //All the Recipients
        $email_obj->addReplyTo('<a href="" rel="nofollow"></a>');
        //live File Attachment Data
            $email_obj->addAttachment($location, $livef_name); 
        //live Data Content
        $email_obj->Subject = $subject;
        $email_obj->Body    = $my_message;
        $_SESSION['my_message'] = 'Your Email Message has been sent, Good Luck!';
    } catch (Exception $e) {
        $_SESSION['my_message'] = 'Your Email Message could not be sent sorry. Mailer Error: '.$email_obj->ErrorInfo;
    $_SESSION['my_message'] = 'Your Form is empty';

Allowing Less Secure Apps

Last step, in simple SMTP Based server email, we need to seom process of allow less defult set of the secure apps by simple domin name logging in your google server email as well as use Like this Less Secure Apps

Also Read :   JQuery AJAX form Submit and serialize send Server Side


Imp note Dont forget to make any pdf file, jpg file or any data file_attachments folder in simple main root app folder. It will be the div main container for simple nay data uploaded with send attachments. Also,Imp take note of some account other domains send mail Gmails restrictions in sending some emails with any file of the attachments.

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example


I hope you have Got What is send email using Gmail SMTP server through PHP Mailer And how it works.I would Like to have FeedBack From My Blog( readers.Your Valuable FeedBack,Any Question,or any Comments about This Article( Are Most Always Welcome.