即時獲取「無效的電子郵件地址」PHP郵件沒有發送「無效的電子郵件地址」
所有硬編碼測試,缺少什麼?謝謝!
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['[email protected]'];
$subject = $HTTP_POST_VARS['subjectaaa'];
$message = $HTTP_POST_VARS['messageeeee'];
/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
這與PHP沒有發送郵件沒有關係..你使用的是一個錯誤的正則表達式來驗證電子郵件地址。你的代碼永遠不會接近實際調用'mail()',因爲你永遠不會放過它。您應該在FILTER_VALIDATE_EMAIL中使用'filter_var',而不是滾動您自己的損壞系統。 –
如果您剛剛編寫了這個PHP代碼,並且您仍在學習,請發現自己比您使用的教程更新的教程 - 您正在使用一些**糟糕的**過時的技術在這裏。 – Spudley