【问题标题】:Select and send mail from my database从我的数据库中选择并发送邮件
【发布时间】:2016-02-13 19:00:47
【问题描述】:

我正在尝试从 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) 你的 if/else 是不是错了? 2)您应该循环使用 fetch,而不是使用 fetch 然后循环返回的内容。目前你只是得到一行,然后围绕列循环。
  • 另外,使用query() 而不是prepare() 你没有传递任何参数。 execute() 只是多余的......只是重新学习一遍
  • $$rows 是故意的吗?
  • 它的印刷错误。

标签: php mysql email oop


【解决方案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();
?>

【讨论】:

  • @Oladeji 如果它解决了您的问题,请考虑接受答案。以下是meta.stackexchange.com/questions/5234/… 然后返回此处并对勾号/复选标记执行相同操作直到它变为绿色的方法。这会通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,可能想要发布(更多)答案。
猜你喜欢
  • 2017-02-18
  • 2020-09-15
  • 2012-12-07
  • 2012-08-30
  • 2021-10-17
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多