step1-create a test2.php
Demo-Send multiple attachment with PHP MAIL
Demo2-SEND multiple attachment WITH PHP MAIL FUNCTION(AJAX JQUERY)
<center>
<form id="frmEnquiry" action="mail2.php" method="post" enctype='multipart/form-data'>
<h2>Send Multiple Attachment With Php Mail Function</h2><br>
<div id="mail-status"></div>
<div id="loader-icon" style="display: none;">
<img src="LoaderIcon.gif" />
</div>
<div>
<input
type="text" name="userName" id="userName"
class="demoInputBox" placeholder="Name" required>
</div>
<div>
<input type="text" name="userEmail" id="userEmail"
class="demoInputBox" placeholder="from email" required>
</div>
<div>
<input type="text" name="toemail" id="toemail"
class="demoInputBox" placeholder='to email' required>
</div>
<div>
<textarea name="content" id="content" class="demoInputBox"
cols="90" rows="26" placeholder="Content"></textarea>
</div>
<div>
<center> <label>Attachment</label><br /> <input type="file"
name="file[]" class="demoInputBox" multiple="multiple"></div></center>
<div>
<center> <input type="submit" value="Send" class="btnSubmit" />
<br>
<button type="reset" class="btnSubmit" value="Reset">Reset</button></center></div>
<div class="wrapperDiv">
</div>
</form>
</center>
</body>
</html>
Demo-Send multiple attachment with PHP MAIL
Demo2-SEND multiple attachment WITH PHP MAIL FUNCTION(AJAX JQUERY)
<center>
<form id="frmEnquiry" action="mail2.php" method="post" enctype='multipart/form-data'>
<h2>Send Multiple Attachment With Php Mail Function</h2><br>
<div id="mail-status"></div>
<div id="loader-icon" style="display: none;">
<img src="LoaderIcon.gif" />
</div>
<div>
<input
type="text" name="userName" id="userName"
class="demoInputBox" placeholder="Name" required>
</div>
<div>
<input type="text" name="userEmail" id="userEmail"
class="demoInputBox" placeholder="from email" required>
</div>
<div>
<input type="text" name="toemail" id="toemail"
class="demoInputBox" placeholder='to email' required>
</div>
<div>
<textarea name="content" id="content" class="demoInputBox"
cols="90" rows="26" placeholder="Content"></textarea>
</div>
<div>
<center> <label>Attachment</label><br /> <input type="file"
name="file[]" class="demoInputBox" multiple="multiple"></div></center>
<div>
<center> <input type="submit" value="Send" class="btnSubmit" />
<br>
<button type="reset" class="btnSubmit" value="Reset">Reset</button></center></div>
<div class="wrapperDiv">
</div>
</form>
</center>
</body>
</html>
Step2-create a mail2.php file
<?php
if($_POST && isset($_FILES['file']))
{
$recipient_email = $_POST['toemail'];
$from_email = $_POST['userEmail'];
$subject = "Attachment email from your website!";
$sender_name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING); //capture sender name
$sender_email = filter_var($_POST["toemail"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST["content"], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['file'];
//php validation
if(strlen($sender_name)<4){
die('Name is too short or empty');
}
if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email');
}
if(strlen($sender_message)<4){
die('Too short message! Please enter something');
}
$file_count = count($attachments['name']); //count total files attached
$boundary = md5("sanwebe.com");
if($file_count > 0){ //if attachment exists
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$from_email."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($sender_message));
//attachments
for ($x = 0; $x < $file_count; $x++){
if(!empty($attachments['name'][$x])){
if($attachments['error'][$x]>0) //exit script and output error if we encounter any
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
die($mymsg[$attachments['error'][$x]]);
}
//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];
//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}
}
}else{ //send plain email otherwise
$headers = "From:".$from_email."\r\n".
"Reply-To: ".$sender_email. "\n" .
"X-Mailer: PHP/" . phpversion();
$body = $sender_message;
}
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
die('Thank you for your email');
}else{
die('Could not send mail! Please check your PHP mail configuration.');
}
}
?>
1 comments:
Click here for commentsThe Article are written by the author are very informative and have a unique idea to share. Thanks for sharing this Information with us.
We provide english speaking course in gurgaon at very resonable price with 100% Sucess rate by AIBEDUCATION.
ConversionConversion EmoticonEmoticon