如何加速Mail()PHP代码

时间:2018-04-20 15:15:43

标签: php performance email

我的代码有效,但是当客户在网络表单中添加两张或更多图片并按下发送时,我的网站加载速度非常慢。加载大约需要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.0\r\n";
            $headers .= "From:" . $from_email . "\r\n";
            $headers .= "Reply-To: " . $sender_car . "" . "\r\n";
            $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

            //message text
            $body = "--$boundary\r\n";
            $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
            $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
            $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 .= "--$boundary\r\n";

                    $body .= "Content-Type: $file_type; name=\"$file_tmp_name\"\r\n";
                    $body .= "Content-Disposition: attachment; filename=\"$file_tmp_name\"\r\n";
                    $body .= "Content-Transfer-Encoding: base64\r\n";
                    $body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
                    $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);
}

1 个答案:

答案 0 :(得分:0)

问题可能是您的图片太大而您的上传速率很小或您的系统/邮件服务器速度很慢。但你可以试试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 {...}