File not being attached for mailing - php

I have been trying to create a html form for users to send a mail with an attachment using the php mail function. The mail gets sent successfully but without the attachment. When i tried to check the file being uploaded I don't seem to be getting the file selected by the user. Below is my html code
<form name="message" id="message" method="post" action="mail.php" enctype="multipart/form-data">
<input type="text" class ="text-class" id="name" name="name" placeholder="Name*" required/>
<input type="email" class ="text-class" placeholder="Email*" name="Email" id ="Email" required/>
<textarea class ="text-class" placeholder="Message*" name="Message" id="Message" required></textarea>
<div class="captcha">
<div class="row">
<div class="4u">
<input class ="text-class" type="text" value="2 + 3 is ?" name="question" readonly/>
</div>
<div class="8u">
<input class ="text-class" type="text" placeholder="Answer*" name="captcha" id="captcha" required/>
</div>
</div>
</div>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="attachment" id="attachment"/>
<input type="submit" name="Submit" value="Send" class="submit"/>
</form>
mail.php
<?php
if(isset($_POST['Submit']) && ($_POST['Submit']) == 'Send' )
{
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["Email"]==""||$_POST["Message"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['Email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$message = $_POST['Message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
if((empty($_POST['attachment'])) || (empty($_FILES['attachment']))){
echo "No attachement!";
}
else{
//file is attached, process it and send via mail
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
mail("mail#test.com", "Mail from my website", $message, $headers);
echo "Your message has been received by us ! Thank you for contacting us";
echo "<script>
alert('Your message has been sent!');
</script>";
}
exit;
}
}
}else{
echo "Not submitted";
}
?>
I get "No attachment" messaged echoed since I don't seem to be getting the selected file from the html form. Please can someone tell me where I'm going wrong?

Related

unable to see status message after sending an email from contact page in html using php

