dsjpqpdm620596 2019-03-26 09:27
浏览 18
已采纳

用PHP发送多封邮件

I have an issue with my code, I'am trying to send multiple mails at once to different users but it only sends the mail to the first mail in my mails list.

Can anyone help me find where the problem is located?

    function send_mails($filename, $from){
        $infos = combine_mails_to_passwords($filename);
        $headers = "MIME-Version: 1.0" . "
";
        $headers .= "Content-Type: text/html; charset=UTF-8
";
        $headers .= "X-Mailer: PHP". phpversion() ."
" ;
        $headers .= "From: '$from'" . "
";
        foreach($infos as $key => $info){
            $to = $info;
            $subject = "TEST Mail";
            $message = "<h2 style='font-size:18px;'>
            Voici vos identifiants pour passer l'évaluation</h2> 
            <div style='text-align:center;'>
            <table><tr><td><u>Login: </u></td> <td><b> ".$_SESSION['login']."</b><td></tr><br/> 
            <tr><td><u>Votre Mot de Passe: </u></td> <td><b> ".$key."</b></td></tr></table></div>";
            $from = "From: Company Name <TEST>";

            $ok = mail($to,$subject,$message,$headers);
            return $ok;
    }
}

if(isset($_POST['send-submit'])){

        if (!empty($_FILES['fileToUpload']) && $_FILES['fileToUpload']['name']!=''){
            $file_path = $_FILES['fileToUpload']['tmp_name'];
            $from = $_POST['from'];
            if($from!=""){
            if($_FILES['fileToUpload']['type'] != 'text/plain'){
                $error = "The file is not a text file!";
            }
            else if(count_mails($file_path)==0){
                $error = "The file is empty!";
            }
            else{

                if(send_mails($file_path, checkInput($from))){
                    $good = "Mails sent!";
                }
                else{
                    $error = "Connexion Problem!";
                }

            }
        }
            else{
                $error = "Please enter the email of the sender!";
            }
    }
    else{
        $error = "You did not import the emails file!";
    }
}

Note: The mails are uploaded from a text file. Thank you!

  • 写回答

3条回答 默认 最新

  • doumu1951 2019-03-26 09:35
    关注

    return $ok; is the culprit...this means your function returns after the first time it loops, so the rest of it is never executed.

    Instead you probably want to build up a list of the results of all the email attempts, so you can see which ones succeeded, and which failed. Then you can return that list at the very end of your function, after the loop ends.

    Something like this (untested):

    function send_mails($filename, $from){
    
        $infos = combine_mails_to_passwords($filename);
        $headers = "MIME-Version: 1.0" . "
    ";
        $headers .= "Content-Type: text/html; charset=UTF-8
    ";
        $headers .= "X-Mailer: PHP". phpversion() ."
    " ;
        $headers .= "From: '$from'" . "
    ";
    
        $results = array(); //hold a list of results
    
        foreach($infos as $key => $info){
            $to = $info;
            $subject = "TEST Mail";
            $message = "<h2 style='font-size:18px;'>
            Voici vos identifiants pour passer l'évaluation</h2> 
            <div style='text-align:center;'>
            <table><tr><td><u>Login: </u></td> <td><b> ".$_SESSION['login']."</b><td></tr><br/> 
            <tr><td><u>Votre Mot de Passe: </u></td> <td><b> ".$key."</b></td></tr></table></div>";
            $from = "From: Company Name <TEST>";
    
            $results[$to] = mail($to,$subject,$message,$headers);
        }
        return $results;
    }
    

    and something like this to process the result and warn about failures:

    $results = send_mails($file_path, checkInput($from));
    foreach($results as $email => $result)
    {
      if ($result == false) echo "Alert: Email to $email failed<br/>";
    }
    echo "All mail sent successfully apart from any which are alerted above";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?