Latest web development tutorials

PHP Security E-mail

On a PHP e-mail in the script, there is a loophole.


PHP E-mail injection

First, look at the last chapter of the PHP code:

<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
</head>
<body>

<?php
if (isset($_REQUEST['email'])) { // 如果接收到邮箱参数则发送邮件
	// 发送邮件
	$email = $_REQUEST['email'] ;
	$subject = $_REQUEST['subject'] ;
	$message = $_REQUEST['message'] ;
	mail("someone@example.com", $subject,
	$message, "From:" . $email);
	echo "邮件发送成功";
} else { // 如果没有邮箱参数则显示表单
	echo "<form method='post' action='mailform.php'>
	Email: <input name='email' type='text'><br>
	Subject: <input name='subject' type='text'><br>
	Message:<br>
	<textarea name='message' rows='15' cols='40'>
	</textarea><br>
	<input type='submit'>
	</form>";
}
?>

</body>
</html>

Problems above code is that unauthorized users can insert data in the message header by input form.

If the user in the form of the input box by adding the following text into an email, what will happen?

someone@example.com%0ACc:person2@example.com
%0ABcc:person3@example.com,person3@example.com,
anotherperson4@example.com,person5@example.com
%0ABTo:person6@example.com

As usual, mail () function to the above text into the message header, so now the head with extra Cc:, Bcc: and To: fields. When the user clicks the submit button, to this e-mail will be sent to all the above address!


PHP injection to prevent E-mail

The best way to prevent e-mail injection is to validate the input.

The following code is similar to the last chapter, but here we have increased the detection of form input validation program email field:

<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
</head>
<body>
<?php
function spamcheck($field)
{
	// filter_var() 过滤 e-mail
	// 使用 FILTER_SANITIZE_EMAIL
	$field=filter_var($field, FILTER_SANITIZE_EMAIL);

	//filter_var() 过滤 e-mail
	// 使用 FILTER_VALIDATE_EMAIL
	if(filter_var($field, FILTER_VALIDATE_EMAIL))
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

if (isset($_REQUEST['email']))
{
	// 如果接收到邮箱参数则发送邮件

	// 判断邮箱是否合法
	$mailcheck = spamcheck($_REQUEST['email']);
	if ($mailcheck==FALSE)
	{
		echo "非法输入";
	}
	else
	{	
		// 发送邮件
		$email = $_REQUEST['email'] ;
		$subject = $_REQUEST['subject'] ;
		$message = $_REQUEST['message'] ;
		mail("someone@example.com", "Subject: $subject",
		$message, "From: $email" );
		echo "Thank you for using our mail form";
	}
}
else
{ 
	// 如果没有邮箱参数则显示表单
	echo "<form method='post' action='mailform.php'>
	Email: <input name='email' type='text'><br>
	Subject: <input name='subject' type='text'><br>
	Message:<br>
	<textarea name='message' rows='15' cols='40'>
	</textarea><br>
	<input type='submit'>
	</form>";
}
?>

</body>
</html>

In the above code, we use the PHP filter to verify the input:

  • FILTER_SANITIZE_EMAIL filter delete e-mail from a string of illegal characters
  • FILTER_VALIDATE_EMAIL filter verify the value of the e-mail address

You can in our PHP Filter Read more about filters knowledge.