Mail send with attachment in php demo

Today, We want to share with you mail send with attachment in php demo.In this post we will show you php send email smtp, hear for php code to send email from contact form we will give you demo and example for implement.In this post, we will learn about Send Simple Mail Function Using PHP with an example.

How to Send Email with Attachment in PHP?

PHP Mail Attachment – Php Send Email File-Images Attachments
PHP Mail Attachment – Php Send Email File-Images Attachments

Step 1: Create Email Form HTML

index.php

<div class="container">
	<h2>Example: Send Email with Attachment in PHP</h2>
	<div id='success_message' class='hidden'>		 
		 <h2>Your Mail Sent Successfully! Good Luck</h2>
		 <p><strong>You will be in touch soon.</strong></p>		 
	</div>	
	<form action='email_send.php' class="form-email" method="post" id="email-form" enctype='multipart/form-data'>		
		<div id="error">
		</div>
		<div class="form-group">
			<input type="input" class="form-control" placeholder="Enter Your Name" name="u_name" id="u_name" />
			<span id="check-e"></span>
		</div>
		<div class="form-group">
			<input type="email" class="form-control" placeholder="Enter Your Email address" name="u_email" id="u_email" />
			<span id="check-e"></span>
		</div>		
		<div class="form-group">
			<input type="file" class="form-control" placeholder="File" name="file_attachements_doc" id="file_attachements_doc" />
		</div>
		<div class="form-group">
			<textarea cols="60" rows="5"  id="message" name="message" placeholder='Message'></textarea>
		</div>
		<hr />
		<div class="form-group">
			<button type="submit" class="btn btn-default" name="email_send" id="email_send">
			Send Email
			</button> 
		</div> 
	</form>	
</div>

PHP Contact Form Send Email Add More File Attachment
PHP Contact Form Send Email Add More File Attachment

Step 3: Implement Email Send with Attachment using PHPMailer

email_send.php

<?php
require 'PHPMailer/PHPMailerAutoload.php';
try {
	if(isset($_POST['email_send'])) {
		$sendMailObj = new PHPMailer;
		$sendMailObj->FromName   = $_POST['u_name'];
		$to_email = $_POST['u_email'];
		$sendMailObj->AddAddress($to_email);
		$sendMailObj->From       = "[email protected]";		
		$sendMailObj->Subject  = "send mail with attachment in php w3schools";
		$body = "<table>
			 <tr>
				<th colspan='2'>simple file attachment in php mail function</th>
			 </tr>
			 <tr>
				<td>User Name :</td>
				<td>".$_POST['u_name']."</td>
			 </tr>
			 <tr>
			  <td>User E-mail : </td>
			  <td>".$_POST['u_email']."</td>
			</tr>	
			<tr>
			  <td>User Message : </td>
			  <td>".$_POST['message']."</td>
			</tr>
			<table>";
		$body = preg_replace('/\\\\/','', $body);
		$sendMailObj->MsgHTML($body);		
		$sendMailObj->IsSendmail();
		$sendMailObj->AddReplyTo("[email protected]");
		$sendMailObj->AltBody    = "To view the message, please use an HTML CODE compatible email Display viewer!"; 
		$sendMailObj->WordWrap   = 80; 
		$sendMailObj->AddAttachment($_FILES['file_attachements_doc']['tmp_name'], $_FILES['file_attachements_doc']['name']);
		$sendMailObj->IsHTML(true);
		$sendMailObj->Send();
		echo 'The message has been sent.';
	}
} catch (phpmailerException $e) {
	echo $e->errorMessage();
}
?>

Step 2: Validate Email Form

form_validation.js

$('document').ready(function() { 
	(function() {
		$('form').ajaxForm({
			beforeSubmit: function() {			
				$("#email-form").validate({
					rules: {
						u_name: {
							required: true,
							minlength : 3
						},
						u_email: {
							required: true,
							email: true
						},
						file_attachements_doc: {
							required: true	
						},			
						message: {
							required: true
						}
					},
					messages: {
						u_name: {
						   required:"Please enter User name",
						   minlength: "Please enter a valid User name"
						},
						u_email:{ 
						   required: "Please enter your User email",
						   minlength: "Please enter a valid User email address",
						},
						file_attachements_doc: "Please Choose User Profiles",
						message: "Please enter message"
					},		     
				 });
				 var flag= $('#email-form').valid();
				 if(!flag){
					 return false;
				 }
			},			
			complete: function(xhr) {	
				$("#email-form").addClass("hidden");
				$("#success_message").removeClass("hidden");						
			}
		}); 	
	})();	
});

I hope you get an idea about how to send mail in php from localhost?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Create Contact Form Sending Email via SMTP PHPMailer

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  laravel Views share Variable multiple to controller
  2. Read Also:  Send Simple Mail Function using PHP
  3. Read Also:  Laravel 5.8 get current url with parameters
  4. Read Also:  php send mail attachment with Pear Mail
  5. Read Also:  How to Send Emails Using PHP Scripts
  6. Read Also:  How To Create Contact Form With PHPMailer To Send Mails
  7. Read Also:  How to Send Email Using PHP scripts?
  8. Read Also:  Create Contact Form And Send Email using PHP
  9. Read Also:  PHP Send HTML Email File Attachment Using PHPs mail
  10. Read Also:  how to display current month in PHP?
CLOSEX