我正在尝试从MySQL数据库表中获取“电子邮件”列,然后向其发送电子邮件。
下面是我的PHP代码:

private function sendEmailTech() {
$select = $this->pdo->prepare("SELECT email FROM tbl_tech_email");
try {
    $select->execute();
    $data = $select->fetch();
    foreach($data as $datum=>$email){
        if ($email == '') {
            $rows.=$email.',';
        } else {
            return false;
        }
    $rows = str_replace(',--','',$rows);
    $to = explode(',', $rows); // to change to array
    mail($$rows, "My Info", "Hello, I just sent a mail to You");
    }
}
catch (PDOException $e) {
    die($e->getMessage());
}


从MySQL表中选择列字段并将电子邮件发送到与该列关联的收件人的正确方法是什么?

最佳答案

尝试这样的事情:

<?php
    function sendEmailTech() {
        $select = $this->pdo->query("SELECT email FROM tbl_tech_email");
        $select = $select->fetchAll();
        if(count($select) > 0) {
            foreach($select AS $recipient) {
                mail($recipient["email"], "My Info", "Hello, I just sent a mail to You");
            }
        } else {
            echo "No user to send email";
            die();
        }
    }
    // sendEmailTech();
?>

关于php - 从我的数据库中选择并发送邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33671694/

10-09 20:02
查看更多