我正在使用PHP、HTML、SMTP 服務器和 PHPmailer 創(chuàng)建一個帶有用戶輸入驗證的聯(lián)系我們表單。但是按“提交”按鈕后,它給了我一個錯誤:
無效地址:(發(fā)件人): 致命錯誤:未捕獲 PHPMailer\PHPMailer\Exception:無效地址:(來自):在 C:\xampp\htdocs\RESPONSIVE 中 SITE3_2\供應商\phpmailer\phpmailer\src\PHPMailer.php:1324 堆棧跟蹤: #0 C:\xampp\htdocs\RESPONSIVE SITE3_2\send-email.php(74): PHPMailer\PHPMailer\PHPMailer->setFrom('', '') #1 {main} 拋出在 C:\xampp\htdocs\RESPONSIVE SITE3_2\vendor\phpmailer\phpmailer\src\PHPMailer.php 第 1324 行
PHP 代碼:
<?php $name = $_POST["name"]; $email = $_POST["email"]; // $email = test_input($_POST["email"]); $message = $_POST["message"]; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; } if (!preg_match("/^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+$/", $email)) { $emailErr = "Email should contain only letters, numbers, and underscores"; } require "vendor/autoload.php"; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; $mail = new PHPMailer(true); $mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->isSMTP(); $mail->SMTPAuth = true; $mail->Host = "smtp.gmail.com"; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; require_once 'config.php'; $mail->Username = SMTP_USERNAME; $mail->Password = SMTP_PASSWORD; $mail->setFrom($email, $name); $mail->addAddress("myemail@gmail.com", "Ads"); $mail->Subject = $subject; $mail->Body = "Name: $name\nEmail: $email\n\n$message"; $mail->send(); header("Location: sent.html"); ?>
HTML 代碼
<form method="POST" action="send-email.php"> <input type="text" name="name" id="name" placeholder="Name*"> <input type="email" name="email" id="email" placeholder="Email*"> <span class="error" style="color:red"><?php echo $emailErr;?></span> <textarea name="message" id="message" placeholder="Your Message*"></textarea> <button type="submit" name="submit" id="submit" class="button">Start</button> </form>
我嘗試了不同的教程 - 不成功。 我需要此表格
我對此很陌生,請用非常簡單的語言解釋一下:)
診斷
如果任一驗證 if
語句為 true,則將設置一個字符串變量 $emailErr
。
然后代碼再次退出 if
塊,腳本繼續(xù)其愉快的方式并嘗試發(fā)送電子郵件。代碼中沒有任何邏輯可以阻止這種情況。
最后,$emailErr
永遠不會被使用,因為代碼將用戶重定向到另一個不能涉及該變量的 HTML 頁面。
解決方案
如果任何驗證失敗,您需要一些額外的邏輯來告訴代碼跳過電子郵件發(fā)送部分。巧妙地實現(xiàn)此目的的一種方法非常簡單,即使用“標志”變量。
例如:
<?php require "vendor/autoload.php"; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; $name = $_POST["name"]; $email = $_POST["email"]; // $email = test_input($_POST["email"]); $message = $_POST["message"]; $valid = true; $emailErr = ""; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; $valid = false; } if (!preg_match("/^[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+$/", $email)) { $emailErr = "Email should contain only letters, numbers, and underscores"; $valid = false; } //only send if all validations passed: if ($valid == true) { $mail = new PHPMailer(true); $mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->isSMTP(); $mail->SMTPAuth = true; $mail->Host = "smtp.gmail.com"; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; require_once 'config.php'; $mail->Username = SMTP_USERNAME; $mail->Password = SMTP_PASSWORD; $mail->setFrom($email, $name); $mail->addAddress("myemail@gmail.com", "Ads"); $mail->Subject = $subject; $mail->Body = "Name: $name\nEmail: $email\n\n$message"; $mail->send(); header("Location: sent.html"); } else { echo $emailErr; }
附注順便說一句,您的正則表達式限制太多 - 請參閱 允許使用哪些字符在電子郵件地址中?。由于您已經(jīng)在使用 FILTER_VALIDATE_EMAIL
,因此在任何情況下您都不需要它。