电子邮件仅显示Outlook的代码/文本

时间:2012-06-25 20:11:57

标签: php email outlook

我遇到Outlook正确显示电子邮件内容和附件的问题,电子邮件内容在邮件服务提供商(如Google Mail或Hotmail等)上运行良好。

这是Outlook产生的电子邮件内容:

X-Mailer: simpleEmailClass v1.0

MIME-Version: 1.0

Content-Type: multipart/mixed; boundary="13406541204fe8c2284f5aa"

Message-Id: < - removed - >
Date: Mon, 25 Jun 2012 20:55:20 +0100 (BST)

--13406541204fe8c2284f5aa

Content-Type: multipart/alternative; boundary="13406541204fe8c2284f98f"


--13406541204fe8c2284f98f

Content-Type: text/plain; charset="ISO-8859-1"

Content-Transfer-Encoding: quoted-printable

Dear Rachel
Further to your recent enquiry please accept this email as confirmation of your booking.
Kind Regards
Hanna

--13406541204fe8c2284f98f

Content-Type: text/html; charset="ISO-8859-1"

Content-Transfer-Encoding: quoted-printable



<html>
<body>
<html><body><p>

    <span style="font-family: arial, helvetica, sans-serif; font-size: 12px; ">Dear Rachel</span></p>

<p>

    <span style="font-size:12px;"><span style="font-family:arial,helvetica,sans-serif;">Further to your recent enquiry please accept this email as confirmation of your booking.</span></span></p>

<p>

    <span style="font-size:12px;"><span style="font-family:arial,helvetica,sans-serif;">Kind Regards</span></span></p>

<p>

    <font face="arial, helvetica, sans-serif">Hanna</font></p>

</body></html>
</body>
</html>


--13406541204fe8c2284f98f--



--13406541204fe8c2284f5aa

Content-Type: application/pdf; name="invoice.pdf"

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename="invoice.pdf"

JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1Bh

^^ This go on and on for a long time, thought it was best not to post it all.

--13406541204fe8c2284f5aa--

上面的这封电子邮件是在Outlook中看到的,在Google Mail中,它显示为HTML版本。

这是我用于电子邮件类的代码。

<?php
class sec {
    var $secVersion = '1.0';
    var $to = '';
    var $Cc = array ();
    var $Bcc = array ();
    var $subject = '';
    var $message = '';
    var $attachment = array ();
    var $embed = array ();
    var $charset = 'ISO-8859-1';
    var $emailboundary = '';
    var $emailheader = '';
    var $textheader = '';
    var $errors = array ();

    function __construct($toname, $toemail, $fromname, $fromemail) {
        $this->emailboundary = uniqid ( time () );
        $this->to = "{$toname} <" . $this->validateEmail ( $toemail ) . ">";
        $email = $this->validateEmail ( $fromemail );
        $this->emailheader .= "From: {$fromname} <{$email}>\r\n";
    }

    function validateEmail($email) {
        if (! preg_match ( '/^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}$/i', $email ))
            die ( 'The Email ' . $email . ' is not Valid.' );

        return $email;
    }

    function Cc($email) {
        $this->Cc [] = $this->validateEmail ( $email );
    }

    function Bcc($email) {
        $this->Bcc [] = $this->validateEmail ( $email );
    }

    function buildHead($type) {
        $count = count ( $this->$type );
        if ($count > 0) {
            $this->emailheader .= "{$type}: ";
            $array = $this->$type;
            for($i = 0; $i < $count; $i ++) {
                if ($i > 0)
                    $this->emailheader .= ',';
                $this->emailheader .= $this->validateEmail ( $array [$i] );
            }
            $this->emailheader .= "\r\n";
        }
    }

    function buildMimeHead() {
        $this->buildHead ( 'Cc' );
        $this->buildHead ( 'Bcc' );

        $this->emailheader .= "X-Mailer: simpleEmailClass v{$this->secVersion}\r\n";
        $this->emailheader .= "MIME-Version: 1.0\r\n";
    }

    function buildMessage($subject, $message = '') {
        $textboundary = uniqid ( time () );
        $this->subject = strip_tags ( trim ( $subject ) );

        $this->textheader = "Content-Type: multipart/alternative; boundary=\"$textboundary\"\r\n\r\n";
        $this->textheader .= "--{$textboundary}\r\n";
        $this->textheader .= "Content-Type: text/plain; charset=\"{$this->charset}\"\r\n";
        $this->textheader .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
        $this->textheader .= strip_tags ( $message ) . "\r\n\r\n";
        $this->textheader .= "--$textboundary\r\n";
        $this->textheader .= "Content-Type: text/html; charset=\"$this->charset\"\r\n";
        $this->textheader .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
        $this->textheader .= "<html>\n<body>\n{$message}\n</body>\n</html>\r\n\r\n";
        $this->textheader .= "--{$textboundary}--\r\n\r\n";
    }

    function mime_type($file) {
        return (function_exists ( 'mime_content_type' )) ? mime_content_type ( $file ) : trim ( exec ( 'file -bi ' . escapeshellarg ( $file ) ) );
    }

    function attachment($file, $filename = NULL, $direct_input = FALSE, $mime = '') {
        if(!$direct_input)
        {
            if (is_file ( $file )) {
                $basename = $filename? $filename: basename ( $file );
                $attachmentheader = "--{$this->emailboundary}\r\n";
                $attachmentheader .= "Content-Type: " . $this->mime_type ( $file ) . "; name=\"{$basename}\"\r\n";
                $attachmentheader .= "Content-Transfer-Encoding: base64\r\n";
                $attachmentheader .= "Content-Disposition: attachment; filename=\"{$basename}\"\r\n\r\n";
                $attachmentheader .= chunk_split ( base64_encode ( fread ( fopen ( $file, "rb" ), filesize ( $file ) ) ), 72 ) . "\r\n";

                $this->attachment [] = $attachmentheader;
            } else {
                die ( 'The File ' . $file . ' does not exsist.' );
            }
        }
        else
        {
            $basename = $filename;
            $attachmentheader = "--{$this->emailboundary}\r\n";
            $attachmentheader .= "Content-Type: $mime; name=\"{$basename}\"\r\n";
            $attachmentheader .= "Content-Transfer-Encoding: base64\r\n";
            $attachmentheader .= "Content-Disposition: attachment; filename=\"{$basename}\"\r\n\r\n";
            $attachmentheader .= chunk_split ( base64_encode ( $file ), 72 ) . "\r\n";

            $this->attachment [] = $attachmentheader;
        }
    }

    function embed($file) {
        if (is_file ( $file )) {
            $basename = basename ( $file );
            $fileinfo = pathinfo ( $basename );
            $contentid = md5 ( uniqid ( time () ) ) . "." . $fileinfo ['extension'];
            $embedheader = "--{$this->emailboundary}\r\n";
            $embedheader .= "Content-Type: " . $this->mime_type ( $file ) . "; name=\"{$basename}\"\r\n";
            $embedheader .= "Content-Transfer-Encoding: base64\r\n";
            $embedheader .= "Content-Disposition: inline; filename=\"{$basename}\"\r\n";
            $embedheader .= "Content-ID: <{$contentid}>\r\n\r\n";
            $embedheader .= chunk_split ( base64_encode ( fread ( fopen ( $file, "rb" ), filesize ( $file ) ) ), 72 ) . "\r\n";

            $this->embed [] = $embedheader;

            return "<img src=3D\"cid:{$contentid}\">";
        } else {
            die ( 'The File ' . $file . ' does not exsist.' );
        }
    }

    function sendmail() {
        $this->buildMimeHead ();

        $header = $this->emailheader;

        $attachcount = count ( $this->attachment );
        $embedcount = count ( $this->embed );

        if ($attachcount > 0 || $embedcount > 0) {
            $header .= "Content-Type: multipart/mixed; boundary=\"{$this->emailboundary}\"\r\n\r\n";
            $header .= "--{$this->emailboundary}\r\n";
            $header .= $this->textheader;

            if ($attachcount > 0)
                $header .= implode ( "", $this->attachment );
            if ($embedcount > 0)
                $header .= implode ( "", $this->embed );
            $header .= "--{$this->emailboundary}--\r\n\r\n";
        } else {
            $header .= $this->textheader;
        }

        return mail ( $this->to, $this->subject, $this->message, $header );
    }
}

我该怎么办才能使Outlook中的电子邮件只显示纯文本或html和附件,而不显示当前显示的内容。

由于

2 个答案:

答案 0 :(得分:1)

我无法确定,但我似乎回想起遇到同样的问题并且发现使用'\ n'代替'\ r \ n'帮助了,你试过吗?

答案 1 :(得分:0)

我最近有类似的问题向较新的电子邮件客户端发送电子邮件。我正在使用Thunderbird 3,升级到v8或当时的任何东西(当初更新)所有的电子邮件都没有附件和电子邮件的一些源代码。

修复程序正在更改为单个multipart / mixed容器。在该示例中,您有2个容器。看起来不错,但是放下嵌套的容器解决了这个问题。

另外,我使用\ n而不是\ r \ n