John Davidson

contact form - PHP Code with broken syntax works, when I rewrite it properly it breaks


I'm new to PHP and as a web developer have only been using it to write simple contact forms. Recently I was making a contact form with a file upload feature, not knowing any PHP I found a solution online here. I like it quite a bit because it sends an email from the domain to a personal gmail, which wasn't possible with my old PHP code. I tried to change it up a bit so the $message formatting looks a bit better and so I can use it as a contact form WITHOUT file upload. This is what I got:


$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['tel'];
$subj = $_POST['subject'];
$usermessage = $_POST['message'];

// $message ="Name = ". $name . "\r\n Email = ". $email . "\r\n Phone = ". $phone . "\r\n Message = ". $usermessage; ------- original $message code with what looks like broken syntax

// new $message code that does not work
$message = "Name: ".$name. "\r\n";
$message .= "Email: ".$email. "\r\n";
$message .= "Phone: ".$phone. "\r\n";
$message .= "Message: ".$usermessage. "\r\n";

$subject = $subj;
$fromname ="Someone";
$fromemail = '[email protected]';
$mailto = '[email protected]';

// $content = file_get_contents($fileName);
// $content = chunk_split(base64_encode($content));

// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment

// $body .= "--" . $separator . $eol;
// $body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
// $body .= "Content-Transfer-Encoding: base64" . $eol;
// $body .= "Content-Disposition: attachment" . $eol;
// $body .= $content . $eol;
// $body .= "--" . $separator . "--";

//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // do what you want after sending the email
header('Location: success.html');

} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );

Basically I just commented out what looked like code for the file, so it wouldn't send a 'noname' attachment with no file extension. That worked, but the formatting had some weird indentations, random text color changes to a a:visited blue/purple, and the = wasn't visually appealing, I'd rather have a ':'. Plus, the syntax looked broken with quotations in the wrong place. So I made the new message code that can be clearly seen, and commented out the original broken one line $message code, and now the email sends with no content. How is this possible? How do I fix this code?

It seems the slightest thing breaks the code to where a message sends completely empty or only one field sends. Commenting a line out, replacing the '=' with ':' in the original $message code, deleting comments, all could possibly break this again.

My Answer:

$to = "[email protected]";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact Form Submission";
$message = "Name: " . $name . "\n";
$message .= "Email: " . $from . "\n";
$message .= "Message: " . $_POST['message'];

$headers = "From:" . $from;

if(mail($to, $subject, $message, $headers)){
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
echo "Failed to send email. Please try again.";

This code should work properly for a basic contact form without file upload. Make sure to replace "[email protected]" with your actual email address where you want to receive the form submissions. Also, ensure that your server is properly configured to send emails.

Rate this post

3 of 5 based on 8601 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us