problems with creating both HTML and plaintext emails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news@celticbear.com

    problems with creating both HTML and plaintext emails

    Before I begin, I realise there's a big war regarding HTML in e-mails.
    Personally, I don't like it. Unfortunately, I'm being paid by my
    company to create an automated newsletter for our customers.
    I did all I could just to get them to allow me to have it sent in both
    plaintext and HTML so that the e-mail clients have a choice.

    Anyway. I've looked around the RFC's and the source of similar e-mails,
    and I'm certain I figured out how to do it...but there's still some
    problems I can't figure out.

    Below will be the PHP script I'm using to create the message.
    But what's happening is that it appears to work fine in the major
    E-mail clients like Outlook and Thunderbird, but doesn't work in Kmail
    or Evolution or other clients.

    What am I doing wrong?
    Thanks for any advice!
    Liam

    <?php
    // PAGE CREATED 2004-06-01 by Liam
    /*############## ############### ############### ####
    ### DETERMINE DAY APPROPRIATENESS
    ############### ############### ############### #####*/
    $today_day = date("w"); // Day of the week numeric: Sun=0,
    Mon=1...Sat=6
    $today = date("Y-m-d");
    /*############## ############### ############### ####
    ### RUN PROCESS
    ############### ############### ############### #####*/

    $to="Recipient Name <my-email-addy>";
    $from="Sender Name <another-addy>";
    $subject="My first HTML E-mail";
    $mime_boundary= "==Multipart_Bo undary_x".md5(m t_rand())."x";
    $headers = "From: $from\r\n" .
    "MIME-Version: 1.0\r\n" .
    "Content-Type:multipart/alternative;\n" .
    " boundary=\"{$mi me_boundary}\r\ n\"";
    $headers.= "From: $from\r\n";
    $message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary }\n" .
    "Content-Disposition: inline\n".
    "Content-Transfer-Encoding: 7bit\n".
    "Content-Type: text/plain\n\n".
    "The plaintext message goes here!
    " .
    "--{$mime_boundary }\n" .
    "Content-Disposition: inline\n".
    "Content-Transfer-Encoding: 7bit\n".
    "Content-Type: text/html\n\n".
    "<html>
    <head>
    <title>HTML MSG HERE</title>
    </head>
    <body bgcolor=\"#ffff ff\">
    </body>
    </html>
    ".
    "--{$mime_boundary }\n
    ";
    if (mail($to, $subject, $message, $headers))
    echo "Message Sent!";
    else
    echo "Failed to send message.";
    ?>

  • Alan Connor

    #2
    Re: problems with creating both HTML and plaintext emails

    On 20 Jan 2005 15:41:50 -0800, news@celticbear .com
    <news@celticbea r.com> wrote:
    [color=blue]
    > Before I begin, I realise there's a big war regarding HTML
    > in e-mails. Personally, I don't like it. Unfortunately, I'm
    > being paid by my company to create an automated newsletter for
    > our customers. I did all I could just to get them to allow me
    > to have it sent in both plaintext and HTML so that the e-mail
    > clients have a choice.[/color]

    Good work there.

    I don't accept HTML mails, and I am hardly alone in this choice.

    In some cases, like an initial contact, I'll (my filter will)
    send the body through a utility to convert it into plain text,
    dumping images, soundfiles, and attachments.
    [color=blue]
    >
    > Anyway. I've looked around the RFC's and the source of similar
    > e-mails, and I'm certain I figured out how to do it...but
    > there's still some problems I can't figure out.
    >
    > Below will be the PHP script I'm using to create the message.
    > But what's happening is that it appears to work fine in the
    > major E-mail clients like Outlook and Thunderbird, but doesn't
    > work in Kmail or Evolution or other clients.
    >
    > What am I doing wrong? Thanks for any advice! Liam[/color]

    <snip>

    I don't read PHP, News, or generally send html mails, but here's
    a link to a post that you might find useful:



    And here's all that's needed to send an in-line html mail:

    #
    # From: no_reply@domain .com
    # To: recipient@whate ver.com
    # Subject: Whatever
    # Content-Type: text/html
    # Content-Transfer-Encoding: 7bit
    # Content-Disposition: inline
    #
    # <BODY>
    #
    # rest of the html
    #
    #
    # </BODY>
    # .
    #
    #


    AC


    --
    Pro-Active Spam Fighter
    Pass-list --> Spam-Filter --> Challenge-Response


    Comment

    • Sam

      #3
      Re: problems with creating both HTML and plaintext emails

      news@celticbear .com writes:
      [color=blue]
      > Below will be the PHP script I'm using to create the message.
      > But what's happening is that it appears to work fine in the major
      > E-mail clients like Outlook and Thunderbird, but doesn't work in Kmail
      > or Evolution or other clients.
      >
      > What am I doing wrong?[/color]
      [color=blue]
      > "Content-Type:multipart/alternative;\n" .
      > " boundary=\"{$mi me_boundary}\r\ n\"";[/color]

      Bug #1. For starters, you're using LF everywhere else, and here you have
      CRLF. Furthermore, it's misplaced. It should be after the closing quote.

      You'll have to investigate whether PHP's mail() functions expects all lines
      to be terminated by LF, or CRLF. I don't recall offhand. Then you must
      change your code and use a consistent EOL.
      [color=blue]
      > <title>HTML MSG HERE</title>
      > </head>
      > <body bgcolor=\"#ffff ff\">
      > </body>
      > </html>
      > ".
      > "--{$mime_boundary }\n
      > ";[/color]

      Bug #2. The closing boundary delimiter should have a trailing "--".




      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.2.4 (GNU/Linux)

      iD8DBQBB8FTVx9p 3GYHlUOIRAnjcAJ 9zk5YHpb8DRelZp lq/UzP5ayI5jwCZAT9 v
      t8TQH2ZX8umJuK0 9IsGSRRQ=
      =En7E
      -----END PGP SIGNATURE-----

      Comment

      • Sam

        #4
        Re: problems with creating both HTML and plaintext emails

        Beavis writes:
        [color=blue]
        > Good work there.
        >
        > I don't accept HTML mails, and I am hardly alone in this choice.[/color]

        Nobody asked you.
        [color=blue]
        >
        > In some cases, like an initial contact, I'll (my filter will)
        > send the body through a utility to convert it into plain text,
        > dumping images, soundfiles, and attachments.[/color]

        Nobody cares, Beavis.
        [color=blue]
        > <snip>
        >
        > I don't read PHP, News, or generally send html mails, but here's
        > a link to a post that you might find useful:
        >
        > http://groups.google.com/groups?selm...oglegroups.com
        >
        > And here's all that's needed to send an in-line html mail:[/color]

        Beavis, you ignorant slut. He wants to know how properly format
        multipart/alternative MIME content, and not just HTML mail.

        If you don't understand what someone's asking, just keep your yap shut and
        let your mental superiors handle it.


        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.4 (GNU/Linux)

        iD8DBQBB8FVex9p 3GYHlUOIRArmuAJ 9QquO3S1NuU9+lO fpweaGUvN7uqwCd HYmC
        VQIlhBv0lkmenYZ cueRnOPg=
        =uTQz
        -----END PGP SIGNATURE-----

        Comment

        • Jem Berkes

          #5
          Re: problems with creating both HTML and plaintext emails

          > Anyway. I've looked around the RFC's and the source of similar e-mails,[color=blue]
          > and I'm certain I figured out how to do it...but there's still some
          > problems I can't figure out.[/color]

          Besides the technical side of creating the content, you should of course
          also test your output with a number of popular readers to see what it looks
          like! I would suggest,

          hotmail (web)
          gmail (web0
          Outlook
          Eudora
          Pegasus Mail
          Mozilla Thunderbird

          It's a nagging problem with design for presentation... you had better
          really see what the result looks like :)

          --
          Jem Berkes
          Windows, UNIX software and system design
          Made with Softr, the easiest way to turn your data into portals and internal tools.

          Comment

          • news@celticbear.com

            #6
            Re: problems with creating both HTML and plaintext emails

            Sam wrote:[color=blue]
            > news@celticbear .com writes:[color=green]
            > > What am I doing wrong?[/color]
            >[color=green]
            > > "Content-Type:multipart/alternative;\n" .
            > > " boundary=\"{$mi me_boundary}\r\ n\"";[/color]
            >
            > Bug #1. For starters, you're using LF everywhere else, and here you[/color]
            have[color=blue]
            > CRLF. Furthermore, it's misplaced. It should be after the closing[/color]
            quote.[color=blue]
            >
            > You'll have to investigate whether PHP's mail() functions expects all[/color]
            lines[color=blue]
            > to be terminated by LF, or CRLF. I don't recall offhand. Then you[/color]
            must[color=blue]
            > change your code and use a consistent EOL.[/color]

            OK, Below is my updated script.
            \n is the appropriate EOL, so I've fixed that as well as where the
            quotes were. That was just bad on my part.
            [color=blue][color=green]
            > > ".
            > > "--{$mime_boundary }\n
            > > ";[/color]
            >
            > Bug #2. The closing boundary delimiter should have a trailing "--".[/color]

            And you'll see I added that too.
            I also looked around at more source code of my successfully received
            newsletter e-mails and am trying to emulate them, so I made a couple of
            other changes.
            And yet, still doesn't work.
            No matter what e-mail client I use, the source code for the e-mail
            looks fine, it's all there...but nothing is visible in the e-mail
            display.
            In Kmail, it show a brief summary of the parts the e-mail includes, and
            on mine it always says:

            e-mail type: multipart/alternative 8bit (that's odd considering I use
            7bit in the code) 19.6 KB
            body part: Plain Text Document 0 KB

            E-mails that work always has a 2nd "body part" as HTML listed, and
            obviously, the appropriate size. Why mine seems to cut off detection at
            the plain text doc and not have the size, I can't fathom. Everything
            appears the same as working e-mails.

            Anyway, thanks for the replies, and any tips are appreciated!
            Liam

            <?php
            // PAGE CREATED 2004-06-01 by Liam
            $today_day = date("w"); // Day of the week numeric: Sun=0,
            Mon=1...Sat=6
            $today = date("Y-m-d");

            $to="Recipient Name <e-mail-addy>";
            $from="Sender Name <another-email-addy>";
            $subject="My first HTML E-mail";
            $mime_boundary= "Multipart-Boundary-".md5(mt_rand() );
            $headers = "From: $from\n" .
            "MIME-Version: 1.0\n" .
            "Content-Type:multipart/alternative;\n" .
            " boundary=\"--{$mime_boundary }--\"\n";
            $headers.= "From: $from\n";
            $message = "--{$mime_boundary }--\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
            "Content-Disposition: inline\n".
            "Content-Transfer-Encoding: 7bit\n\n".
            "(PLAIN TEXT DOC GOES HERE)" .
            "--{$mime_boundary }--\n" .
            "Content-Type: text/html; charset=\"iso-8859-1\"\n".
            "Content-Disposition: inline\n".
            "Content-Transfer-Encoding: 7bit\n\n".
            "<html>

            <head>
            <title>Welcom e to E-Boot Camp Phase I</title>
            </head>

            <body bgcolor=\"#ffff ff\">
            (HTML DOC GOES IN HERE)
            </body>
            </html>
            ".
            "--{$mime_boundary }---\n\n\n

            ";
            if (mail($to, $subject, $message, $headers))
            echo "Message Sent!";
            else
            echo "Failed to send message.";

            ?>

            Comment

            • Kartic

              #7
              Re: problems with creating both HTML and plaintext emails

              news@celticbear .com said the following on 1/20/2005 6:41 PM:[color=blue]
              > Before I begin, I realise there's a big war regarding HTML in e-mails.
              > Personally, I don't like it. Unfortunately, I'm being paid by my
              > company to create an automated newsletter for our customers.
              > I did all I could just to get them to allow me to have it sent in both
              > plaintext and HTML so that the e-mail clients have a choice.
              >
              > Anyway. I've looked around the RFC's and the source of similar e-mails,
              > and I'm certain I figured out how to do it...but there's still some
              > problems I can't figure out.
              >
              > Below will be the PHP script I'm using to create the message.
              > But what's happening is that it appears to work fine in the major
              > E-mail clients like Outlook and Thunderbird, but doesn't work in Kmail
              > or Evolution or other clients.
              >
              > What am I doing wrong?
              > Thanks for any advice!
              > Liam[/color]

              Liam - Why don't you take a look at phpmailer available at
              http://phpmailer.sourceforge.net/ ?

              It is a PHP class that helps you create MIME emails, including emails
              with HTML and also build an alternative plain text part for MUAs that do
              not read HTML.

              This class is good and will save you time building HTML emails.

              Of course, as Jem mentioned, test your sample emails with the prominent
              free email services.

              HTH.

              Thanks,
              --Kartic

              Comment

              • Sam

                #8
                Re: problems with creating both HTML and plaintext emails

                news@celticbear .com writes:
                [color=blue]
                >[color=green][color=darkred]
                >> > ".
                >> > "--{$mime_boundary }\n
                >> > ";[/color]
                >>
                >> Bug #2. The closing boundary delimiter should have a trailing "--".[/color]
                >
                > And you'll see I added that too.[/color]

                No you didn't. You added trailing --s to every boundary delimiter, not just
                the closing one.

                You should review the RFCs, and use this message as an example of how to
                correctly set the delimiters for a multipart MIME message.


                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.2.4 (GNU/Linux)

                iD8DBQBB8azqx9p 3GYHlUOIRApYyAJ 4lg8QhtXQxbEYSy YLOMkWf1cUhOgCd FMzU
                /5B/TI6X+oeScmiCZwq glS0=
                =uWba
                -----END PGP SIGNATURE-----

                Comment

                Working...