2012-05-16 2 views
0

Следующая программа предназначена для сопоставления входящих псевдонимов электронной почты с данными в базе данных и переадресации электронной почты на правильный адрес, например, Craigslist.Создание анонимной программы переадресации переадресации Craigslist

Сейчас я получаю эту ошибку:

Error: [1] You must provide at least one recipient email address. 
in anon-email.php at line number: sending the email 

Вот код:

$mailboxinfo = imap_mailboxmsginfo($connection); 
$messageCount = $mailboxinfo->Nmsgs; //Number of emails in the inbox 
for ($MID = 1; $MID <= $messageCount; $MID++) 
    { 
    $EmailHeaders = imap_headerinfo($connection, $MID); //Save all of the header information 
    $Body = imap_qprint(imap_fetchbody($connection, $MID, 1)); //The body of the email to be forwarded 

    $MessageSentToAllArray = $EmailHeaders->to; //Grab the “TO” header 
    $MessageSentToAllObject = $MessageSentToAllArray[0]; 
    $MessageSentToMailbox = $MessageSentToAllObject->mailbox ."@". $MessageSentToAllObject->host; //Everything before and after the “@” of the recipient 

    $MessageSentFromAllArray = $EmailHeaders->from; //Grab the “FROM” header 
    $MessageSentFromAllObject = $MessageSentFromAllArray[0]; 
    $MessageSentFromMailbox = $MessageSentFromAllObject->mailbox ."@". $MessageSentFromAllObject->host; //Everything before and after the “@” of the sender 
    $MessageSentFromName = $MessageSentFromAllObject->personal; //The name of the person who sent the email 

    $toArray = searchRecipient($MessageSentToMailbox); //Find the correct person to send the email to 
    if($toArray == FALSE) //If the alias they entered doesn’t exist… 
    { 
    $bounceback = 'Sorry the email in your message does not appear to be correct'; 
    /* Send a bounceback email */ 
    $mail = new PHPMailer(); // defaults to using php “mail()” 
    $mail -> ContentType = 'text/plain'; //Plain email 
    $mail -> IsHTML(false); //No HTML 
    $the_body = wordWrap($bounceback, 70); //Word wrap to 70 characters for formatting 
    $from_email_address = '[email protected]'; 
    $mail->AddReplyTo($from_email_address, "domain.Com"); 
    $mail->SetFrom($from_email_address, "domain.Com"); 
    $address = $MessageSentFromMailbox; //Who we’re sending the email to 
    $mail->AddAddress($address, $MessageSentFromName); 
    $mail->Subject = 'Request'; //Subject of the email 
    $mail->Body = $the_body; 
    if(!$mail->Send()) //If the mail fails, send to customError 
     { 
     customError(1, $mail->ErrorInfo, "anon-email.php", "sending the email"); 
     } 
    } 
    else //If the candidate address exists, forward on the email 
    { 
    $mail = new PHPMailer(); // defaults to using php “mail()” 
    $mail -> ContentType = 'text/plain'; //Plain E-mail 
    $mail -> IsHTML(FALSE); //No HTML 
    $the_body = wordwrap($Body, 70); //Wordwrap for proper email formatting 
    $from_email_address = "$MessageSentFromMailbox"; 
    $mail->AddReplyTo($from_email_address); 
    $mail->SetFrom($from_email_address); 
    $address = $toArray[1]; //Who we’re sending the email to 
    $mail->AddAddress($address, $toArray[0]); //The name of the person we’re sending to 
    $mail->Subject = $EmailHeaders->subject; //Subject of the email 
    $mail->Body = ($the_body); 
    if(!$mail->Send()) //If mail fails, go to the custom error 
     { 
     customError(1, $mail->ErrorInfo, "anon-email.php", "sending the email"); 
     } 
    } 
    /* Mark the email for deletion after processing */ 
    imap_delete($connection, $MID); 
    } 
    imap_expunge($connection); // Expunge processes all of the emails marked to be deleted 
    imap_close($connection); 

    function searchRecipient() // function to search the database for the real email 
{ 
    global $MessageSentToMailbox; // bring in the alias email 
    $email_addr = mysql_query("SELECT email FROM tbl WHERE source='$MessageSentToMailbox'"); // temp store of the real email 
    $row = mysql_fetch_array($email_addr); //making temp store of data for use in program 
    if(empty($row['email'])) 
    { 
     return FALSE; 
    } 
    else /* Else, return find the person's name and return both in an array */ 
    { 
     $results = mysql_query("SELECT * FROM tbl WHERE email = '$email_addr'"); // temp store of both queries from this function 
     $row = mysql_fetch_array($results, $email_addr); //making temp store of data for use in program 
     $name = $row['author']; // taking the author data and naming its variable 
     return array($name, $email_addr); // this is the name and the real email address to be used in function call 
    } 
} 

function customError($errno, $errstr, $file, $line) 
{ 
    error_log("Error: [$errno] $errstr in $file at line number: $line",1, "[email protected]","From: [email protected]"); 
    die(); 
} 
+0

Возможно, это глупый вопрос, но уверены ли вы, что адрес $ заполняется должным образом? – andrewsi

+0

Я просмотрел код и ничего не видел, почему этого не будет. Вы заметили что-нибудь? –

+0

О, если это не так, то нужно отправить отскок назад. –

ответ

1

Вот первое, что я хотел бы попробовать: Казалось бы, что ваша функция searchRecipient не передается параметр. Вместо того, чтобы использовать ключевое слово global, я бы определил его в вызове функции. Кроме того, mysql_fetch_array не возвращает ассоциативный массив, который вы используете на следующем шаге. Я бы изменил это на mysql_fetch_assoc (это одно и то же). В этой функции также есть несколько других незначительных синтаксических поправок. Вот мои предложенные изменения этой функции. Я думаю, это должно решить вашу проблему. Или, по крайней мере, заставить вас двигаться вперед.

function searchRecipient($MessageSentToMailbox) // function to search the database for the real email 
{ 

    $email_addr = mysql_query("SELECT email FROM tbl WHERE source='$MessageSentToMailbox'"); // temp store of the real email 
    $row = mysql_fetch_assoc($email_addr); //making temp store of data for use in program 
    if(empty($row['email'])) 
    { 
     return FALSE; 
    } 
    else /* Else, return find the person's name and return both in an array */ 
    { 
     $email_addr = $row['email']; 
     $results = mysql_query("SELECT * FROM tbl WHERE email = '$email_addr'"); // temp store of both queries from this function 
     $row = mysql_fetch_assoc($results); //making temp store of data for use in program 
     $name = $row['author']; // taking the author data and naming its variable 
     return array($name, $email_addr); // this is the name and the real email address to be used in function call 
    } 
} 

Вы также можете объединить это в один запрос и сделать его немного проще. Вот это решение.

function searchRecipient($MessageSentToMailbox) 
{ 
    $results = mysql_query("SELECT email, author FROM tbl WHERE source='$MessageSentToMailbox'"); 
    $row = mysql_fetch_assoc($results); 
    if(empty($row['email']) || empty($row['author'])) return false; 
    return array($row['email'], $row['author']); 
} 
+0

Большое вам спасибо, я попробую. –

+0

У меня все еще такая же ошибка. –

+0

Хорошо, я нахожусь на телефоне в данный момент, а не на компьютере, поэтому трудно увидеть достаточно кода. Позвольте мне добраться до стола, и я закончу взглянуть на остальную часть кода. Я прекратил смотреть, как только увидел это. –

 Смежные вопросы

  • Нет связанных вопросов^_^