I have a code for sending a contact form content with attachment to email,but in my query the contend are not showing in email body,so i choose to show it in header so please help to figure it out......
<?php
// Settings
$name = "A";
$email = "a@q.com";
$email2 = "v@c.in";
$to = "$name <$email>,<$email2>";
$from = $_POST["EEmail"];
$subject2 = "message from contact page";
$name1 = $_POST["SName"];
$phone = $_POST["Pphone"];
$subject1 = $_POST["txtSubject"];
$description = nl2br($_POST["txtDescription"]);
$fileatt = ".docx";
$fileatttype = "application/docx";
$fileattname = $_FILES["fileAttach"]["name"];
$headers = "From: $from";
$subject = "name:$name1,"."Phone:$phone,"."Email
Id:$from,"."Subject:$subject1,"."Comment:$description.";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "
MIME-Version: 1.0
" .
"Content-Type: multipart/mixed;
" .
" boundary=\"{$mime_boundary}\"";
$body = "This is a multi-part message in MIME format.
" .
"-{$mime_boundary}
" .
"Content-Type: text/plain; charset=\"iso-8859-1
" .
"Content-Transfer-Encoding: 7bit
" .
$data = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"] ["tmp_name"])));
$body .= "--{$mime_boundary}
" .
"Content-Type: {$fileatttype};
" .
" name=\"{$fileattname}\"
" .
"Content-Disposition: attachment;
" .
" filename=\"{$fileattname}\"
" .
"Content-Transfer-Encoding: base64
" .
"
" ."-{$mime_boundary}-
";
// Send the email
mail($to, $subject, $headers , $body);
?>
Use phpmailer https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
How to use:
<?php
include("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "abc@xyz.com";
$mail->FromName = "My Name";
$mail->AddAddress("recipient@gmail.com");
$mail->AddAttachment("uploads/".$profile_photo_name); //Path to the file to be attached
$mail->IsHTML(true); //Set the email type to rich HTML
$mail->Subject = "Your subject";
$mail->Body = "Your email body"; //Body for rich HTML mail
if($mail->Send()) //Send email
{
echo "Mail sent";
}
else
echo "Failure";
?>
For one thing, you have your $headers
and $body
placement mixed up.
You have:
mail($to, $subject, $headers , $body);
where it should be:
mail($to, $subject, $body, $headers);
The body must come become headers.
As per the PHP manual http://php.net/manual/en/function.mail.php