Sickaaron
Sickaaron

Reputation: 448

Email Only showing code / text with Outlook

I am having an issue with Outlook displaying email content and attachments correctly, the email content works fine on mail service providers such as Google Mail or Hotmail etc.

This is the email content outlook produces:

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

This email above is what is seen in outlook, in Google Mail, it shows as the HTML version.

This is the code im using for the email class.

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

What can i do so that the email in outlook will only show the plain text or html and the attachment, and not how it is currently showing.

Thanks

Upvotes: 0

Views: 3516

Answers (2)

Ernie
Ernie

Reputation: 1

I recently had a similar issue sending email to newer email clients. I was using Thunderbird 3, and upon upgrade to v8 or whatever it was at the time (newer for sure) all the emails came thru with no attachment and some of the source code for the email.

The fix was changing to a single multipart/mixed container. In the example, you have 2 containers. Looked ok to me, but dropping the nested container solved the issue.

Also, I use \n not \r\n

Upvotes: 0

Sabeen Malik
Sabeen Malik

Reputation: 10880

I can't be sure but I seem to recall coming across the same issue and turned out that using '\n' instead of '\r\n' helped, have you tried it?

Upvotes: 1

Related Questions