-1

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

<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>
4

2 回答 2

2

您的电子邮件发送代码在循环之外

把它移到下面:

while($row = mysql_fetch_array($sql)){
}
于 2012-08-29T14:30:07.033 回答
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
?>

于 2012-08-29T14:31:11.067 回答