Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts

    Issues sending simple HTML mail

    Hi Folks!

    I am trying to send a simple HTML e-mail but it fails. Here is the code:

    PHP Code:
    <?
    $to 
    'arnaud.xxx@xxx.org';
    $subject 'Test email';
    $message '
    <html>
    <head>
      <title>Hello</title>
    </head>
    <body>
      <p>Here some text!</p>
      <table>
        <tr>
          <td>Here some more text!</td>
        </tr>
      </table>
    </body>
    </html>
    '
    ;
    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
    mail($to$subject$message$headers);
    ?>
    The mail goes out correctly but when I receive it in my e-mail client (whatever I use, Outlook, OWA, iPhone) the formatting is not done. I basically receive the HTML code as text...

    This is how my e-mail client shows it (message body):


    -------------------------------------

    Content-type: text/html; charset=iso-8859-1

    Message-Id: <20100120165728.9BC6C2E20E@helpdesk.xxxx.local>
    Date: Wed, 20 Jan 2010 17:57:28 +0100 (CET)
    From: arnaud.xxx@xxx.org (WWW daemon apache)

    <html>
    <head>
    <title>Hello</title>
    </head>
    <body>
    <p>Here some text!</p>
    <table>
    <tr>
    <td>Here some other text</td>
    </tr>
    </table>
    </body>
    </html>

    -------------------------------------

    Any idea what I am doing wrong? Or am I missing something on the PHP server? Thanks in advance. This is not the first time I use PHP generated HTML e-mail but never got to something similar...
    Chuck Norris counted to infinity.
    Twice.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    your code seems correct according to the manual,

    this is a function that I wrote a few years ago that should solve this issue for you:

    PHP Code:
        // Format and Sends HTML & TXT Email at the same time
        
    function SendHTMLTxTEmail($From$FromName$To$ToName$Subject$Text$Html) {
            
    $OB="----=_OuterBoundary_000";
            
    $IB="----=_InnerBoundery_001";
            
    $Html=$Html?str_replace(array("\r","\n","\t"), ""$Html):preg_replace("/\n/","<br>",$Text
            or die(
    "neither text nor html part present.");
            
    $Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
            
    $From or die("sender address missing");
            
    $To or die("recipient address missing");

            
    $headers ="MIME-Version: 1.0\n"
            
    $headers.="From: ".$FromName." <".$From.">\n"
            
    // $headers.="To: ".$ToName." <".$To.">\n"; 
            
    $headers.="Reply-To: ".$FromName." <".$From.">\n"
            
    // $headers.="X-Priority: 1\n"; 
            // $headers.="X-MSMail-Priority: High\n"; 
            // $headers.="X-Mailer: My PHP Mailer\n"; 
            
    $headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";

            
    //Messages start with text/html alternatives in OB
            
    $Msg ="This is a multi-part message in MIME format.\n";
            
    $Msg.="\n--".$OB."\n";
            
    $Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

            
    //plaintext section
            
    $Msg.="\n--".$IB."\n";
            
    $Msg.="Content-Type: text/plain;\n\tcharset=\"Windows-1252\"\n";
            
    $Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
            
    // plaintext goes here
            
    $Msg.=$Text."\n\n";

            
    // html section
            
    $Msg.="\n--".$IB."\n";
            
    $Msg.="Content-Type: text/html;\n\tcharset=\"Windows-1252\"\n";
            
    $Msg.="Content-Transfer-Encoding: base64\n\n";
            
    // html goes here 
            
    $Msg.=chunk_split(base64_encode($Html))."\n\n";

            
    // end of IB
            
    $Msg.="\n--".$IB."--\n";

            
    //message ends
            
    $Msg.="\n--".$OB."--\n";

            
    //if(USE_SMTP){
            //    $this->mymail($To,$Subject,$Msg,$headers);
            //} else {
                
    mail($To,$Subject,$Msg,$headers);
            
    //}
        


  • #3
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Wow, man, that was a quick response, quick implementation, and quick resolution!

    Works fine, thanks a lot. But any idea why I need your function and why basic code does not work?

    Thanks again!
    Arnaud
    Chuck Norris counted to infinity.
    Twice.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    no problem,
    as I said I'm not exactly sure, I would need to do some testing, as your code does look correct and it right out of the php manual. are you by chance running this on a windows server?

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Ok. Don't spend too much time on this. I will try to figure out myself.

    But no, this is not running on a Windows machine. PHP 5 on a standard linux apache 2 server.
    Chuck Norris counted to infinity.
    Twice.

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm ok, well have a look at my function, that may give you some ideas.

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Location
    Geneva, Switzerland
    Posts
    413
    Thanks
    12
    Thanked 29 Times in 29 Posts
    Sure! thanks for your contribution!
    Chuck Norris counted to infinity.
    Twice.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •