以下是我的HTML Select字段,它从我的数据库中获取所有电子邮件。现在我正在尝试使用php发送单个或多个电子邮件。但它不发送任何电子邮件。你能告诉我我的代码有什么问题吗?
Html代码:
<tr>
<td valign="top">To</td>
<td>
<select multiple="multiple" size="7" name="to[]">
<?php
$getemail = mysql_query("SELECT email FROM clients");
while($res = mysql_fetch_array($getemail)){
$email = inputvalid($res['email']);
echo "<option value='$email'>$email</option>";
}
?>s
</select>
</td>
</tr>
Php代码:
foreach($to as $total){
$total;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$mail = mail($total, $subject, $msg, $headers);
}
更新 - 完整代码:
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" />
<table width="400" border="0" cellspacing="10" cellpadding="0" style="float:left;
position:relative;">
<tr>
<td>Subject</td>
<td><input type="text" name="subject" value="<?php if(isset($_POST['subject'])) echo
$_POST['subject']; ?>" class="tr"/></td>
</tr>
<tr>
<td valign="top">Message</td>
<td><textarea name="msg" class="textarea_email"><?php if(isset($_POST['msg'])) echo
$_POST['msg']; ?></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send Message" class=" submit" name="Submit"/></td>
</tr>
</table>
<table style="float:left; position:relative; border:0px #000 solid;" width="400" border="0"
cellspacing="10" cellpadding="0">
<tr>
<td valign="top">To</td>
<td>
<select multiple="multiple" size="7" name="to[]">
<?php
$getemail = mysql_query("SELECT email FROM clients") or die(mysql_error());;
while($res = mysql_fetch_array($getemail)){
$email = inputvalid($res['email']);
echo "<option value='$email'>$email</option>";
}
?>s
</select>
</td>
</tr>
</table>
</form>
Php代码:
if(isset($_POST['Submit']) && $_POST['Submit'] == "Send Message")
{
$subject = inputvalid($_POST['subject']);
$msg = inputvalid($_POST['msg']);
$to = $_POST['to'];
if(isset($subject) && isset($msg) && isset($to)){
if(empty($subject) && empty($msg) && empty($to))
$err[] = "All filed require";
}
else{
if(empty($to))
$err[] = "Please select email address";
if(empty($subject))
$err[] = "Subject require";
if(empty($msg))
$err[] = "Message require";
}
if(!empty($err))
{
echo "<div class='error'>";
foreach($err as $er)
{
echo "<font color=red>$er.</font>
<br/>";
}
echo "</div>";
echo "<br/>";
}
else{
foreach($to as $total){
echo $total;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$mail = mail($total, $subject, $msg,
$headers);
}
if($mail)
echo "<font color=green>Successfully sent your message. We will be get in
touch with you. Thank You.</font/><br/><br/>";
header("Refresh:10; url=email.php");
}
}
答案 0 :(得分:3)
您可以为一个邮件功能发送多封电子邮件。
仅以逗号分隔的方式传递电子邮件地址。
喜欢“test @ gmail.com,test1 @ gmail.com”
答案 1 :(得分:1)
首先确保您的查询是正确的。
更改以下行:
$getemail = mysql_query("SELECT email FROM clients");
到
$getemail = mysql_query("SELECT email FROM clients") or die(mysql_error());
更改以下行:
$total;
到
echo $total . '<BR />';
保存并刷新页面 你的输出是什么?
你收到错误消息吗?
你看到电子邮件吗?
编辑:
更改以下行
$mail = mail($total, $subject, $msg, $headers);
到
if (!mail($total, $subject, $msg, $headers);) {
echo 'This is not working';
}
你的结果是什么?
答案 2 :(得分:1)
检查你的apache-configs!我在本地机器上遇到了同样的问题。一旦我将其上传到完全配置的Web服务器上,代码就开始工作了!也许这也解决了你的问题
答案 3 :(得分:0)
有些时候mail()函数可能无效,所以你应该使用PHPMailer,有关使用它的更多细节,你可以通过一个很好的文档: