Help with mail() - message not displaying

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karl Groves

    #1

    Help with mail() - message not displaying

    I'm trying to work out a mail system which can send an attachment as well
    as an HTML formatted message (and a default plain text version).

    I found some pretty good code on PHP.net and modified it a little but I
    can't seem to get it to work.

    It attaches the file properly, but only displays the Plain Text message,
    even in an HTML-capable mail client.

    I'm guessing it has something to do with the placement of the boundaries,
    but I can't figure it out.

    TIA


    <?php

    // Is the OS Windows or Mac or Linux
    if (strtoupper(sub str(PHP_OS,0,3) == 'WIN')) {
    $eol = "\r\n";
    } elseif (strtoupper(sub str(PHP_OS,0,3) == 'MAC')) {
    $eol = "\r";
    } else {
    $eol = "\n";
    }


    // Set Up Message Details
    $recipient = "foo@bar.co m";
    $subject = "E-mail Message ".date("Y/m/d H:i:s");
    $from_name = "Mr. Foo Bar";
    $from_email = "foo@bar.co m";
    $text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
    message
    $html_msg = $eol."<html><bo dy><strong>HTML MESSAGE BODY</strong></body>
    </html>".$eol; // teh HTML goodness

    // File for Attachment
    $file_name = "file.gif"; // name of the file being attached
    $f_name = $path.$file_nam e; // path + name of the file being attached
    $handle = fopen($f_name, 'rb');
    $f_contents = fread($handle, filesize($f_nam e));
    $f_contents = chunk_split(bas e64_encode($f_c ontents));
    $f_type = filetype($f_nam e);
    fclose($handle) ;


    // Headers
    $headers = "";
    $headers .= "From: $from_name <$from_email>". $eol;
    $headers .= "Reply-To: $from_name <$from_email>". $eol;
    $headers .= "Return-Path: $from_name <$from_email>". $eol;
    $headers .= "X-Sender: <$from_email>". $eol;
    $headers .= "Errors-To: <$from_email>". $eol;
    $headers .= "Message-ID: <".md5(uniqid(r and())).".".pre g_replace("/[^a-z0-
    9]/i", "", $from_name).">" .$eol;
    $headers .= "X-Mailer: PHP v".phpversion() .$eol;
    $headers .= "X-Priority: 3\n";

    // Boundary for marking the split & Multitype Headers
    $mime_boundary = "php-".md5(rand( ));
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".
    $mime_boundary. "\"".$eol;
    $msg = "";

    // Attachment
    $msg .= "--".$mime_boundar y.$eol;
    $msg .= "Content-Type: $f_type; name=\"".$file_ name."\"".$eol;
    $msg .= "Content-Transfer-Encoding: base64".$eol;
    $msg .= "Content-Disposition: attachment; filename=\"".$f ile_name."\"".
    $eol.$eol;
    $msg .= $f_contents.$eo l.$eol;
    $msg .= "--".$mime_boundar y.$eol;


    // Text Version
    $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
    $msg .= "Content-Transfer-Encoding: 8bit".$eol;
    $msg .= "This is a multi-part message in MIME format.".$eol;
    $msg .= "If you are reading this, please update your email-reading-
    software.".$eol ;
    $msg .= $text_msg;
    $msg .= "--".$mime_boundar y.$eol;

    // HTML Version
    $htmlalt_mime_b oundary = $mime_boundary. "_htmlalt";
    $msg .= "Content-Type: multipart/alternative; boundary=\"".
    $htmlalt_mime_b oundary."\"".$e ol;
    $msg .= "--".$htmlalt_mime _boundary.$eol. $eol;
    $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
    $msg .= "Content-Transfer-Encoding: 8bit".$eol;
    $msg .= $html_msg;
    $msg .= $eol.$eol."--".$htmlalt_mime _boundary."--".$eol.$eol ;

    // Finish Message
    $msg .= "--".$mime_boundar y."--".$eol.$eol ;

    // SEND THE EMAIL
    ini_set(sendmai l_from, $from_email); // the INI lines are to force the
    From Address to be used !
    mail($recipient , $subject, $msg, $headers);
    ini_restore(sen dmail_from);

    // DEBUG STUFF
    //print("<pre>$ms g</pre>");
    ?>


    --
    Karl Groves

  • Tom

    #2
    Re: Help with mail() - message not displaying

    uncomment the last line:

    //print("<pre>$ms g</pre>");

    Also, unless you have other reasons for writing your own code here, you
    might check out something phpmailer. It may help save you from
    reinventing the wheel.

    Tom


    Karl Groves wrote:
    I'm trying to work out a mail system which can send an attachment as well
    as an HTML formatted message (and a default plain text version).
    >
    I found some pretty good code on PHP.net and modified it a little but I
    can't seem to get it to work.
    >
    It attaches the file properly, but only displays the Plain Text message,
    even in an HTML-capable mail client.
    >
    I'm guessing it has something to do with the placement of the boundaries,
    but I can't figure it out.
    >
    TIA
    >
    >
    <?php
    >
    // Is the OS Windows or Mac or Linux
    if (strtoupper(sub str(PHP_OS,0,3) == 'WIN')) {
    $eol = "\r\n";
    } elseif (strtoupper(sub str(PHP_OS,0,3) == 'MAC')) {
    $eol = "\r";
    } else {
    $eol = "\n";
    }
    >
    >
    // Set Up Message Details
    $recipient = "foo@bar.co m";
    $subject = "E-mail Message ".date("Y/m/d H:i:s");
    $from_name = "Mr. Foo Bar";
    $from_email = "foo@bar.co m";
    $text_msg = $eol."TEXT MESSAGE BODY".$eol; // plain text version of the
    message
    $html_msg = $eol."<html><bo dy><strong>HTML MESSAGE BODY</strong></body>
    </html>".$eol; // teh HTML goodness
    >
    // File for Attachment
    $file_name = "file.gif"; // name of the file being attached
    $f_name = $path.$file_nam e; // path + name of the file being attached
    $handle = fopen($f_name, 'rb');
    $f_contents = fread($handle, filesize($f_nam e));
    $f_contents = chunk_split(bas e64_encode($f_c ontents));
    $f_type = filetype($f_nam e);
    fclose($handle) ;
    >
    >
    // Headers
    $headers = "";
    $headers .= "From: $from_name <$from_email>". $eol;
    $headers .= "Reply-To: $from_name <$from_email>". $eol;
    $headers .= "Return-Path: $from_name <$from_email>". $eol;
    $headers .= "X-Sender: <$from_email>". $eol;
    $headers .= "Errors-To: <$from_email>". $eol;
    $headers .= "Message-ID: <".md5(uniqid(r and())).".".pre g_replace("/[^a-z0-
    9]/i", "", $from_name).">" .$eol;
    $headers .= "X-Mailer: PHP v".phpversion() .$eol;
    $headers .= "X-Priority: 3\n";
    >
    // Boundary for marking the split & Multitype Headers
    $mime_boundary = "php-".md5(rand( ));
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".
    $mime_boundary. "\"".$eol;
    $msg = "";
    >
    // Attachment
    $msg .= "--".$mime_boundar y.$eol;
    $msg .= "Content-Type: $f_type; name=\"".$file_ name."\"".$eol;
    $msg .= "Content-Transfer-Encoding: base64".$eol;
    $msg .= "Content-Disposition: attachment; filename=\"".$f ile_name."\"".
    $eol.$eol;
    $msg .= $f_contents.$eo l.$eol;
    $msg .= "--".$mime_boundar y.$eol;
    >
    >
    // Text Version
    $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
    $msg .= "Content-Transfer-Encoding: 8bit".$eol;
    $msg .= "This is a multi-part message in MIME format.".$eol;
    $msg .= "If you are reading this, please update your email-reading-
    software.".$eol ;
    $msg .= $text_msg;
    $msg .= "--".$mime_boundar y.$eol;
    >
    // HTML Version
    $htmlalt_mime_b oundary = $mime_boundary. "_htmlalt";
    $msg .= "Content-Type: multipart/alternative; boundary=\"".
    $htmlalt_mime_b oundary."\"".$e ol;
    $msg .= "--".$htmlalt_mime _boundary.$eol. $eol;
    $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
    $msg .= "Content-Transfer-Encoding: 8bit".$eol;
    $msg .= $html_msg;
    $msg .= $eol.$eol."--".$htmlalt_mime _boundary."--".$eol.$eol ;
    >
    // Finish Message
    $msg .= "--".$mime_boundar y."--".$eol.$eol ;
    >
    // SEND THE EMAIL
    ini_set(sendmai l_from, $from_email); // the INI lines are to force the
    From Address to be used !
    mail($recipient , $subject, $msg, $headers);
    ini_restore(sen dmail_from);
    >
    // DEBUG STUFF
    //print("<pre>$ms g</pre>");
    ?>
    >
    >
    --
    Karl Groves
    www.karlcore.com

    Comment

    • Karl Groves

      #3
      Re: Help with mail() - message not displaying

      "Tom" <klenwell@gmail .comwrote in
      news:1164830182 .439220.189140@ 80g2000cwy.goog legroups.com:
      uncomment the last line:
      >
      //print("<pre>$ms g</pre>");
      That line is just a debug line I put there to see the message printed. It
      is done *after* the mail is sent, so that doesn't solve my problem
      Also, unless you have other reasons for writing your own code here,
      you might check out something phpmailer. It may help save you from
      reinventing the wheel.
      This is actually going into another pre-existing script, so the wheel was
      already reinvented.




      --
      Karl Groves

      Comment

      • Tom

        #4
        Re: Help with mail() - message not displaying

        My apologies for the first response. Felt so bad that I cracked open
        phpmailer to glance at its code to see if any obvious differences stood
        out. I don't have any experience with this particular problem and
        prefer my email in good old plain text, so that's about the best I can
        do.

        I did notice that phpmailer does encode some parts of the message. I
        refer specifically to this line of code:

        @mail($to, $this->EncodeHeader($ this->Subject), $body, $header,
        $params);

        Could it be an encoding problem, specifically something somewhere not
        being encoded in the right way?

        If that doesn't help, my further apologies. I'll let the experts take
        over from here.

        Tom

        On Nov 29, 12:16 pm, Karl Groves <k...@NOSPAMkar lcore.comwrote:
        "Tom" <klenw...@gmail .comwrote innews:11648301 82.439220.18914 0@80g2000cwy.go oglegroups.com:
        >
        uncomment the last line:
        >
        //print("<pre>$ms g</pre>");That line is just a debug line I put there to see the message printed. It
        is done *after* the mail is sent, so that doesn't solve my problem
        >
        Also, unless you have other reasons for writing your own code here,
        you might check out something phpmailer. It may help save you from
        reinventing the wheel.This is actually going into another pre-existing script, so the wheel was
        already reinvented.
        >
        --
        Karl Groveswww.karlc ore.com

        Comment

        • bill

          #5
          Re: Help with mail() - message not displaying

          Tom wrote:
          uncomment the last line:
          >
          //print("<pre>$ms g</pre>");
          >
          Also, unless you have other reasons for writing your own code here, you
          might check out something phpmailer. It may help save you from
          reinventing the wheel.
          >
          Tom
          >
          Please excuse the ignorance,
          where would I find phpmailer ?

          bill

          Comment

          • Tom

            #6
            Re: Help with mail() - message not displaying



            Tom

            On Nov 30, 4:09 am, bill <nob...@spamcop .netwrote:
            Tom wrote:
            uncomment the last line:
            >
            //print("<pre>$ms g</pre>");
            >
            Also, unless you have other reasons for writing your own code here, you
            might check out something phpmailer. It may help save you from
            reinventing the wheel.
            >
            TomPlease excuse the ignorance,
            where would I find phpmailer ?
            >
            bill

            Comment

            • bill

              #7
              Re: Help with mail() - message not displaying

              Tom wrote:THank you kindly
              bill



              >
              On Nov 30, 4:09 am, bill <nob...@spamcop .netwrote:
              >Tom wrote:
              >>uncomment the last line:
              >>//print("<pre>$ms g</pre>");
              >>Also, unless you have other reasons for writing your own code here, you
              >>might check out something phpmailer. It may help save you from
              >>reinventing the wheel.
              >>TomPlease excuse the ignorance,
              > where would I find phpmailer ?
              >>
              >bill
              >

              Comment

              Working...