【发布时间】:2012-12-07 19:05:14
【问题描述】:
嗨,我想发送多封已注册用户的电子邮件,并且信息已保存在数据库中,所以在这里我想使用 php 脚本发送电子邮件。
这是我的代码
<?php
//Connect to database
$sql = "SELECT email FROM TABLENAME";
$res = mysql_query($sql) or die(mysql_error());
while( $row = mysql_fetch_assoc($res) )
{
$area = $row['email']. ", ";
// read the list of emails from the file.
$email_list = $area;
// count how many emails there are.
$total_emails = count($email_list);
// go through the list and trim off the newline character.
for ($counter=0; $counter<$total_emails; $counter++) {
$email_list[$counter] = trim($email_list[$counter]);
}
$to = $email_list;
echo $to;
}
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "newsletter@mydomain.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action=''>
Subject: <input name='subject' type='text' class='xl' /><br />
Message: <textarea name='message' cols='20' rows='10'></textarea><br />
<input type='submit' class='btn btn-primary' value='Send' />
</form>";
}
?>
【问题讨论】:
-
在每个循环中,
$email_list = $area;都会被覆盖。count用于数组而不是字符串 -
您将
$email_list视为一个数组,但它只是$area字符串的副本。你的意思是$email_list = explode(', ', $area);?