从我的数据库中选择并发送邮件

时间:2015-11-12 12:44:00

标签: php mysql email oop

我正在尝试从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表中选择列字段并将电子邮件发送给与该列关联的收件人的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<?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();
?>
相关问题