以下是我的HTML Select字段,它从我的数据库中获取所有电子邮件。现在我试图用php发送单个或多个电子邮件。但它不发送任何电子邮件。你能告诉我我的代码有什么问题吗?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 code:
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");
}
}
你的错误是什么? '$ mail'的价值是什么?你有没有检查你的邮件日志?一旦它接收到来自PHP'mail()'命令的输入,取决于你的邮件服务器/ MTA。 PHP简单地将它交给MTA,并且从那里出现的任何错误都会出现在你的邮件日志中(也就是说,直到它被发送;当它被发送时,取决于接收MTA)。同时检查你的防火墙设置。 – BLaZuRE
@BLaZuRE我检查了我的电子邮件。我没有收到任何电子邮件。没有错误,但它不起作用。如果你想,那么我可以显示我的完整代码。 –
它不那么简单。 PHP不发送邮件。它告诉另一个程序发送邮件。我们不知道你是否仅仅通过你的代码就有这样的程序。在mail()函数中阅读[documentation](http://php.net/manual/en/function.mail.php)。如果您的电子邮件不被这样的程序接收,它将返回false。 ** PHP不发送邮件。它使用另一个程序发送邮件**您需要检查此类程序的邮件日志(Postfix,Sendmail等)。请输出'$ mail'的值(使用'var_dump($ mail);'有帮助)。 – BLaZuRE