PHP Email with attachment
PHP Email with attachment
PHP Email With Attachment
Sending email with attachment from your website is really a great add on. Usually this is required if you have a contact us page where you need your users to attach any further information or a web page where users can attach files and send etc.,
This is a simple example, All you need is to create a HTML form with all the required entries as below. Let us name the file as mail.html
<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>PHP Mail With Attachment</title></head> <body style="padding:3px; margin:0px;" bgcolor="#FFFFFF"> <form name="frm" method="POST" action="success.php" enctype="multipart/form-data">Name<input type="text" name="strname" class="textfield"><p> Address<textarea cols="16" name="straddress"></textarea><p>City<input type="text" name="strcity" class="textfield"><p> State<input type="text" name="strstate" class="textfield"><p>Contact No<input type="text" name="strno" class="textfield"><p>Email<input type="text" name="stremail" class="textfield"><p> Comments<textarea cols="16" name="strcomments"></textarea><p>Resume<input type="file" name="strresume"><p><input type="submit" value="Send" name="submit" onClick="return validate();"> <input type="reset" value="Reset" name="reset"> </form></body></html>
Next step is to create a PHP file to process the information from the HTML page.
Before we proceed some information regarding the functions and code used in the PHP script.
I have added 4 file types here. You are free to add any number of file types according to your convenience.
if($filetype=="application/octet-stream" or $filetype=="text/plain" or $filetype=="application/msword" or $filetype=="image/jpeg")
ucfirst() function in PHP returns a string with the first character of str capitalized
To avoid email landing in SPAM folder of your mail client include these headers (Not always helpful 🙁 ). There might be other reasons as well on why your emails land in SPAM . Make sure you modify the emails accordingly.
$headers .= "Reply-To: The Sender <[email protected]>\r\n"; $headers .= "Return-Path: The Sender <[email protected]>\r\n"; $headers = "From: Mistonline Demo< [email protected]>\r\n"; $headers .= "MIME-Version: 1.0\r\n";
Let us name it as success.php and the entire code look like the one below
<?php $strname=ucfirst($_REQUEST["strname"]); $straddress=ucfirst($_REQUEST["straddress"]); $strcity=ucfirst($_REQUEST["strcity"]); $strstate=ucfirst($_REQUEST["strstate"]); $phone=$_REQUEST["strno"]; if($phone != ""){ $strno=$phone; } else { $strno="-"; } $stremail=$_REQUEST["stremail"]; $strcomments=ucfirst($_REQUEST["strcomments"]); $filename=$_FILES["strresume"]["name"]; $filetype=$_FILES["strresume"]["type"]; $filesize=$_FILES["strresume"]["size"]; $filetemp=$_FILES["strresume"]["tmp_name"]; echo $filename; if($filetype=="application/octet-stream" or $filetype=="text/plain" or $filetype=="application/msword" or $filetype=="image/jpeg") {$message= 'Name'.$strname.'Address '.$straddress.'City '.$strcity.'State '.$strstate.'Contact No. '.$strno.'Email '.$stremail.'Comments '.$strcomments.''; // MAIL SUBJECT $subject = "Mail with doc file attachment"; // TO MAIL ADDRESS $to=$stremail; // MAIL HEADERS $headers = "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; $fp = fopen($filetemp, "rb"); $file = fread($fp, $filesize); $file = chunk_split(base64_encode($file)); $num = md5(time()); //Normal headers $headers .= "Reply-To: The Sender <[email protected]>\r\n"; // Make sure to add your VALID EMAIL ID HERE $headers .= "Return-Path: The Sender <[email protected]>\r\n"; // Make sure to add your VALID EMAIL ID HERE $headers = "From: Mistonline Demo< [email protected]>\r\n"; // Make sure to add your VALID EMAIL ID HERE $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed; "; $headers .= "boundary=".$num."\r\n"; $headers .= "--$num\r\n"; // This two steps to help avoid spam $headers .= "Message-ID: [email protected]".$_SERVER['SERVER_NAME'].">\r\n"; $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; // With message $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 8bit\r\n"; $headers .= "".$message."\n"; $headers .= "--".$num."\n"; // Attachment headers $headers .= "Content-Type:".$filetype." "; $headers .= "name=\"".$filename."\"r\n"; $headers .= "Content-Transfer-Encoding: base64\r\n"; $headers .= "Content-Disposition: attachment; "; $headers .= "filename=\"".$filename."\"\r\n\n"; $headers .= "".$file."\r\n"; $headers .= "--".$num."--"; // SEND MAIL $from = ""; //Include your FROM EMAIL ID HERE mail($to, $subject, $message, $headers); fclose($fp); echo ' Attachment has been sent Successfully.'; } else {echo 'Wrong file format. Mail was not sent.'; } ?>
Note: This tutorial has been updated and all issues fixed. Previous submitted Sep 16, 2008. Bugs fixed on May 9, 2016
PHP Email with attachment,Incoming search terms:
- hyip inurl:/guestbook php cgi?gbook= (180)
- submariner forum inurl:/faq php intext:powered by vbulletin (20)
- ageing inurl:/faq/memberlist php?mode= (12)
- background color with width using phpexcel (11)
- mail gazete3 com tr loc:US (8)
- medication inurl:/chat/memberlist php?mode= (6)
- inurl:/searchkeyword php?type=blog (6)
- mail ppi-etc sk loc:US (5)
- inurl:mailer php (5)
- mail bormetal com tr loc:US (5)
- mail aktuelarkeoloji com tr loc:US (4)
- mail barisboya com tr loc:US (4)
- mail itbak com tr loc:US (4)
- mail mavimedya com tr loc:US (3)
- mail mrtmakina com tr loc:US (3)
- mail odtuebi com tr loc:US (3)
- mail akdagisil com tr loc:US (3)
- mail clarionjeans com tr loc:US (3)
- mail odtuebi com tr (2)
- mail best com tr loc:US (2)
- mail kayasusondaj com tr (2)
- mail berkeczanesi com tr loc:US (2)
- mail asdembeach com tr (2)
- mail bayramemlak com tr loc:US (2)
- mail armateknoloji com tr loc:US (2)
- mail arasgrup com tr loc:US (2)
- PHPE-mailwithattachment|WebsiteScriptsAndTutorials (2)
- mail akyapiticaret com tr loc:US (2)
- mail aktoros com tr loc:US (2)
- mail aknurorme com tr loc:US (2)
- mail aametal com tr loc:US (2)
- yii mail with attachments (1)
- inurl : Mailer © 2012 (1)
- inurl:/comment-attachment/comment-attachment php (1)
- inurl:guestbook php (1)
- inurl:xMailler php (1)
- [email protected] com tr loc:US (1)
- mail ardicoto com tr (1)
- mail akcamtekstil com tr loc:US (1)
- mail ardicoto com tr loc:US (1)
The script i was looking for….thanks bro..