0

以下是我的 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>&nbsp;</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");
    }

}
4

4 回答 4

3

您可以为一项邮件功能发送多封电子邮件。

仅传递以逗号分隔的电子邮件地址。

像“test@gmail.com,test1@gmail.com”

于 2013-07-16T09:44:14.857 回答
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';
}

你的结果是什么?

于 2013-07-16T09:37:44.140 回答
1

检查您的 apache 配置!我在本地机器上遇到了同样的问题。一旦我将它上传到完全配置的网络服务器上,代码就开始工作了!也许这也解决了你的问题

于 2013-07-16T09:56:03.827 回答
0

有时 mail() 函数可能不起作用,所以你应该使用 PHPMailer,有关使用它的更多详细信息,你可以阅读一个好的文档:

使用 PHPMailer 发送邮件

于 2013-07-16T11:08:11.553 回答