PHP send mail to multiple email addresses

后端 未结 14 1379
盖世英雄少女心
盖世英雄少女心 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:51

    Try this. It works for me.

    $to = $email1 .','. $email2 .','. $email3;
    
    0 讨论(0)
  • 2020-11-27 06:52

    Following code will do the task....

    <?php
    
    $contacts = array(
    "youremailaddress@yourdomain.com",
    "youremailaddress@yourdomain.com",
    //....as many email address as you need
    );
    
    foreach($contacts as $contact) {
    
    $to      =  $contact;
    $subject = 'the subject';
    $message = 'hello';
    mail($to, $subject, $message, $headers);
    
    }
    
    ?>
    
    0 讨论(0)
  • 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");
    
    0 讨论(0)
  • 2020-11-27 07:00

    In mail function you can as many reciepient as you want in $emailto paramater seperated by comma.

    0 讨论(0)
  • 2020-11-27 07:04

    I think the following code will works.

    $tos = array('address1@yourdomain.com', 'address2@yourdomain.com');
    foreach ($tos as $to){
        $ok = mail ($to, $subject, $body, $from);
    }
    if ($ok) {
        echo "Message Send";
    } else { 
        echo "Error";
    }
    
    0 讨论(0)
  • 2020-11-27 07:06
        $recipients = "test1@test.com,test2@test.com,test3@test.com,test1@test.com";
        $email_array = explode(",",$recipients);
        foreach($email_array as $email)
        {
            echo $to      =  $email;
            $subject = 'the subject';
            $message = 'hello';
           $headers = 'From: webmaster@example.com' . "\r\n" .
           'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
            mail($to, $subject, $message, $headers);
    
        }
    
    0 讨论(0)
提交回复
热议问题