如何加速 Mail() PHP 代码



我的代码有效,但是当客户在Web表单中添加两张或更多图片并按发送时,我的网站加载速度非常慢。加载大约需要 1 分钟,实际发送大约需要 1-3 分钟。

如何加快此过程?

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['namecontact']) && !empty($_POST['namecarinfo'])) {
    $contacts = array(
        //"autoperka.info@gmail.com",
        //"automobiliukas24@gmail.com",
        //  "ruslanneviadomskij@gmail.com",
        "gabriele.giniot@gmail.com"
    );
    foreach($contacts as $contact) {

        $recipient_email = $contact; //recepient
        $from_email = $_POST['namecontact']; //from email using site domain.
        $subject = $from_email . " SupirkimasPlius.lt"; //email subject line
        $sender_name = $_POST["namecontact"];
        $sender_car = $_POST["namecarinfo"];
        $sender_message = 'Automobilio pasiūlymas:
          Marke/modelis:' . $_POST["namecarinfo"] . 'Kontaktai/miestas:' . $_POST["namecontact"] . 'Komentaras:' . $_POST["namecoment"];
        $attachments = $_FILES['namephoto'];
        $file_count = count($attachments['name']); //count total files attached
        $boundary = md5(time());



        if($file_count > 0) { //if attachment exists
            //header
            $headers = "MIME-Version: 1.0rn";
            $headers .= "From:" . $from_email . "rn";
            $headers .= "Reply-To: " . $sender_car . "" . "rn";
            $headers .= "Content-Type: multipart/mixed; boundary = $boundaryrnrn";
            //message text
            $body = "--$boundaryrn";
            $body .= "Content-Type: text/plain; charset=ISO-8859-1rn";
            $body .= "Content-Transfer-Encoding: base64rnrn";
            $body .= chunk_split(base64_encode($sender_message));
            //attachments
            for($x = 0; $x < $file_count; $x++) {
                if(!empty($attachments['name'][$x])) {
                    /*if($attachments['error'][$x]>0) //exit script and output error if we encounter any
                    {
                    $mymsg = array(
                    1=>"Įkeltos nuotraukos/ų dydis per didelis",
                    2=>"Įkeltos nuotraukos/ų dydis per didelis",
                    3=>"Įkėlimo klaida, pabandykite dar kartą",
                    4=>"Nėra įkeltų nuotraukų",
                    6=>"Įkėlimo klaida" );
                    die($mymsg[$attachments['error'][$x]]);
                    }*/
                    //get file info
                    $file_name = $attachments['name'][$x];
                    $file_tmp_name = $attachments['tmp_name'][$x];
                    $file_size = $attachments['size'][$x];
                    $file_type = $attachments['type'][$x];

                    //read file
                    $handle = fopen($file_tmp_name, "r");
                    $content = fread($handle, $file_size);
                    fclose($handle);
                    $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
                    $body .= "--$boundaryrn";
                    $body .= "Content-Type: $file_type; name="$file_tmp_name"rn";
                    $body .= "Content-Disposition: attachment; filename="$file_tmp_name"rn";
                    $body .= "Content-Transfer-Encoding: base64rn";
                    $body .= "X-Attachment-Id: " . rand(1000, 99999) . "rnrn";
                    $body .= $encoded_content;
                }
            }
        } else { //send plain email otherwise
            $headers = "From:" . $from;
            $body = $sender_message;
        }


        $sentMail = mail($recipient_email, $subject, $body, $headers);
    } //foreach uzdaro

}

if($sentMail) //output success or failure messages
    {
    $myfile = fopen("success.php", "r") or die(fopen("index.php", "r"));
    echo fread($myfile, filesize("success.php"));
    fclose($myfile);
    //mail('gabriele.giniot@gmail.com',$subject,$message,$headers);
    //mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender

} else {
    $myfile = fopen("failed.php", "r") or die(fopen("index.php", "r"));
    echo fread($myfile, filesize("failed.php"));
    fclose($myfile);
}

问题可能是您的图像很大,而上传速度很小,或者您的系统/邮件服务器很慢。但是你可以试试phpmailer。这将降低代码的复杂性,并可能提高性能。另请查看 move_uploaded_file((。然后,您的代码将如下所示

//first upload your attachments to a temp-directory (tmp/)
for ($i = 0; $i < count ($_FILES['namephoto']['tmp_name']); $i++)
    move_uploaded_file ($_FILES['namephoto']['tmp_name'][$i], 'tmp/'.$_FILES['namephoto']['name'][$i]);
//initialize phpmailer
$mail = new PHPMailer ();
//set smtp credentials
$mail->Host       = 'smtp1.example.com';     // Specify main and backup SMTP servers
$mail->SMTPAuth   = true;                    // Enable SMTP authentication
$mail->Username   = 'user@example.com';      // SMTP username
$mail->Password   = 'secret';                // SMTP password
$mail->SMTPSecure = 'tls';                   // Enable TLS encryption, `ssl` also accepted
$mail->Port       = 587;
//set from-address
$mail->setFrom ($_POST['namecontact']);
//add recipients
foreach ($contacts as $contact)
    $mail->addAddress ($contact);
//add attachments
for ($i = 0; $i < count ($_FILES['namephoto']['name']); $i++)
    $mail->addAttachment ('tmp/'.$_FILES['namephoto']['name'][$i]);
//set subject and body
$mail->Subject = $from_email.' SupirkimasPlius.lt';
$mail->Body    = 'Automobilio pasiūlymas: Marke/modelis: '.$_POST["namecarinfo"].' Kontaktai/miestas: '.$_POST["namecontact"].'Komentaras:'.$_POST["namecoment"];
//send your mail
$sentMail = $mail->send ();
//check if successfully sent
if ($sentMail) {...} else {...}

最新更新