0

I wrote sending email script in PHP. When I press send button the script is loading so long. Finnaly it doesn't load. And when I delete the recaptcha check, page load so fast and gives me an error 500. Code:

<?php
session_start();
$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
    CURLOPT_PORT => 1,
    CURLOPT_POSTFIELDS => [
        'secret' => '_____SECRET!____',
        'response' => $_POST['g-recaptcha-response'],
    ],
]);
$response = curl_exec($curl);
if(!$response->success){
    $_SESSION['success'] = false;
    $_SESSION['message'] = 'Nie zaznaczono pola: "Nie jestem robotem".';
    $_SESSION['redirect'] = $_SERVER['HTTP_REFERER'];
    header("result.php");
}else{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $content = $_POST['content'];
    if(isset($name) && isset($email) && isset($content)){
        $content = n12br($content);
        $to = "terapiaautyzmu@gmail.com";
        $from = $email;
        $subject = 'Terapiaautyzmu.pl - Email';
        $message = '<b>Imie i nazwisko:</b> '.$name.' <br><b>Email:</b> '.$from.' <p>'.$content.'</p>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=utf-8\n";
        $result = mail($to, $subject, $message, $headers);
        if($result){
            $_SESSION['success'] = true;
            $_SESSION['message'] = 'Wysłano emaila.';
            $_SESSION['redirect'] = $_SERVER['HTTP_REFERER'];
            header("result.php");
        }else {
            $_SESSION['success'] = false;
            $_SESSION['message'] = 'Serwer odrzucił wysłanie emaila". Sprawdź czy wszystkie pola zostały wypełnione poprawnie.';
            $_SESSION['redirect'] = $_SERVER['HTTP_REFERER'];
            header("result.php");
        }
    }
}

?>

Karol
  • 5
  • 1
  • 6
  • Try to find out what part of the code is actually slow. You can do that by commenting out the mailing part or the curl part and see what part is actually taking up so much time. – Ivar Sep 02 '17 at 14:52

1 Answers1

0

You might want to send the mail as a background task through a queue.

In some cases you will pass from waiting from a direct email 2.60 secs to queue-exec-background 0.024 secs, that is an improve of x11 faster.

SagunKho
  • 985
  • 10
  • 26