スクリプトでいくつかの問題が発生しています。基本的に、スクリプトに実行させたいのは、フォームデータを取得してデータベースに投稿することです (これは問題なく実行されます)。次に、感謝のメールを送信します。うまくいかない、うまくいかない。また、スクリプトの実行に失敗した場合でも、スクリプトは電子メールを送信します。これを回避するにはどうすればよいですか。
私は PHP にかなり慣れていないので、何ができるかを発見したばかりなので、本当に助けていただきありがとうございます。
<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$name = $_POST['name'];
$email = $_POST['email'];
require_once('Connections/connection.php');
mysql_select_db($database_connection);
$query = "INSERT INTO mailing_list ( name, email)
VALUES ( '$name', '$email');";
mysql_query($query);
mysql_close();
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'mailinglist@myemail.com';
$email_subject = "Welcome to our mailinglist";
$email_body = "
$name,
Welcome to our mailing list, we will now keep you updated on whats going on here.
To unsubscribe go to.".
$to = "$email";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.php');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>