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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题