MIME电子邮件 - while循环 - 数据库 - 错误

时间:2012-10-10 18:03:11

标签: mysql while-loop mime

已解决:安装PHPMailer后,它现在可以正常工作。


我正在尝试使用MIME电子邮件向大量成员发送邮件。 看到我的网络主机对功能在其服务器上运行的时间有限制,我不得不调整旧功能。 这就是我希望它工作的方式:

  1. 从数据库表中获取每个成员的电子邮件地址,并创建一个包含所有这些电子邮件地址的新表。
  2. 在新表中,我还创建了两个名为“已发送”和“错误”的属性。
  3. 我打算在此功能上使用Cron。这样每一行电子邮件地址最终都会设置为Sent = True(1)或Error = True(1)。
  4. 只有一个问题。我尝试在4个电子邮件地址上测试它。当其中一个返回错误时,以下所有电子邮件地址都会出现同样的错误。

    错误消息示例:

    Error: 501 : malformed address: �> may not follow **Probably caused of ÆØÅ**
    Error: 501 : domain missing or malformed  **Because @service.com is missing**
    

    等。

    输出示例:

    1: Before function. Error value: 
    1: After function. Error value: 501 : domain missing or malformed
    2012-10-10 20:07:46 : 1: MemberID: 1, Mail:testme
    Error: 501 : domain missing or malformed 
    
    2: Before function. Error value: 
    2: After function. Error value: 501 : domain missing or malformed
    2012-10-10 20:07:46 : 2: MemberID: 2, Mail:testme@gmail.com
    Error: 501 : domain missing or malformed 
    
    3: Before function. Error value: 
    3: After function. Error value: 501 : domain missing or malformed
    2012-10-10 20:07:46 : 3: MemberID: 3, Mail:testme@hotmail.com
    Error: 501 : domain missing or malformed* 
    

    我无法理解为什么会这样。

    ######################################
    ### START NEW MAIL FUNCTION   ####
    ######################################
    ##############################################
    ### Deletes table if already exists   ####
    ##############################################
    
    
    $dropExistingTable = "DROP TABLE IF EXISTS $databaseTableNameContainingMembers";
    mysql_query($dropExistingTable) or die(mysql_error());
    
    ####################################
    ### Creating new table  ####
    ####################################
    $queryCreateTable = "CREATE TABLE $databaseTableNameContainingMembers(ID mediumint(6) NOT NULL AUTO_INCREMENT, Mail TEXT, MailSent BOOLEAN DEFAULT 0, Error BOOLEAN DEFAULT 0, PRIMARY KEY (ID))";
    mysql_query($queryCreateTable) or die(mysql_error());
    
    ####################################
    ### Filling new table   ####
    ####################################
    //Now we fill the new table with all our members (MemberID, Mail, Sendt = 0).
    
    while ($medlem = mysql_fetch_array($query_medlem)) {
        //insert all member mails to new table
        $memberMail = $medlem["Epost"];
        $queryInsertMemberToNewTable = "INSERT INTO $databaseTableNameContainingMembers (`Mail`) VALUES ('" . $memberMail . "')";
        mysql_query($queryInsertMemberToNewTable) or die(mysql_error());
    }
    
    ######################################
    ### Sending function      ####
    ######################################
    
    function sendMail($table, $email_message) {
    ##########################################
    ###     Selecting the new table       ####
    ##########################################
        $querySelectAllMembersThatHasNotAlreadyBeenSentTo = "SELECT * FROM $table WHERE `MailSent` = 0 AND `Error` = 0";
    
    //Selects all members that havent been sendt mail to yet.
    
        $allMembersThatHasNotAlreadyBeenSentTo = mysql_query($querySelectAllMembersThatHasNotAlreadyBeenSentTo) or die("Problem with selecting table. <br>" . mysql_error());
    
        $i = 1;
        while ($row = mysql_fetch_array($allMembersThatHasNotAlreadyBeenSentTo)) {
    
            $to_address = $row["Mail"];
            $memberId = $row["ID"];
    
           $email_message->SetEncodedEmailHeader("To", $to_address, ""); 
    
            $error = "";
            echo $i . ": Before function. Error value: " . $error . "</br></br>";
            $error = $email_message->Send();
            echo $i . ": After function. Error value: " . $error . "</br></br>";
            echo "</br>";
    
            if (strcmp($error, "") > 0) {
                ##########################################################
                ###      IF ERROR UPDATE MEMBER VARIABLE "Error"      ####
                ##########################################################
                updateErrorToMember($memberId, $table, 1);
                echo date("Y-m-d H:i:s") . " : " . $i . ": MemberID: " . $memberId . ", Mail:" . $to_address . "</br>Error: $error </br></br></br>";
            } else {
                ##################################################################
                ###    IF SENDING SUCCEEDS UPDATE MEMBER VARIABLE "Sent"      ####
                ##################################################################
                updateSentToMember($memberId, $table);
                echo date("Y-m-d H:i:s") . ": " . $i . " :  Message sent to $to_address<br></br>";
            }
            $i++;
        } //while
    }//function
    
    ######################################
    ###    End of sending function    ####
    ######################################
    ######################################
    ###     Run sending function      ####
    ######################################
    sendMail($databaseTableNameContainingMembers, $email_message);
    

2 个答案:

答案 0 :(得分:2)

您没有在每次发送后清除$email_message对象,您正在使用相同的$email_message对象,只是附加新地址并发送,如果其中一个地址不正确,您继续执行错误以下收件人。

您可以尝试替换以下代码:

$email_message->SetEncodedEmailHeader("To", $to_address, "");

这个:

$email_message->ClearAddresses();
$email_message->AddAddress($to_address, $to_address);

答案 1 :(得分:0)

$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html; charset=utf-8"."\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "From: ".$from."\r\n";
$message = '<html><body>';
$message .= stripslashes($msg);
$message .= '</body></html>';

if(mail($to_address, $subject, $message, $headers)){
    updateSentToMember($memberId, $table);
    echo date("Y-m-d H:i:s") . ": " . $i . " :  Message sent to $to_address<br></br>";
}else{
    updateErrorToMember($memberId, $table, 1);
    echo date("Y-m-d H:i:s") . " : " . $i . ": MemberID: " . $memberId . ", Mail:" . $to_address . "</br>Error: $error </br></br></br>";
}

试试这个PHP mail()函数如果它运行良好,那么我可以肯定问题是在phpMailer类中。