Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problems with PHP contact page...

Featured Replies

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 by RiskyShenanigan

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 by andyl

  • 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.

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 by andyl

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.