Why don't these input validations work? (Contact us via PHP & HTML & SMTP Server & PHPmailer)
P粉613735289
P粉613735289 2023-09-06 21:34:49
0
1
382

I'm usingPHP, HTML, SMTP server and PHPmailerto create a contact us form with user input validation. But after pressing "Submit" button it gives me an error:

Invalid address: (Sender): Fatal error: Uncaught PHPMailer\PHPMailer\Exception: Invalid address: (from): in C:\xampp\htdocs\RESPONSIVE SITE3_2\Supplier\phpmailer\phpmailer\src\PHPMailer.php:1324 Stack trace: #0 C:\xampp\htdocs\RESPONSIVE SITE3_2\send-email.php(74): PHPMailer\PHPMailer\PHPMailer->setFrom('', '') #1 {main} throws in C:\xampp\htdocs\RESPONSIVE SITE3_2\vendor\phpmailer\phpmailer\src\PHPMailer.php line 1324

PHP code:

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 code

I tried different tutorials - without success. I need this form

  • Validate user input, only allow numbers and letters to be inserted (mail fields)
  • If the data is invalid, an error message must appear under the input field

I am very new to this, please explain it in very simple language :)

P粉613735289
P粉613735289

reply all (1)
P粉924915787

diagnosis

If either validationifstatement is true, a string variable$emailErrwill be set.

Then the code exits theifblock again and the script continues on its merry way and attempts to send the email. There is no logic in the code to prevent this.

Finally,$emailErris never used because the code redirects the user to another HTML page that cannot involve that variable.

solution

If any validation fails, you need some additional logic to tell the code to skip the email sending part. A very simple way to do this cleverly is to use a "flag" variable.

For example:

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; }

PS BTW, your regex is too restrictive - seeWhat characters are allowed in email addresses?. Since you're already usingFILTER_VALIDATE_EMAIL, you don't need it under any circumstances.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!