mail with attachement – php


html page

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

<body>
<table width=”500″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td>Conatct Form with Attachment</td>
</tr>
<tr>
<td>
<form method=”post” enctype=”multipart/form-data” action=”contact_me.php”>
<table width=”500″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td>Name <span class=”required”>*</span></td>
<td><input type=”text” name=”name” id=”name” required class=”input-field”/></td>
</tr>
<tr>
<td>Email <span class=”required”>*</span></td>
<td><input type=”email” name=”email” required class=”input-field”/></td>
</tr>
<tr>
<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>
</tr>
<tr>
<td>Attachment</td>
<td><input type=”file” name=”file_attach” class=”input-field” /></td>
</tr>
<tr>
<td>Regarding</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>
</select></td>
</tr>
<tr>
<td>Message <span class=”required”>*</span></td>
<td><textarea name=”message” id=”message” class=”textarea-field” required></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type=”submit” id=”submit_btn” value=”Submit” /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

 

 

 

php page

<?php
if($_POST)
{
$to_email = “ananthu.krishna@gmail.com”; //Recipient email, Replace with own email here
$from_email = “noreply@YOUR-DOMAIN.com”; //From email address (eg: no-reply@YOUR-DOMAIN.com)

$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
if($file_error>0)
{
$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]));
die($output);
}

//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, “r”);
$content = fread($handle, $file_size);
fclose($handle);
$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”);

//header
$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));

//attachment
$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{
//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(!$send_mail)
{
//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.’));
die($output);
}else{
$output = json_encode(array(‘type’=>’message’, ‘text’ => ‘Hi ‘.$user_name .’ Thank you for your email’));
die($output);
}
}
?>

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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