I am trying to send an email from a contact page. the functionality is working fine, I am able to send mails from the html page but the only issue that I am facing is I am unable to see the Status div(success or failed).
Initially the page was redirecting to php file without any status message. I have added page redirect to the actual mailsend.html using header() in php. Now I want to have a status after the send operation whether mail has sent or not.
Below is the code snippet. Please help. Thanks in advance.
mailSend.html code:
<?php if(!empty($statusMsg)){ ?>
<p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
<?php } ?>
<form action="example.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<input style = "padding-left:2%; width: 97%;" type="text" name="name" class="form-control" value="" placeholder="Name" required="">
</div>
<div class="form-group">
<input style = "padding-left:2%; width: 97%;" type="email" name="email" class="form-control" value="" placeholder="Email address" required="">
</div>
<div class="form-group">
<input style = "padding-left:2%; width: 97%;" type="text" name="subject" class="form-control" value="" placeholder="Subject" required="">
</div>
<div class="form-group">
<textarea name="message" class="form-control" placeholder="Write your message here" required="" style = 'border :0.5px solid '></textarea>
</div>
<div class="form-group">
<input type="file" name="attachment" class="form-control" style = 'border :0.5px solid; height: auto;'>
</div>
<div class="submit">
<input type="submit" name="submit" class="btn" value="SUBMIT" style= 'float : right;'>
</div>
</form>
example.php code:
<?php
//first we leave this input field blank
$recipient = "";
//if user click the send button
if(isset($_POST['submit'])){
//access user entered data
$recipient = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$sender = "From: xyz#gmail.com";
//if user leave empty field among one of them
if(empty($recipient) || empty($subject) || empty($message)){
?>
<!-- display an alert message if one of them field is empty -->
<div class="alert alert-danger text-center">
<?php echo "All inputs are required!" ?>
</div>
<?php
}else{
$uploadStatus = 1;
// Upload attachment file
if(!empty($_FILES["attachment"]["name"])){
// File path config
$targetDir = "uploads/";
$fileName = basename($_FILES["attachment"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
// Allow certain file formats
$allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
if(in_array($fileType, $allowTypes)){
// Upload file to the server
if(move_uploaded_file($_FILES["attachment"]["tmp_name"], $targetFilePath)){
$uploadedFile = $targetFilePath;
}else{
$uploadStatus = 0;
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$uploadStatus = 0;
$statusMsg = 'Sorry, only PDF, DOC, JPG, JPEG, & PNG files are allowed to upload.';
}
}
if($uploadStatus == 1){
// Recipient
$toEmail = 'abc#gmail.com';
// Sender
$from = 'xyz#gmail.com';
$fromName = 'example';
// Subject
$emailSubject = 'Contact Request Submitted by '.$recipient;
// Message
$htmlContent = '<h2>Contact Request Submitted</h2>
<p><b>Name:</b> '.$recipient.'</p>
<p><b>Email:</b> '.$sender.'</p>
<p><b>Subject:</b> '.$subject.'</p>
<p><b>Message:</b><br/>'.$message.'</p>';
// Header for sender info
$headers = "From: $fromName"." <".$from.">";
if(!empty($uploadedFile) && file_exists($uploadedFile)){
// Boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// Multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
// Preparing attachment
if(is_file($uploadedFile)){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($uploadedFile,"rb");
$data = #fread($fp,filesize($uploadedFile));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($uploadedFile)."\"\n" .
"Content-Description: ".basename($uploadedFile)."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($uploadedFile)."\"; size=".filesize($uploadedFile).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $recipient;
// Send email
$mail = mail($toEmail, $emailSubject, $message, $headers, $returnpath);
// Delete attachment file from the server
#unlink($uploadedFile);
}else{
// Set content-type header for sending HTML email
$headers .= "\r\n". "MIME-Version: 1.0";
$headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
// Send email
$mail = mail($toEmail, $emailSubject, $htmlContent, $headers);
}
// If mail sent
if($mail){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
?>
<!-- display a success message if once mail sent sucessfully -->
<div class="alert alert-success text-center">
<!--<?php echo "Your mail successfully sent to $recipient"?>-->
<!--readfile('submitResume.html');-->
<?php
header('Location: mailSend.html') ;
echo "Your mail successfully sent to $recipient"
?>
</div>
<?php
$recipient = "";
$postData = '';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
?>
<!-- display an alert message if somehow mail can't be sent -->
<div class="alert alert-danger text-center">
<?php echo "Failed while sending your mail!" ?>
</div>
<?php
}
}
}
}
?>
You are sending to a .html page, which does not process PHP code by default, the server just serves it unless specifically configured on the server. Rename the page from mailSend.html to mailSend.php and it should resolve it. Make sure to change your code to send to .php page.
For further reading see here
You would need to pass the message itself or a way for the script to know which message to show. The easiest way would be to pass it via $_GET, by attaching it to the end of the URL you are trying to redirect. Like so:
$target_url = mailSend.php;
$get_data = '?statusMsg=' . urlencode($statusMsg) . '&$msgClass=' . urlencode($msgClass);
header( 'Location: ' . $target_url . $get_data );
Which you can then recover on mailSend.php via the global $_GET variable. Such as:
$statusMsg = urldecode($_GET['statusMsg']);
$msgClass= urldecode($_GET['msgClass']);
There are other ways to get the data from one page to another but that I will leave it up to you to do research. As it is out of scope for a simple answer.

display a message after submit and refresh

i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.
i have no knowledge of PHP so sorry is its and obvious answer
thanks
my html & PHP if needed
<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
<input type="text" id="name" name="name" placeholder="name" required="required">
<input type="email" id="email" name="email" placeholder="email" required="required">
<input type="tel" id="phone" name="phone" placeholder="phone">
<div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
<textarea id="message" name="message" placeholder="message" style="height:200px" required="required"></textarea>
<input id="submit" type="submit" value="submit">
</form>
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your message has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Update your php sending email to this
if (mail($myEmail, $subject, $message)){
$success = "Message sent successful";
}else{
$success = "Message Sending Failed.";
}
Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
You may even add some styling on the message if you prefer.

PHP attachment form after submitting getting blank page

after submitting my form am getting the mail but its going to white black screen with message of message send.
I try to reload the page after the submit the form but getting error.
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$to = "name#gmail.com";
$subject = "E-mail with attachment";
$from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";
// generate a random string to be used as the boundary marker
$mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "Canditade Resume";
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// iterating each File type
print_r($_FILES);
foreach ($_FILES as $userfile) {
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name)) {
if (is_uploaded_file($tmp_name)) {
$file = fopen($tmp_name, 'rb');
$data = fread($file, filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$tmp_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (#mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
?>
<form action="index.php" method="post" enctype="multipart/form-data" name="form1">
<label>Name</label>
<input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
<label>Email Address</label>
<input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
<label>Designation</label>
<input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
<label>Upload Your CV</label>
<input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
</div>
<input type="submit" name="Submit" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
</form>
<?php } ?>
please help me
if (#mail($to, $subject, $message, $headers))
//echo "Message Sent";
header('location:email-success.php');
else
//echo "Failed to send";
header('location:email-error.php');
}
Give page name in header location to which you want to redirect it.
Change
if (#mail($to, $subject, $message, $headers))
echo "Message Sent";
else
echo "Failed to send";
} else {
To
if (#mail($to, $subject, $message, $headers))
header("location:yourpage.php?message=Message Sent");
else
header("location:yourpage.php?message=Failed to send");
} else {
yourpage.php
<?php
if(isset($_GET['message'])){
echo $_GET['message'];
}
.
.
?>
Your coding is a little messed. Here's the adjustments I made:
I also commented out the print_r($_FILES) as this will display the array of what has been passed through which you do not want to display when a user presses submit.
So I renamed the $_POST and made it an isset:
<?php
if (isset($_POST['send'])) {
$to = "name#gmail.com";
$subject = "E-mail with attachment";
$from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";
// generate a random string to be used as the boundary marker
$mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";
// now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
" boundary=\"{$mime_boundary}\"";
$message = "Canditade Resume";
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// iterating each File type
//print_r($_FILES);
foreach ($_FILES as $userfile) {
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name)) {
if (is_uploaded_file($tmp_name)) {
$file = fopen($tmp_name, 'rb');
$data = fread($file, filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$tmp_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message .= "--{$mime_boundary}--\n";
// now we just send the message
if (#mail($to, $subject, $message, $headers)) {
echo "Message Sent";
} else {
echo "Failed to send";
}
}
?>
Not sure why you wrapped the PHP around the html side but that would be why you wasn't being taken back to the form side and just a blank page.
<form action="" method="post" enctype="multipart/form-data" name="form1">
<label>Name</label>
<input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
<label>Email Address</label>
<input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
<label>Designation</label>
<input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
<label>Upload Your CV</label>
<input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
</div>
<input type="submit" name="send" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
</form>
I have tested this code and it redirects back to the form with the "Message sent" above the form (as you'd see on any contact form).

Website form - Multiple file upload [duplicate]

This question already has answers here:
Multiple file upload in php
(14 answers)
Closed 8 years ago.
So I have searched through a lot of questions similar to this but cannot seem to find a solution.
I have a contact form and I want it to be able to send multiple images. The code I have now has three input fields for pictures but right now I have only figured out how to get one of the pictures to upload and send. I am pretty sure I need some kind of loop or array but that is not a strong point of mine.
HTML
<form action="test.php" method="post" autocomplete="on" enctype="multipart/form-data" accept-charset='UTF-8'>
<fieldset><label for="Name">Name <sup>*</sup></label>
<input name="name" placeholder="full name" id="Name" required="" autofocus><br>
<label for="email">E-mail <sup>*</sup></label>
<input type="email" name="email" placeholder="youremail#domain.com" id="email" required=""><br>
<label for="phoneNumber">Phone # <sup>*</sup></label>
<input type="tel" name="phone" placeholder="XXX-XXX-XXXX" id="phoneNumber" required=""><br>
</fieldset>
<fieldset>
<label for="year">Year <sup>*</sup></label>
<input type="text" name="year" placeholder="year of car" id="year" required=""><br>
<label for="make">Make <sup>*</sup></label>
<input type="text" name="make" placeholder="make of car" id="make" required=""><br>
<label for="model">Model <sup>*</sup></label>
<input type="text" name="model" placeholder="model of car" id="model" required=""><br>
</fieldset>
<label for="State" class="state">State <sup>*</sup></label><br>
<input type="text" class="state" name="state" id="state" placeholder="Do not enter if you are human">
<fieldset>
<legend>Add Photos <sup>*</sup></legend>
<input type="file" name="attachment" required=""><br>
<input type="file" name="attachment"><br>
<input type="file" name="attachment"><br>
</fieldset>
<label for="comments">Comments</label><br>
<textarea style="float: left;" rows="4" name="comment" id="comments" placeholder="additional comments"></textarea>
<br>
<input class="button" type="submit" name="submit" value="Send" class="button">
<p class="required"><sup>*</sup> denotes a required field.</p>
</form>
PHP
<?php
$to = '';
$name = $_POST['name'];
$email = $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "
Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment
";
if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
else{
//process the rest of the form
}
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $name $email";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent. We will get back to you as soon as
possible.";
}
else{
echo "Error in Email sending";
}
?>
All three of your fields are named "attachment". Give them unique names like "attachment1", "attachment2", etc.
Then use this on your upload:
for ($x = 1; $x <= $numberOfFiles; ++$x) {
$tmpName = $_FILES['attachment'.$x]['tmp_name'];
$fileType = $_FILES['attachment'.$x]['type'];
$fileName = $_FILES['attachment'.$x]['name'];
// do your upload here
}
You could even do it in a loop like this, which would allow you to add more file fields (maybe with JavaScript) without changing the backend code, as long as you kept the naming pattern consistent.
$x = 1;
while (isset($_FILES['attachment'.$x])) {
$tmpName = $_FILES['attachment'.$x]['tmp_name'];
$fileType = $_FILES['attachment'.$x]['type'];
$fileName = $_FILES['attachment'.$x]['name'];
// do your upload here
++$x;
}
That just increments x on each cycle, and checks to see if a file was sent with that name.
You should probably also include some checking on the size and type of files, before someone fills your inbox with huge files and/or cyber-nasties.
UPDATE: This should be a mostly-complete solution for you. I'm not able to test it for you at the moment so it may not work straight out of the box, but it should at least point you in the right direction. In this code snippet it sets up the initial part of the email first, then loops through and adds each file one at a time.
<?php
$to = '';
$name = $_POST['name'];
$email = $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment";
if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
/* Start of headers */
$headers = "From: $name $email";
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Loop through files */
$x = 1;
while (isset($_FILES['attachment'.$x])) {
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Skip invalid files */
if (!file($tmpName)) {
++$x;
continue;
}
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
++$x;
}
/* Close the message */
$message .= "--{$mimeBoundary}--\n";
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent. We will get back to you as soon as possible.";
} else {
echo "Error in Email sending";
}
?>

html showing in php mail

I'm trying to send a php mail but it seems that I have a error in my foreach because the mail shows html..
This is my code:
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<label for="fullname">Votre Nom :
<input id="fullname" name="fullname" type="text" value="nelson" />
</label>
<label for="emailaddress" class="margin">Votre e-mail:
<input id="email" name="email" type="text" value="" />
</label>
<label for="message">Message:<br />
<textarea id="message" name="message" cols="40" rows="8"></textarea>
</label>
<p>
<input id="submit-button" class="button gray stripe" type="submit" name="submit" value="Envoyer le message" />
</p>
</fieldset>
</form>
<?php
foreach ($_POST as $value) {
$value = strip_tags($value);
$value = htmlspecialchars($value);
}
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
if (isset($_POST['submit'])) {
mail($to, $suject, $message);
echo"mail had been sent";
}
?>
Can anyone help me please
You need to set the Content-type header in your email message:
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $suject, $message, $headers);
echo"mail had been sent";
Your message body also needs to be contained in <html> tags.
Your foreach is kind of useless, just do that. More fast than a loops
$name = strip_tags(htmlspecialchars($_POST["fullname"]));
$email = "email:" .strip_tags(htmlspecialchars($_POST["email"]));
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .strip_tags(htmlspecialchars($_POST["message";));
To send email containing HTML you must set the header so that the email client knows that the email contains HTML. You also have to make the body of your email an HTML document.
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $suject, $message, $header);
And then in the message itself:
<html>
<head></head>
<body>
Content here
</body>
</html>

Categories