Skip to Content
PHPCODE
send email with attachment in php
php code / April 25, 2018

Sending email from the content is the extremely valuable usefulness in the web application. The majority of the site utilized the email sending highlight to send the notices. On the off chance that your web application utilizes PHP, it’s anything but difficult to send email from the content utilizing PHP

PHP gives a simple method to send email from the site. Utilizing mail() work in PHP, you can send content or HTML email. Be that as it may, once in a while email usefulness should be reached out for sending a connection with the mail. In this instructional exercise, we will demonstrate to you proper methodologies to send email with connection in PHP. Our case content makes it easy to send content or HTML email including any kinds of documents as a connection (like picture, .doc, .docx, .pdf, .txt, and so forth.) utilizing PHP

This custom capacity or PHP mail connection content can send a plain content email message together with a solitary connection record. The connection record must be transferred first or you can utilize a document which as of now exists on your web server. There are vastly improved and further developed PHP contents on the Internet, however I trust this case will assist you to see, how it’s conceivable with sending an email message in addition to connection by utilizing some PHP code.

The mail() work doesn’t bolster connection or HTML mail as a matter of course. You have to utilize diverse headers and MIME mail parts to make this conceivable. Numerous common facilitating suppliers doesn’t permit the utilization of this capacity and it may be debilitated.

step by step code : here

step 1: create container.php

</head>
<body class=””>
<div role=”navigation” class=”navbar navbar-default navbar-static-top”>
      <div class=”container”>
        <div class=”navbar-header”>
          <button data-target=”.navbar-collapse” data-toggle=”collapse” class=”navbar-toggle” type=”button”>
            <span class=”sr-only”>Toggle navigation</span>
            <span class=”icon-bar”></span>
            <span class=”icon-bar”></span>
            <span class=”icon-bar”></span>
          </button>
          <a href=”#” class=”navbar-brand”>PHPCODE</a>
        </div>
        <div class=”navbar-collapse collapse”>
          <ul class=”nav navbar-nav”>
            <li class=”active”><a href=”#”>Home</a></li>
          </ul>
        </div><!–/.nav-collapse –>
      </div>
    </div>
<div class=”container” style=”min-height:500px;”>
<div class=”>
</div>

step 2 Created email_send.php

<?php
require ‘PHPMailer/PHPMailerAutoload.php’;
try {
if(isset($_POST[’email_send’])) {
$mail = new PHPMailer;
$mail->FromName   = $_POST[‘u_name’];
$to_email = $_POST[‘u_email’];
$mail->AddAddress($to_email);
$mail->From       = “admin@phpzag.com”; $mail->Subject  = “Test Email Send using PHP”;
$body = “<table>
<tr>
<th colspan=’2′>This is a test email</th>
</tr>
<tr>
<td>Name :</td>
<td>”.$_POST[‘u_name’].”</td>
</tr>
<tr>
  <td>E-mail : </td>
  <td>”.$_POST[‘u_email’].”</td>
</tr> <tr>
  <td>Message : </td>
  <td>”.$_POST[‘message’].”</td>
</tr>
<table>”;
$body = preg_replace(‘/\\/’,”, $body);
$mail->MsgHTML($body); $mail->IsSendmail();
$mail->AddReplyTo(“admin@phpzag.com”);
$mail->AltBody    = “To view the message, please use an HTML compatible email viewer!”;
$mail->WordWrap   = 80;
$mail->AddAttachment($_FILES[‘attach’][‘tmp_name’], $_FILES[‘attach’][‘name’]);
$mail->IsHTML(true);
$mail->Send();
echo ‘The message has been sent.’;
}
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
step 3 created  index.php

<?php
include(‘header.php’);
?>
<title>phpcode : Demo Send Email with Attachment in PHP</title>
<script type=”text/javascript” src=”script/validation.min.js”></script>
<script type=”text/javascript” src=”script/form_validation.js”></script>
<script src=”http://malsup.github.com/jquery.form.js”></script>
<link href=”css/style.css” rel=”stylesheet” type=”text/css” media=”screen”>
<?php include(‘container.php’);?>
<div class=”container”>
<h2>Example: Send Email with Attachment in PHP</h2>
<div id=’success_message’ class=’hidden’>
<h2>Your Mail Sent Successfully!</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=”Name” name=”u_name” id=”u_name” />
<span id=”check-e”></span>
</div>
<div class=”form-group”>
<input type=”email” class=”form-control” placeholder=”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=”attach” id=”attach” />
</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 style=”margin:10px 0px 0px 0px;”>
<a class=”btn btn-default read-more” style=”background:#3399ff;color:white” href=”#” title=””>Back to Tutorial</a>
</div>
</div>

<?php include(‘footer.php’);?>

step 4 step header.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<link rel=”stylesheet” id=”font-awesome-style-css” href=”http://phpflow.com/code/css/bootstrap3.min.css” type=”text/css” media=”all”>
<!– jQuery –>

<script type=”text/javascript” charset=”utf8″ src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js”></script>

step 5 step footer.php
<div class=”insert-post-ads1″ style=”margin-top:20px;”>

</div>
</div>
</body></html>
Validation 
Step 6 created 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
},
attach: {
required: true
},
message: {
required: true
}
},
messages: {
u_name: {
   required:”Please enter name”,
   minlength: “Please enter a valid name”
},
u_email:{
   required: “Please enter your email”,
   minlength: “Please enter a valid email address”,
},
attach: “Please Choose image”,
message: “Please enter message”
},
});
var flag= $(‘#email-form’).valid();
if(!flag){
return false;
}
},
complete: function(xhr) {
$(“#email-form”).addClass(“hidden”);
$(“#success_message”).removeClass(“hidden”);
}
});
})();

});
created 7 created style.css
@charset “utf-8”;

.form-email {
max-width: 500px;
padding: 19px 29px 29px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
font-family:Tahoma, Geneva, sans-serif;
color:#990000;
font-weight:lighter;
}
.form-email .form-email-heading {
color:#00A2D1;
}
.form-email input[type=”text”],
.form-email input[type=”password”],
.form-email input[type=”email”] {
font-size: 16px;
height: 45px;
padding: 7px 9px;
}
.body-container {
margin-top:110px;
}
.navbar-brand {
font-family:”Lucida Handwriting”;
}
#btn-submit{
height:45px;

}

NOTE This is email form with library used all code download

DOWNLOAD

PHPCODE © 2024