0
您好我想發送多個電子郵件是註冊用戶和信息已經保存在數據庫中,所以我在這裏我想用PHP腳本發送電子郵件。如何發送多個電子郵件選擇從數據庫在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("[email protected]", "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'用於數組而不是字符串 – asprin
您將'$ email_list'當作數組來處理,但它只是'$ area'字符串的副本。你的意思是'$ email_list = explode(',',$ area);'? – Barmar