How to send e-mail to multiple recipients from database query (PHP)

前端 未结 7 2410
南方客
南方客 2020-12-29 16:41

I\'m trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need

7条回答
  •  有刺的猬
    2020-12-29 17:05

    Try something like this but one point to note is that you should send emails individually ratehr than group all your email addresses in one "to" field. Other users might not like others seeing that. Maybe your smtp function breaks down the array, not sure :-|

    function sendmail($cat, $user)
    { 
        require_once "Mail.php"; 
        $elist = mysql_query("SELECT cEmail FROM tblUsers WHERE cAlerts = 'All' AND cEAlerts = 'Yes' AND cPreferences LIKE '%$cat%';"); 
    
        $from = "EMAIL ADDRESS"; 
        $subject = "SUBJECT"; 
        $body = "BODY"; 
    
        $host = "smtp.domain.com"; 
        $username = "USERNAME"; 
        $password = "PASSWORD"; 
    
            if(mysql_num_rows($elist) > 0)
            {
                while($elist_result = mysql_fetch_array($elist))
                {
                $headers = array ('From' => $from, 
                'To' => $elist_result['cEmail'], 
                'Subject' => $subject); 
                $smtp = Mail::factory('smtp', 
                array ('host' => $host, 
                'auth' => true, 
                'username' => $username, 
                'password' => $password)); 
    
                $mail = $smtp->send($to, $headers, $body); 
                }
            }
     } 
    

提交回复
热议问题