0

I have a very interesting problem with my script. I need to send a brief to my email, and I use PHP Mailer for that.

<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Host = 'mail.webtests.xyz';
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->Username = 'yapik@webtests.xyz';
    $mail->Password = '1104Romik!';
    $mail->setFrom('yapik@webtests.xyz', 'Yaroslav Kretov');
    $mail->addAddress($_POST['email'], $_POST['name']);
    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'PHPMailer contact form';
        $mail->isHTML(false);
        $mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
        if (!$mail->send()) {
            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Thanks for contacting us.';
        }
    } else {
        $msg = 'Share it with us!';
    }
}
else{
    echo 'error';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact form</title>
</head>
<body>
<h1>Do You Have Anything in Mind?</h1>
<?php if (!empty($msg)) {
    echo "<h2>$msg</h2>";
} ?>
<form method="POST">
    <label for="name">Name: <input type="text" name="name" id="name"></label><br><br>
    <label for="email">Email: <input type="email" name="email" id="email"></label><br><br>
    <label for="message">Message: <textarea name="message" id="message" rows="8" cols="20"></textarea></label><br><br>
    <input type="submit" value="Send">
</form>
</body>
</html>

It sends a brief but before I confirm this form, or don't send it at all and give me timeout. I don't know what I should do.

Yap1k
  • 1
  • 2
  • 1
    You should check method of request before send mail like that $_SERVER['REQUEST_METHOD']. If GET -> dont run php, else run php – Mai Truong Aug 19 '22 at 08:33
  • It doesn't help( – Yap1k Aug 19 '22 at 08:41
  • 1
    where are you added check request method? – Mai Truong Aug 19 '22 at 08:42
  • 1
    Does this answer your question? [Checking if form has been submitted - PHP](https://stackoverflow.com/questions/7711466/checking-if-form-has-been-submitted-php) – ADyson Aug 19 '22 at 08:44
  • ah @MaiTroung, yes sorry it uses method _GET_ – Yap1k Aug 19 '22 at 08:51
  • My website gives me the result _timeout_ – Yap1k Aug 19 '22 at 08:58
  • In what situation? Did you apply the fix from my link first? – ADyson Aug 19 '22 at 08:59
  • Yes, I used it and I website print me an error because it used the get method. – Yap1k Aug 19 '22 at 09:07
  • 1
    The site will use GET when it loads the form, before you submit it. That's normal in a browser when you load a page. Using GET does not, by itself, cause timeouts. When you submit the form it uses POST (as per your form tag's "method" attribute) so you can check if its POST in order to decide whether to run the code which sends the email. Maybe you wrote something wrong when you tried to apply the fix to your code - please [edit] the question to show your updated code. Or maybe it's some unrelated performance issue in your server, it's very difficult for us to tell from here. – ADyson Aug 19 '22 at 09:09
  • Thanks. So it times out when you load the page, or only when you submit the form and send the email? My thoughts are either your webserver is unstable, or your Internet connection is unreliable, or your smtp mail server is not good, or some combination of those factors. – ADyson Aug 19 '22 at 09:30
  • Only when I submit the form. If it helps I can get the link to my website (https://webtests.xyz/smtp_email_composer/stackoverflow.php) – Yap1k Aug 19 '22 at 09:39
  • It's impossible to know anything just from looking at the front end. It's a server issue you'll need to debug. If it only happens when submitting then I'd guess some issue with the mailserver. Comment out the send() command and see if you still have the issue, that would help to confirm – ADyson Aug 19 '22 at 09:53
  • Yes, I think it is some problems with the server. If I found the solution I posted it here. – Yap1k Aug 19 '22 at 11:14

0 Answers0