I'm working on file attachment here mail function is working fine I'm getting all fields through mail accept file upload field is not coming. I have tried using Content-Type: multipart/mixed
and some other methods but unable to achieve the desired output. I have servey and find the different answer and tried but still facing the same issue. Can anyone suggest to me according to my script how should I get the file attachment.
HTML
<input id="file-upload" name="upload" type="file" required>
PHP mail function
<?php
// Receiver mail id
$mail_to = 'yourmail@gmail.com';
// Mail Subject
$subject = 'title';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ( isset($_POST['name']) ) {
$name = $_POST['name'];
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
}
if (isset($_POST['company'])) {
$company = $_POST['company'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
if(isset($_POST['industry'])) {
$industry = $_POST['industry'];
}
if(isset($_POST['job'])) {
$job = $_POST['job'];
}
if(isset($_POST['upload'])) {
$upload = $_POST['upload'];
}
// Message body
$msg = '<html><body><p>';
$msg .= '<b> Name : </b>' . $name . '<br/>';
if($_POST["phone"] != "") {
$msg .= '<b> Phone : </b>' . $phone . '<br/>';
}
if($_POST["company"] != "") {
$msg .= '<b> Company : </b>' . $company . '<br/>';
}
if($_POST["message"] != "") {
$msg .= '<b> Message : </b>' . $message . '<br/>';
}
if($_POST["industry"] != "") {
$msg .= '<b> Industry : </b>' . $industry . '<br/>';
}
if($_POST["job"] != "") {
$msg .= '<b> Job Role : </b>' . $job . '<br/>';
}
if($_POST["upload"] != "") {
$msg .= '<b> Upload : </b>' . $upload . '<br/>';
}
$msg .= '</p>';
$msg .= '</body></html>';
// Mail headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: yourmail@gmail.com' . "\r\n";
if( mail( $mail_to, $subject, $msg, $headers )) {
echo "Thank You!";
} else {
die("Error!");
}
}
?>
I had tried like this here only file coming other fields not coming in the mail. what I'm missing here.
<?php
// Receiver mail id
$mail_to = 'yourmail@gmail.com';
// Mail Subject
$subject = 'project';
$path = 'assets/file';
$filename = 'myfile';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ( isset($_POST['name']) ) {
$name = $_POST['name'];
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
}
if (isset($_POST['company'])) {
$company = $_POST['company'];
}
if(isset($_POST['message'])) {
$message = $_POST['message'];
}
if(isset($_POST['industry'])) {
$industry = $_POST['industry'];
}
if(isset($_POST['job'])) {
$job = $_POST['job'];
}
if(isset($_POST['upload'])) {
$upload = $_POST['upload'];
}
// Message body
$msg = '<html><body><p>';
$msg .= '<b> Name : </b>' . $name . '<br/>';
if($_POST["phone"] != "") {
$msg .= '<b> Phone : </b>' . $phone . '<br/>';
}
if($_POST["company"] != "") {
$msg .= '<b> Company : </b>' . $company . '<br/>';
}
if($_POST["message"] != "") {
$msg .= '<b> Message : </b>' . $message . '<br/>';
}
if($_POST["industry"] != "") {
$msg .= '<b> Industry : </b>' . $industry . '<br/>';
}
if($_POST["job"] != "") {
$msg .= '<b> Job Role : </b>' . $job . '<br/>';
}
if($_POST["upload"] != "") {
$msg .= '<b> Upload : </b>' . $upload . '<br/>';
}
$msg .= '</p>';
$msg .= '</body></html>';
$file = $path.$filename;
$content = file_get_contents( $file);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$uploadname = basename($file);
$replyto = 'test';
$headers = "From: ".$subject." <".'yourmail@gmail.com'.">\r\n";
$headers .= "Reply-To: ".$replyto."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$msg = "--".$uid."\r\n";
$msg .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$msg .= $msg."\r\n\r\n";
$msg .= "--".$uid."\r\n";
$msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$msg .= $content."\r\n\r\n";
$msg .= "--".$uid."--";
if( mail( $mail_to, $subject, $msg, $headers )) {
echo "Thank You!";
} else {
die("Error!");
}
}
?>