附件作为编码文本发送

时间:2012-10-05 18:19:40

标签: php email

我正在尝试使用此脚本和swift邮件程序库自动将我的电子邮件转发到另一个帐户。事情似乎在某种程度上起作用,但附件是作为编码文本发送的。我想按原样发送附件。我是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);

?>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这看起来并不正确:

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

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

至于他们被编码 - MP3不是纯文本,所以你期待什么?