如何从数据库查询(PHP)向多个收件人发送电子邮件 [英] How to send e-mail to multiple recipients from database query (PHP)

查看:117
本文介绍了如何从数据库查询(PHP)向多个收件人发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送一封电子邮件到我的数据库中的多个电子邮件地址。这是我现在的代码。只有当我指定一个电子邮件地址时才可以工作,但是我需要让他们查询我的数据库,并将电子邮件发送到每个电子邮件地址。我在哪里出错?

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 to have them query my database and send the e-mail to each e-mail address. Where am I going wrong here?

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%';");
    $elist = mysql_fetch_array($elist);

    $from = "EMAIL ADDRESS";
    $to = $elist;
    $subject = "SUBJECT";
    $body = "BODY";

    $host = "smtp.domain.com";
    $username = "USERNAME";
    $password = "PASSWORD";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($to, $headers, $body);
 }


推荐答案

要注意的是,您应该在一个到字段中单独发送电子邮件,将所有电子邮件地址分组。其他用户可能不喜欢别人看到。也许你的smtp功能破坏了数组,不确定: - |

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); 
            }
        }
 } 

这篇关于如何从数据库查询(PHP)向多个收件人发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