So I am trying to mail a form with attachment but when I include $header in my mail it fails to send: mail("mohd.gadiwala@techmatters.com", $subject, $message, $headers)
and when I remove $header from my code mail is being sent but the image attachment being send is text data and not the actual attachment image png form.Everything is crammed up w/o boundary I tried code from this website: Click Me
What am I doing wrong in my code below?:
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit')
{
$to="siva.garre@livait.net";
$subject="File sent by ".$_POST['name'];
// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">";
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['comment'];
// generate a random string to be used as the boundary marker
$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
if($_FILES['filename']['tmp_name'] != ''){
// store the file information to variables for easier access
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$file_name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
}
// here we'll hard code a text message
// again, in reality, you'll normally get this from the form submission
if($tmp_name != ''){
$message = "nn Name: $name nn Email: $email_address nnMessage: nn $message nnHere is your file: $file_name";
}
else{
$message = "nn Name: $name nn Email: $email_address nnMessage: nn $message.";
}
// if the upload succeded, the file will exist
if($tmp_name != ''){
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
}
}
// now we'll build the message headers
$headers = "From: $fromrn";
if( $tmp_name != '' ){
$headers .= "MIME-Version: 1.0rn" .
"Content-Type: multipart/mixed;rn" ;
// next, we'll build the message body
// note that we insert two dashes in front of the
// MIME boundary when we use it
$message = "This is a multi-part message in MIME format.nn" .
"Content-Type:text/plain;charset=iso-8859-1" .
"Content-Transfer-Encoding: 7bitnn" .
$message . "nn";
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content and set another boundary to
// indicate that the end of the file has been reached
$message .=
"Content-Type: ".$type."" .
" name=".$file_name."n" .
//"Content-Disposition: attachment;n" .
//" filename="{$fileatt_name}"n" .
"Content-Transfer-Encoding: base64nn" .
$data . "nn" ;
}
// now we just send the message
if (mail("mohd.gadiwala@techmatters.com", $subject, $message, $headers))
echo "<div class='msg msg-ok'><p><strong>Message Sent</strong></p></div><br><br>";
else
echo "<div class='msg msg-ok'><p><strong>Message sending failed</strong></p></div><br><br>";
}
?>
<html>
<body>
<form id="comment" action="atta.php" method="post" enctype="multipart/form-data">
<label>Name <span></span></label>
<input type="text" name="name" id="name">
<label>Email <span></span></label>
<input type="text" name="email" id="email">
<label>Comment <span></span></label>
<input type="text" name="comment" id="email">
<label>Upload file <span></span></label>
<input type="file" name="filename" id="file">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>