为什么脚本加载这么慢[PHP]

时间:2017-09-02 14:43:53

标签: php email

我用PHP写了发送电子邮件脚本。当我按下发送按钮时,脚本加载的时间很长。 Finnaly它没有加载。当我删除重新检查,页面加载速度如此之快,并给我一个错误500。 代码:

<?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");
        }
    }
}

&GT;

1 个答案:

答案 0 :(得分:0)

您可能希望将邮件作为background task through a queue发送。

  

在某些情况下,您将从等待直接邮件2.60秒传递到queue-exec-background 0.024秒,这样可以更快地提高x11。