Search code examples
mysqlwhile-loopmime

MIME E-mail - While loop - Database - Error


Resolved: After installing PHPMailer it now works.


I am trying to use MIME E-mail to send out mail to a large number of members. Seeing that my web-host has limitations on how long a function can run on their server I had to adjust the old functionality. This is how I want it to work:

  1. Get every member email-address from database table and create a new table with all of these email-addresses.
  2. In the new table I also create two attributes called "Sent" and "Error".
  3. I am planning to use Cron on this functionality. So that every row of email-addresses eventually will be set with Sent = True(1) or Error = True (1).

There is only one problem. I tried testing this on 4 email-addresses. When one of them returns error all the following email-addresses get the same error.

Example of error messages:

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

etc..

Output example:

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* 

I can't understand why this happens.

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

Solution

  • You are not clearing the $email_message object after each send, you are using the same $email_message object, just appending new addresses and sending, if one of those addresses are incorrect you carry on the error to the following recipients.

    You could try replacing your following code:

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

    for this one:

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