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 Live24u.com – 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.

<?php
	session_start();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Simple Sending Email using PHPMailer Examples</title>
	<link href="bootstrap.min.css" rel="stylesheet">
</head>
<body>
<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">
			<?php
				if(isset($_SESSION['my_message'])){
					?>
					<div class="alert alert-info text-center">
						<?php echo $_SESSION['my_message']; ?>
					</div>
					<?php
 
					unset($_SESSION['my_message']);
				}
			?>
			<form method="POST" action="do_send.php" enctype="multipart/form-data">
				<div class="live form-group semail">
					<label>Email:</label>
					<input type="email" class="inpt form-control dctrl" name="email" required>
				</div>
				<div class="live form-group semail">
					<label>Subject:</label>
					<input type="text" class="inpt form-control dctrl" name="subject" required>
				</div>
				<div class="live form-group semail">
					<label>User Message:</label>
					<textarea class="inpt form-control dctrl" name="my_message" required></textarea>
				</div>
				<div class="live form-group semail">
					<label>Add File Attachment:</label>
					<input type="file" name="file_attachments">
				</div>
				<button type="submit" name="send" class="btn btn-success">Mail Send</button>
			</form>
		</div>
	</div>
</div>
</body>
</html>

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

<?php
//import libs of phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
session_start();
 
if(isset($_POST['send'])){
 
    $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 = 'smtp.gmail.com';//Live Specify main and backup SMTP servers
        $email_obj->SMTPAuth = true; //Live Enable SMTP authentication
        $email_obj->Username = '<a href="mailto:adminlive24u@gmail.com" rel="nofollow">adminlive24u@gmail.com</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="mailto:adminlive24u@gmail.com" rel="nofollow">adminlive24u@gmail.com</a>');
 
        //All the Recipients
        $email_obj->addAddress($email);              
        $email_obj->addReplyTo('<a href="mailto:adminlive24u@gmail.com" rel="nofollow">adminlive24u@gmail.com</a>');
 
        //live File Attachment Data
        if(!empty($livef_name)){
            $email_obj->addAttachment($location, $livef_name); 
        }
 
        //live Data Content
        $email_obj->isHTML(true);                                  
        $email_obj->Subject = $subject;
        $email_obj->Body    = $my_message;
 
        $email_obj->send();
        $_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;
    }
 
    header('location:index.php');
}
else{
    $_SESSION['my_message'] = 'Your Form is empty';
    header('location:index.php');
}

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





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(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.