发送电子邮件到数据库中存储的电子邮件地址的数量


Sending emails to number of email addresses stored in database

我想发送电子邮件到数量的电子邮件地址存储在我的数据库使用一些查询,将电子邮件发送到只有那些域将在5天内过期。我认为我的查询没有运行,也没有访问存储在"$到"变量的数据库中的电子邮件。但是当我在"$to"中输入一个特定的电子邮件地址时,此代码将电子邮件发送到该特定地址。但问题是,我想发送电子邮件的电子邮件地址存储在数据库的域即将过期。这是我的模型数据库。php。如有任何帮助,不胜感激。

    database.php model:
    function sendmailto(){
    $query ="select email from domain_detail where expiry_date - curdate()=5"; 
    //$query ="select email from domain_detail where expiry_date < curdate()";

   if($query_result = $this->db->query($query)){
    foreach ($query_result->result() as $domain_list) {
        $to= $domain_list->email;
        //$to = "abcd18@gmail.com";
        $from= "ijkl23@hotmail.com";
        $subject='test cron';
        $message= '<h2> Your domain is going to be expired. Please renew it soon.';
        $headers= "From: $from'n";
        $headers .="MIME-Version: 1.0'n";
        $headers .="Content-type: text/html; charset=iso-8859-1'n";
        mail($to, $subject, $message, $headers);
    }
         if(mail($to, $subject, $message, $headers))
    {
        echo 'Message has been sent successfully.';
    }
    else
    {
        echo 'Message couldnot send. Please try again!';
    }
}
else{
    echo "No Email Address Found!!";
}

}

当你引用$domain_list->email;你确定$domain_list是一个对象而不是一个关联数组吗?你试过使用$domain_list['email']吗?

普通的PHP mail()函数有时会被接收方标记为垃圾邮件。我建议尝试phpmailer库:https://github.com/PHPMailer/PHPMailer/tree/master

如果你使用的是CI,可以试试这个:

$query = $this->db->query('select email from domain_detail where expiry_date - curdate()=5');
if($query->num_rows() > 0) 
{
   foreach($query->result() as $domain_list) 
   {
      // rest of code here...
   }
}

你不能在没有函数的情况下减去两个date或datetime表达式并得到interval。

基本上需要DATEDIFF()

DATEDIFF()返回expr1 - expr2,表示为从一个日期到另一个日期的天数。>expr1和expr2是日期或日期-时间表达式。在计算中只使用值的日期部分。

,那么你的查询将是

$query ="select email from domain_detail where DATEDIFF(expiry_date, curdate() = 5)";
相关文章: