使用mail()从MySQL Query发送多封电子邮件

时间:2014-10-01 12:07:31

标签: php mysql email

我得到了这段代码并且它已经老了,我知道我需要将它转换为PDO(仍然在学习),但我现在需要让它正常工作。

我正在尝试根据查询发送多封电子邮件。所以在这个特定的查询中,我得到两个结果。所以我需要向该组发送两封不同的电子邮件。如果查询有4个结果,那么我需要发送4.现在它只发送第一个结果。如何让它循环并发送多封电子邮件。

以下是代码:

<?
// connection string ......

$production = "SELECT value1 FROM parameters WHERE name = 'IS_PRODUCTION' ";

$query = "SELECT accounts.username, email_activate.email, accounts.user_id, accounts.alt_id FROM accounts INNER JOIN email_activate ON accounts.user_id = email_activate.user_id WHERE dt_welcome = '0000-00-00 00:00:00' AND dt_start <= DATE_SUB(NOW(), INTERVAL 3 DAY)";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {


$message =  "Hello ".$row['username'].", <br/><br/>";
$message .=  "You recently subscribed to <bblah blah blah</b> ";
$message .=  '(<a href="http://">http://</a>), ' ;
$message .=  "the official ";
$message .=  "source of <br /><br /> ";

if ($production == 'Y' ) {


$subject = "How can we Assist!";
$to = "".$row['email']."";
$result2 = mysql_query("UPDATE email_activate SET dt_welcome = NOW() WHERE user_id = '".$row['user_id']."' ");



} else {

$subject = "[TEST] Offer You Assistance";
$to      = 'email1@email.com' . ', ';
$to      .= 'email2@email.com ' . ', ';
$to      .= 'email3@email.com';


$result2 = mysql_query("UPDATE email_activate SET dt_welcome = NOW() WHERE user_id = '".$row['user_id']."' ");

}



$headers = "Content-Type: text/html; charset=utf-8\r\n";


$queryEmail = "SELECT alt_id FROM lookup_roles WHERE spec_tag = 'public_email'";
$result3 = mysql_query($queryEmail) or die(mysql_error());
while ($row12 = mysql_fetch_array($resul3t)) {


mail($to, $subject, $message, $headers, "-f ".$row12[alt_id]."");

}

}

?>

所以我修复了它,上面的代码工作得很好。我忘了使用不同的$ result变量---&gt;将它从$ result更改为$ result3,这样它就不会删除之前的结果,这就是为什么我只收到1封电子邮件。我知道这很简单,只是无法看到它。

0 个答案:

没有答案