php - 创建 Craigslist 匿名电子邮件转发程序

标签 php mysql imap gmail-imap

以下程序旨在将传入的电子邮件别名与数据库中的别名进行匹配,并将电子邮件转发到正确的地址,就像 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 = 'name@domain.com';
    $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, "name@domain.com","From: name@domain.com.com");   
    die();
}

最佳答案

这是我要尝试的第一件事: 您的函数 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']);
}

关于php - 创建 Craigslist 匿名电子邮件转发程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10625616/

相关文章:

php - 如何使用jquery和php验证密码?

php - 如何在 while 循环内的 <li> 标记内创建一个自动递增的 ID?

mysql - 从表中删除重复的行(每天的第一个时间戳除外)

PHP catch 无法打开流 : HTTP request failed! HTTP/1.1 401 Unauthorized

php - 使用 JQUERY 修改 url_for,Symfony 的 href

php - Twitter 动态输入字段上的 Typeahead 自动完成功能

php - 使用foreach循环检索记录,如果找不到则插入到表中

c# - Office 365 Exchange Online - 客户端凭据 - IMAP 无身份验证

ssl - Mule ESB 3.3 - 接收 IMAPS 邮件 (Gmail)

Node.js mailparser 模块错误 : Cannot find module 'iconv'