2012-05-16 201 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不正在傳遞一個參數。我會在你的函數調用中定義它,而不是使用全局關鍵字。此外,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

好的,我現在正在使用手機而不是電腦,所以很難看到足夠的代碼。讓我走到辦公桌前,我會看完其餘的代碼。我一看到就停止看。 –

相關問題