0

次のプログラムは、受信メールのエイリアスをデータベース内のエイリアスと照合し、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();
}
4

1 に答える 1

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
    }
}

これを 1 つのクエリに結合して、少し簡単にすることもできます。これがその解決策です。

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']);
}
于 2012-05-16T20:11:27.633 に答える