4

我正在使用脚本转发电子邮件地址并收到错误的消息编号错误。消息号被解析为函数的参数,我不知道如何获取消息号并将其作为参数插入。请帮忙。我是 php 新手。错误指出 $structure = imap_fetchstructure($connection, $id, FT_UID);$result['text'] = imap_body($connection, $id, FT_UID);部分。请帮我。

 <?php
require_once '../swift/lib/swift_required.php';
$hostname = '{imap.xyz.com:993/imap/ssl}INBOX';
$username = 'email';
$password = 'password';

/* try to connect */
$connection = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());

ini_set('memory_limit', '256M');

function Message_Parse($id)
{   
global $connection;
    if (is_resource($connection))
    {
        $result = array
        (
            'text' => null,
            'html' => null,
            'attachments' => array(),
        );  

                $structure = imap_fetchstructure($connection, $id, FT_UID);
        //print_r($structure);
//array_key_exists — Checks if the given key or index exists in the array
        if (is_array($structure) && array_key_exists('parts', $structure))
        {
            foreach ($structure->parts as $key => $part)
            {
                if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT')))
                {
                    $filename = null;
                    if ($part->ifparameters == 1)
                    {
                        $total_parameters = count($part->parameters);
                        for ($i = 0; $i < $total_parameters; $i++)
                        {
                            if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME'))
                            {
                                $filename = $part->parameters[$i]->value;
                                break;
                            }
                        }
                        if (is_null($filename))
                        {
                            if ($part->ifdparameters == 1)
                            {
                                $total_dparameters = count($part->dparameters);

                                for ($i = 0; $i < $total_dparameters; $i++)
                                {
                                    if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME'))
                                    {
                                        $filename = $part->dparameters[$i]->value;

                                        break;
                                    }
                                }
                            }
                        }
                    }
                    $result['attachments'][] = array
                    (
                        'filename' => $filename,
                        'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($connection, $id, ($key + 1), FT_UID))),
                    );
                }

                else
                {
                    if ($part->subtype == 'PLAIN')
                    {
                        $result['text'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID);
                    }

                    else if ($part->subtype == 'HTML')
                    {
                        $result['html'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID);
                    }

                    else
                    {
                        foreach ($part->parts as $alternative_key => $alternative_part)
                        {
                            if ($alternative_part->subtype == 'PLAIN')
                            {
                                echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                                $result['text'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }

                            else if ($alternative_part->subtype == 'HTML')
                            {
                                echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>';

                                $result['html'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID);
                            }
                        }
                    }
                }
            }
        }

        else
        {
            $result['text'] = imap_body($connection, $id, FT_UID);
        }

        $result['text'] = imap_qprint($result['text']);
        $result['html'] = imap_qprint(imap_8bit($result['html']));

        return $result;

    }

    return false;
}

 $emails = imap_search($connection,'ALL');
//  rsort($emails);

  /* for every email... */
 foreach($emails as $email_number) {
     echo $email_number;
$result = Message_Parse($email_number);
//$data = $result['attachments'];
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
//$attachment = Swift_Attachment::newInstance($result['attachments'], $filename, 'audio/mp3');
$message = Swift_Message::newInstance('test message1 ')
  ->setFrom(array('from address' => 'name'))
  ->setTo(array('to address.com'))
  ->setBody($result['text']);
//  ->attach($attachment);

  $result1 = $mailer->send($message);
 }

?>
4

2 回答 2

12

FT_UID标志表示您正在使用消息 uid,而不是消息序列 id。为了获取给定消息的 uid,您必须使用msg_uid()函数。

由于您是初学者,我几乎不认为这是您的问题,所以这里是如何获取给定消息的标题和内容的完整示例:

<?php

$emailAddress = 'postmaster@example.com';
$password = 'someSecretPassword';
$server = 'localhost';
$folder = 'Inbox';

$dsn = sprintf('{%s}%s', $server, $folder);
$mbox = imap_open($dsn, $emailAddress, $password);

if (!$mbox) {
    die ('Unable to connect');
}

$status = imap_status($mbox, $dsn, SA_ALL);
$msgs = imap_sort($mbox, SORTDATE, 1, SE_UID);

foreach ($msgs as $msguid) {
    $msgno = imap_msgno($mbox, $msguid);
    $headers = imap_headerinfo($mbox, $msgno);
    $structure = imap_fetchstructure($mbox, $msguid, FT_UID);

    var_dump($headers);
    var_dump($structure);
}
于 2012-10-31T13:17:50.937 回答
0

在循环内部时,我遇到了同样的错误,在处理电子邮件后,我将邮件移动到另一个文件夹。

在我注释掉错误之后imap_move,错误就消失了。

于 2022-03-05T20:39:15.197 回答