I am trying to send attach file to email. But i am unable to send the attach file along with the form data to email. I tried
HTML:
<form action="sendmail.php" method="post" name="form1" enctype="multipart/form-data">
<table width="343" border="1">
<tr>
<td>Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" id="city"></td>
</tr>
<tr>
<td>Descibe Yourself</td>
<td><textarea name="description" cols="30" rows="4" id="description"></textarea></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input name="mobile" id="mobile"type="text"></td>
</tr>
<tr>
<tr>
<td>Email</td>
<td><input name="email" type="email" id="email"></td>
</tr>
<tr>
<td>Attach Your Image</td>
<td><input name="fileAttach" type="file"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
sendmail.php:
<?
$name = $_POST['name'];
$city = $_POST['city'];
$description = $_POST['description'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
if ($name == '' || $city == '' || $description==''|| $mobile=='' || $email=='')
{
echo "<script> alert('Fill all fields, try again!');
window.location='contact.html'</script>";
exit;
}
$ToEmail = 'info@mywebsite.com';
$EmailSubject = 'Website contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br><br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Description: ".$_POST["description"]."<br>";
$MESSAGE_BODY .= "Mobile: ".nl2br($_POST["mobile"])."<br>";
$MESSAGE_BODY .= "City: ".nl2br($_POST["city"])."<br>";
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader, $strHeader) or die ("Failure");
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
exit;
if($mail)
{
echo "<script> alert('Messgae successfully sent!');
window.location='index.html'</script>";
}
else
{
echo "<script> alert('Temporary problem, try again!');</script>";
}
?>
I tried the same code without $strHeader in mail() the mail was sent but when i added $strHeader to mail() i was unable to send code. Kindly guide me where i am doing mistake and also plz tell how how can i allow only jpg,jpeg,png format to to uploaded. Edited code for me would be easy for me for learning. I checked lot of answers on stackoverflow bt couldn't found the answer.