PHP mailer with attachment with out storing file at your server

Code for sending Email with out storing it at your server, can handle any size and type of file. Most useful when working with files which can contain virus in comments, resumes or anything else.

/* Mailer with Attachments */

$action = $_REQUEST[‘action’];
global $action;

function showForm() {

<form enctype=”multipart/form-data” name=”send” method=”post” action=”<?=$_SERVER[‘PHP_SELF’]?>”>
<input type=”hidden” name=”action” value=”send” />
<input type=”hidden” name=”MAX_FILE_SIZE” value=”10000000″ />
Attachment: <input type=”file” name=”attachment” size=”50″ />
<input type=”submit” value=”Send Email” /></p>

function sendMail() {
$subject = “test”; //Write a subject here for your email
$to_email = “”; //write the email address where you want to receive the emails
$to_name= “Test”; //Define receive name
if (!isset ($to_email)) { //Oops, forgot your email addy!
die (“<p>Oops!  You forgot to fill out the email address! Click on the back arrow to go back</p>”);
else {
$body = stripslashes($_POST[‘body’]);
$attachment = $_FILES[‘attachment’][‘tmp_name’];
$attachment_name = $_FILES[‘attachment’][‘name’];
if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
$fp = fopen($attachment, “rb”); //Open it
$data = fread($fp, filesize($attachment)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
//Let’s start our headers
$headers = “From:\n”; //Enter from email
//$headers .= “Reply-To: <” . $_POST[‘from_email’] . “>\n”;
$headers .= “MIME-Version: 1.0\n”;
$headers .= “Content-Type: multipart/related; type=\”multipart/alternative\”; boundary=\”—-=MIME_BOUNDRY_main_message\”\n”;
//$headers .= “X-Sender: $from_name<” . $_POST[‘from_email’] . “>\n”;
$headers .= “X-Mailer: PHP4\n”;
$headers .= “X-Priority: 3\n”; //1 = Urgent, 3 = Normal
//$headers .= “Return-Path: <” . $_POST[‘from_email’] . “>\n”;
$headers .= “This is a multi-part message in MIME format.\n”;
$headers .= “——=MIME_BOUNDRY_main_message \n”;
$headers .= “Content-Type: multipart/alternative; boundary=\”—-=MIME_BOUNDRY_message_parts\”\n”;

$message = “——=MIME_BOUNDRY_message_parts\n”;
$message .= “Content-Type: text/plain; charset=\”iso-8859-1\”\n”;
$message .= “Content-Transfer-Encoding: quoted-printable\n”;
$message .= “\n”;
/* Add our message, in this case it’s plain text.  You could also add HTML by changing the Content-Type to text/html */
$message .= “$body\n”;
$message .= “\n”;
$message .= “——=MIME_BOUNDRY_message_parts–\n”;
$message .= “\n”;
$message .= “——=MIME_BOUNDRY_main_message\n”;
$message .= “Content-Type: application/octet-stream;\n\tname=\”” . $attachment_name . “\”\n”;
$message .= “Content-Transfer-Encoding: base64\n”;
$message .= “Content-Disposition: attachment;\n\tfilename=\”” . $attachment_name . “\”\n\n”;
$message .= $data; //The base64 encoded message
$message .= “\n”;
$message .= “——=MIME_BOUNDRY_main_message–\n”;

// send the message
mail($to_email, $subject, $message, $headers);
print “<b>Mail sent, Thank you</b>”;



switch ($action) {
case “send”:

Bansal Sumit

To seek challenging roles and to achieve excellence in the field of Open Source Web Technology with a profound spirit of commitment and dedication.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *