0

我正在尝试使用此脚本和快速邮件程序库将我的电子邮件中的附件自动转发到另一个帐户。事情似乎在一定程度上有效,但附件是作为编码文本发送的。我想按原样发送附件。我是 php 新手,无法弄清楚问题出在哪里。请帮我。

<?php
require_once 'lib/swift_required.php';
$hostname = '{imap.asd.com:993/imap/ssl}INBOX';
$username = 'abc@as.com';
$password = 'ppwppw';

/* 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);

        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);

 foreach($emails as $email_number) {

$result = Message_Parse($email_number);
$data = $result['attachments'];
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$attachment = Swift_Attachment::newInstance($data, 'recorded.mp3', 'audio/mp3');
$message = Swift_Message::newInstance('new messaeg')
  ->setFrom(array('aaa@bbb.com' => 'name'))
  ->setTo(array('aaa@ccc.com'))
  ->setBody($result['text'], 'Here is the message itself')
  ->attach($attachment);

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

?>
4

2 回答 2

0

$data 之后是$data = $result['attachments'];什么?认为它是某种附件容器对象。您可能需要进一步挖掘以获得正确的属性(附件),然后重新附加它。见:http ://dev.kayako.com/browse/SWIFT-2341

于 2012-10-05T18:39:15.993 回答
0

这看起来不对:

$data = $result['attachments'];
...
$attachment = Swift_Attachment::newInstance($data, 'recorded.mp3', 'audio/mp3');

当您使用此方法创建附件时,我认为$data应该是一个包含附件内容的字符串。但是$result['attachments']是原始邮件中所有附件的数组。似乎您应该遍历这些附件,为每个附件创建一个单独的 Swift_Attachment。您还应该保存原始消息中的文件名和内容类型,而不是将它们硬编码为recorded.mp3 和audio/mp3(除非应用程序确保这就是全部内容)。

至于它们被编码——MP3 不是纯文本,那么你有什么期望呢?

于 2012-10-05T20:56:19.667 回答