How to Send Email with Attachment in PHP-My programming school

How to Send Email with Attachment in PHP

Sending Email is a necessary function of net purposes. The e-mail ship performance is used to ship notification emails to users. Generally, emails are ship with textual content particulars however generally we additionally want to ship emails with file attachments.

So you’re serious about implementing sending an email with file attachment utilizing PHP, then you definitely are in the proper place. In this tutorial, you’ll learn the way to implement Send Email with Attachment on Form Submission in PHP utilizing the PHPMailer library.

We will cowl tutorial in simple steps with stay instance to create Bootstrap kind with entering validations and ship email on kind submission utilizing PHP.

How to Send Email with Attachment in PHP

So let’s begin implementing email ship with attachment utilizing PHP. Before we start, have a look at information construction for this instance.

  • index.php
  • validation.js
  • e mail.php

How to Include jQuery, Bootstrap, Bootstrap Validation Plugin and CSS Files

As we are going to create HTML Form with Bootstrap, so first we are going to embody Bootstrap and jQuery Files. We will even embody the bootstrapValidator plugin file to implement Form enter validation and additionally different JavaScript and CSS information.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<hyperlink href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script sort="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
<script src="js/validation.js"></script>
<hyperlink rel="stylesheet" href="css/style.css">

How to Create HTML Form with File Input Field

We will create HTML Form with Bootstrap by entering fields in the index.php file.

<kind motion="mail.php" methodology="post" id="email-Form" enctype="multipart/form-data">
	<div class="contactform">
	<?php if(!empty($_GET['success']) && $_GET['success'])  ?>
		<div id="message" class="alert alert-danger alert-dismissible fade show">The message has been despatched.</div>
	<?php  ?>
	<div class="formgroup">				  
	  <label class="controllabel col-sm-2" for="fname">Name*:</label>
	  <div class="col-sm-10">          
		<enter sort="text" class="form-control" id="uname" title="uname" placeholder="Enter Name" >
	  </div>
	</div>				
	<div class="form-group">
	  <label class="controllabel col-sm-2" for="email">Email*:</label>
	  <div class="col-sm-10">
		<enter sort="email" class="formcontrol" id="email" title="email" placeholder="Enter email" >
	  </div>
	</div>
	<div class="form-group">
	  <label class="controllabel col-sm-2" for="lname">Attach File:</label>
	  <div class="col-sm-10">          
		<enter sort="file" class="formcontrol" id="attachFile" title="attachFile">
	  </div>
	</div>
	<div class="formgroup">
	  <label class="controllabel col-sm-2" for="comment">Message*:</label>
	  <div class="col-sm-10">
		<textarea class="form-control" rows="5" title="message" id="message"></textarea>
	  </div>
	</div>
	<div class="formgroup">        
	  <div class="col-sm-offset-2 col-sm-10">
		<button sort="submit" class="btn btn-default" title="sendEmail">Send Email Now</button>
	  </div>
	</div>
	</div>
</kind>

How to Implement Form Input Validation

In the validation.js file, we are going to implement Form Input Validation utilizing the bootstrapValidator plugin. We will even validate file enter discipline to permit solely particular file sort and measurement to connect with email.

$(doc).prepared(function() {
    $('#e mailForm').bootstrapValidator({        
        feedbackIcons: 
            legitimate: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        ,
        fields: 
            uname: 
                validators: 
                        stringLength: 
                        min: 2,
                    ,
                        notEmpty: 
                        message: 'Please enter your title'
                       
            ,             
            e mail: 
                validators: 
                    notEmpty: 
                        message: 'Please enter your email deal with'
                    ,
                    emailAddress: 
                        message: 'Please enter a legitimate email deal with'

            ,
			connectFile: 
                validators: 
                    file: 
                        extension: 'jpeg,png,pdf,zip,gif,txt,doc',
                        sort: 'picture/jpeg,picture/png,software/pdf,software/zip,picture/gif,textual content/plain,software/msword',
                        maxSize: 2048 * 1024,
                        message: 'The chosen file is not legitimate'
             
            ,
            message: 
                validators: 
                      stringLength: 
                        min: 10,
                        max: 200,
                        message:'Please enter at least 10 characters and no more than 200'
                    ,
                    notEmpty: 
                        message: 'Please enter a message'
                    
        
    });
});

How to Implement Send Email with Attachment with PHP Mailer

Now finally in the mail.php file, we are going to implement email ship performance with attachment utilizing PHPMailer library.

<?php
require 'PHPMailer/PHPMailerAutoload.php';
if($_POST['email']) 
	$title = $_POST['uname'];
	$toEmail = $_POST['email'];
	$message = $_POST['message'];
	try 
		$mail = new PHPMailer;
		$mail->AddAddress($toEmail);
		$mail->From = "myprogrammingschool@gmil.com";
		$mail->Subject = "Email with attachment";
		$physique = "<desk>
			<tr>
			<th colspan='2'>This is a check email with attachment</th>
			</tr>
			<tr>
			<td>Name :</td>
			<td>".$name."</td>
			</tr>			
			<tr>
			<td>Message : </td>
			<td>".$message."</td>
			</tr>
			<desk>";
			$physique = preg_replace('//','', $physique);
			$mail->MsgHTML($physique);
			$mail->IsSendmail();
			$mail->AddReplyTo("myprogrammingschool@gmil.com");
			$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
			$mail->WordWrap = 80;
			$mail->AddAttachment($_FILES['attachFile']['tmp_name'], $_FILES['attachFile']['name']);
			$mail->IsHTML(true);
			$mail->Send();
			header("Location: index.php?success=1");
	 catch (Exception $e) 
		echo "Message could not be sent. Mailer Error: $mail->ErrorInfo";
	

?>

How to obtain Source Code

Below you will find the download link of the complete source code. The file will be in zip format so before to use extract it and then use it.

Download

How to Send Email with Attachment in PHP-My programming school

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.




Best PYthon Course

Get More trending Courses

Leave a Comment