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

Example: Send Email with Attachment in PHP

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

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 = "
simple file attachment in php mail function
User Name : ".$_POST['u_name']."
User E-mail : ".$_POST['u_email']."
User Message : ".$_POST['message']."
"; $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.

Leave a Comment