mail with attachement – php

html page

<!DOCTYPE html>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>mail with attachment</title>

<table width=”500″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<td>Conatct Form with Attachment</td>
<form method=”post” enctype=”multipart/form-data” action=”contact_me.php”>
<table width=”500″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<td>Name <span class=”required”>*</span></td>
<td><input type=”text” name=”name” id=”name” required class=”input-field”/></td>
<td>Email <span class=”required”>*</span></td>
<td><input type=”email” name=”email” required class=”input-field”/></td>
<td>Phone <span class=”required”>*</span></td>
<td><input type=”text” name=”phone1″ maxlength=”4″ placeholder=”+91″ required=”true” class=”tel-number-field”/>
<input type=”text” name=”phone2″ maxlength=”15″ required=”true” class=”tel-number-field long” /></td>
<td><input type=”file” name=”file_attach” class=”input-field” /></td>
<td><select name=”subject” class=”select-field”>
<option value=”General Question”>General Question</option>
<option value=”Advertise”>Advertisement</option>
<option value=”Partnership”>Partnership Oppertunity</option>
<td>Message <span class=”required”>*</span></td>
<td><textarea name=”message” id=”message” class=”textarea-field” required></textarea></td>
<td><input type=”submit” id=”submit_btn” value=”Submit” /></td>




php page

$to_email = “”; //Recipient email, Replace with own email here
$from_email = “”; //From email address (eg:

$user_name = $_POST[“name”];
$user_email = $_POST[“email”];
$country_code = $_POST[“phone1”];
$phone_number = $_POST[“phone2”];
$subject = $_POST[“subject”];
$message = $_POST[“message”];

$message_body =
“\n Name: “.$user_name
.”\n Email : “.$user_email
.”\n Phone Number : (“.$country_code.”) “. $phone_number ;

### Attachment Preparation ###
$file_attached = false;
if(isset($_FILES[‘file_attach’])) //check uploaded file
//get file details we need
$file_tmp_name = $_FILES[‘file_attach’][‘tmp_name’];
$file_name = $_FILES[‘file_attach’][‘name’];
$file_size = $_FILES[‘file_attach’][‘size’];
$file_type = $_FILES[‘file_attach’][‘type’];
$file_error = $_FILES[‘file_attach’][‘error’];

//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” );

$output = json_encode(array(‘type’=>’error’, ‘text’ => $mymsg[$file_error]));

//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, “r”);
$content = fread($handle, $file_size);
$encoded_content = chunk_split(base64_encode($content));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;

if($file_attached) //continue if we have the file
$boundary = md5(“sanwebe”);

$headers = “MIME-Version: 1.0\r\n”;
$headers .= “From:”.$from_email.”\r\n”;
$headers .= “Reply-To: “.$user_email.”” . “\r\n”;
$headers .= “Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n”;

//plain 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($message_body));

$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;

//proceed with PHP email.
$headers = “From:”.$from_email.”\r\n”.
‘Reply-To: ‘.$user_email.” . “\n” .
‘X-Mailer: PHP/’ . phpversion();
$body = $message_body;

$send_mail = mail($to_email, $subject, $body, $headers);

//If mail couldn’t be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array(‘type’=>’error’, ‘text’ => ‘Could not send mail! Please check your PHP mail configuration.’));
$output = json_encode(array(‘type’=>’message’, ‘text’ => ‘Hi ‘.$user_name .’ Thank you for your email’));




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s