为什么我的脚本只向MYSQL数据库中的一个收件人发送邮件

时间:2012-08-29 14:27:47

标签: php mysql forms attachment

所以我的代码在没有'文件附件'的情况下完美运行,并将以html格式输入的消息发送给MYSQL数据库中的所有收件人但是当我在附件代码中插入时,邮件会被发送但只发送给一个收件人。请帮帮我:))

<html>
<head>
<title>test</title>
</head>
<body>

<?

mysql_connect("localhost", "root", "mypass") or die("Could not connect: " . mysql_error());

mysql_select_db("mydb");

$sql = mysql_query("SELECT * FROM mytable");

while($row = mysql_fetch_array($sql)){

$email = $row["email"];
$strSubject = $_POST["txtSubject"];
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
}
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $strHeader .= "--".$strSid."\n";
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $strHeader .= "Content-Transfer-Encoding: base64\n";
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $strHeader .= $strContent."\n\n";
}


$flgSend = @mail($email,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
    echo "Task Complete. Your emails are being forwareded to your clients";
}
else
{
    echo "Error!!! Task incomplete. Please contact website maintainer.";
}
?>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

您的电子邮件发送代码不在循环中

将其移到下方:

while($row = mysql_fetch_array($sql)){
}

答案 1 :(得分:1)

您没有在while循环中发送电子邮件。

$strHeader .= $strMessage."\n\n";
} //This is the end of your while loop
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{

所以你的代码应该是这样的:

<html>
<head>
<title>test</title>
</head>
<body>

<?

mysql_connect("localhost", "root", "mypass") or die("Could not connect: " . mysql_error());

mysql_select_db("mydb");

$sql = mysql_query("SELECT * FROM mytable");

while($row = mysql_fetch_array($sql)){

$email = $row["email"];
$strSubject = $_POST["txtSubject"];
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
//********************* This is the old end of while loop}
//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $strFilesName = $_FILES["fileAttach"]["name"];
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
    $strHeader .= "--".$strSid."\n";
    $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
    $strHeader .= "Content-Transfer-Encoding: base64\n";
    $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
    $strHeader .= $strContent."\n\n";
}


$flgSend = @mail($email,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
    echo "Task Complete. Your emails are being forwareded to your clients";
}
else
{
    echo "Error!!! Task incomplete. Please contact website maintainer.";
}

} // New end of while loop
?>