3

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


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

  1. 从数据库表中获取每个成员的电子邮件地址,并使用所有这些电子邮件地址创建一个新表。
  2. 在新表中,我还创建了两个名为“Sent”和“Error”的属性。
  3. 我打算在这个功能上使用Cron 。这样每一行电子邮件地址最终都将设置为 Sent = True(1) 或 Error = True (1)。

只有一个问题。我尝试在 4 个电子邮件地址上对此进行测试。当其中一个返回错误时,以下所有电子邮件地址都会收到相同的错误。

错误消息示例:

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

ETC..

输出示例:

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

2 回答 2

2

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

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

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

对于这个:

$email_message->ClearAddresses();
$email_message->AddAddress($to_address, $to_address);
于 2012-10-10T18:39:21.380 回答
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 类中。

于 2012-10-10T18:22:25.613 回答