PHP send mail to multiple email addresses

后端 未结 14 1490
盖世英雄少女心
盖世英雄少女心 2020-11-27 06:49

What code I should do change in this PHP script to send one email to more than 20 email addresses?



        
14条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 06:58

    The best way could be to save all the emails in a database.

    You can try this code, assuming you have your email in a database

    /*Your connection to your database comes here*/
    $query="select email from yourtable";
    $result =mysql_query($query);
    

    /the above code depends on where you saved your email addresses, so make sure you replace it with your parameters/

    Then you can make a comma separated string from the result,

    while($row=$result->fetch_array()){
            if($rows=='')    //this prevents from inserting comma on before the first element
            $rows.=$row['email'];
            else
            $rows.=','.$row['email'];
        }
    

    Now you can use

    $to = explode(',',$rows); // to change to array
    
    $string =implode(',',$cc); //to get back the string separated by comma
    

    With above code you can send the email like this

    mail($string, "Test", "Hi, Happy X-Mas and New Year");
    

提交回复
热议问题