July 24, 201115 yr I am having little problems with my PHP contact page: - 1. When I check my inbox, I get a message saying: - Mail failure - malformed recipient address A message that you sent contained a recipient address that was incorrectly constructed: from: missing or malformed local part (expected word or "<") The message has not been delivered to any recipients. ------ This is a copy of your message, including all the headers. ------ 2. When I try to put a invalid email to test it for the alert boxes, instead a small box appears beneath it instead of an alert box. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled</title> </head> <body> <?php if(isset($_POST["submit"])){ $errors = ''; $email_address = $_POST['email']; $message = $_POST['message']; $name = $_POST['name']; //Checks to see if any fields are empty if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) { $errors .= "\n Error: all fields are required"; echo '<script type="text/javascript"> alert("Please check you have filled out all necessary fields"); </script>'; } else //Checks to see if email address entered is valid if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Error: Invalid email address"; echo '<script type="text/javascript"> alert("Please check you have entered a valid email address"); </script>'; } if( empty($errors)){ $myemail = "me@me.com"; $to = $myemail; $from = $_POST["email"]; $name = $_POST["name"]; $message = $_POST["message"]; $headers = 'This message is from: ' . $from; mail($to,$from,$name,$message,$headers); echo '<script type="text/javascript"> alert("Your message has been successfully sent."); </script>'; } else { '<script type="text/javascript"> alert("Message delivery unsuccessful."); </script>'; } } ?> </body> </html> Edited July 24, 201115 yr by RiskyShenanigan
July 24, 201115 yr Your $headers variable is invalid, and your mail() line is wrong. Use this mail line: mail($to, "Contact Form Subject", $message, "From: ".$name." <".$from.">"); Edited July 24, 201115 yr by andyl
July 24, 201115 yr Author Your $headers variable is invalid, and your mail() line is wrong. Use this mail line: mail($to, "Contact Form Subject", $message, "From: ".$name." <".$from.">"); Doesn't seem to work, I don't even get the mail in the inbox.
July 24, 201115 yr I guessed as much. That script is horribly written. Here's a tidy version: <?php // CHANGE THIS $EMAIL_TO = "YOU@YOURDOMAIN.COM"; if (isset($_POST['submit'])) { function filterInjects($input){ return preg_replace("/([A-z-]*:)/", "", $input); } $email = filterInjects($_POST['email']); $name = filterInjects($_POST['name']); $message = $_POST['message']; if (empty($email) || empty($name) || empty($message)) { echo "<h1>All fields are required.</h1>"; } elseif (!preg_match("/^[a-z`'. _-]+$/i", $name)) { echo "<h1>Please enter a valid name.</h1>"; } elseif (!preg_match("/^[_a-z0-9.-]+@[a-z.-]+\.[a-z]{2,3}$/i", $email)) { echo "<h1>Please enter a valid email.</h1>"; } elseif (!mail($EMAIL_TO, "Contact Form Submission", $message, "From: ".$name." <".$email.">")) { echo "<h1>Could not send mail, please try again.</h1>"; } else { echo "<h1>Email sent, thank-you.</h1>"; } } else { echo "<h1>You cannot access this page directly.</h1>"; } ?> Edited July 24, 201115 yr by andyl
Create an account or sign in to comment