将sql结果包含在php邮件中

时间:2012-01-25 10:25:46

标签: php sql email foreach

我想问一下我的脚本中发送邮件是否缺少,我的查询很好并显示结果,我想请求帮助,了解如何将这些查询结果包含在我要发送的邮件中出。如果你能给我一个建议从哪里开始,我真的很感激。感谢。

    <?php

    $cfg = array(

        'display' => 'gw@mail.com',
        'address' => 'support@mail.com',
        'subject' => 'TEST '
            . date("m/d/t h:i:s A", strtotime('TODAY')) . ' hits',

        'queries' => array(
            "SELECT inbox.src AS 'From Mobile Number', outbox.dst AS 'To Mobile Number', outbox.stamp AS 'Timestamp', inbox.data AS 'Message', outbox.status AS 'Status' FROM mega.outbox LEFT JOIN mega.inbox ON outbox.pid=inbox.pid WHERE outbox.sid=156 && inbox.sid=156 && stamp > NOW() - INTERVAL 3 HOUR",

        ),
    );

    include "/var/xrelay/mysql.php";
    $sql = new database();

    $msg = '';
    foreach( $cfg['queries'] as $i ) {
       $tmp = $sql->read($i, true);
       if (!empty($tmp)) {
          foreach($tmp[0] as $k => $v) $msg .= "$k: $v\r\n";
       }
    }

    foreach( preg_split( '/[\s,]+/', $cfg['address'], -1, PREG_SPLIT_NO_EMPTY) as $i ) {
       $sql->write("INSERT INTO mega.emails (sender,recipient,subject,message,stamp) VALUES "
       . "('{$cfg['display']}','$i','{$cfg['subject']}','".$sql->escape($msg)."', NOW());");
    }

    ?>

有没有办法更好地改善这个?如果数据库中有任何活动,这封邮件应该每隔3小时向收件人发送邮件。它将在3小时前检索数据并将结果作为消息。

  

include "/var/xrelay/mysql.php";
$sql = new database('sql://gateway:xrelay@00.000.000.000/test');
$cfg = array(

   'display' => 'gw@mail.com',
   'address' => 'g@mail.com',
   'subject' => 'MAIL HITS ' . date("m/d/y h:i:s A", strtotime("NOW")) . ' hits',

   );

//define('DEBUG',true);

$result = mysql_query("SELECT outbox.dst, DATE_FORMAT(outbox.stamp,'%b-%d-%Y %h:%i %p')AS 'Stamp', outbox.status FROM mega.outbox LEFT JOIN mega.inbox ON outbox.pid=inbox.pid WHERE outbox.sid=156 && inbox.sid=156 && outbox.stamp > NOW() - INTERVAL 3 HOUR");
$msg='|  Mobile#      |         Timestamp          |  Status  |\r\n';
while($row = mysql_fetch_assoc($result))
  {
   $msg .= "| ". $row['dst'] ." | ". $row['Stamp'] ." | ". $row['status'] ." |\r\n";
  }
$num_rows = mysql_num_rows($result);
echo $msg;
if($num_rows != 0){
foreach( preg_split( '/[\s,]+/', $cfg['address'], -1, PREG_SPLIT_NO_EMPTY) as $i ) {
   $sql->write("INSERT INTO mega.emails (sender,recipient,subject,message,stamp) VALUES " . "('{$cfg['display']}','$i','{$cfg['subject']}','". $msg ."', NOW());");
   }
}
?>

1 个答案:

答案 0 :(得分:0)

获得结果后,您应该创建一个字符串,将其附加到电子邮件正文中。您可以使用sprintf来实现此目的。

如果您正在寻找发送实际电子邮件的方法,请解释mail函数here