SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem sending email in HTML

    Hello,
    Here is my script to send emails in text and HTML format, but the problem is that I have no content for HTML email when the email client supports it.
    Can you tell me if you find any errors (or advise) in my code.

    Here's my PHP code:
    PHP Code:
    <?php
       $frontier 
    "-----=" md5(mt_rand());
       
    $to "me@mail.com";
       
    $subject "My Subject";
       
    $text "Hello World";
        
       
    $html_body = <<<EOF
        <html>
          <head>
            <title>Test</title>
          </head>
          <body>
            <div style=\"text-align:center\">
    $text</div>
          </body>
        </html>
    EOF;

        
    $headers "From: <{$_SERVER['SERVER_ADMIN']}>\r\n";
        
    $headers .= "Reply-To: <{$_SERVER['HTTP_HOST']}>\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: multipart/alternative; boundary=\"$frontier\"";
        
        
    // text
        
    $body "--$frontier\r\n";
        
    $body .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
        
    $body .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $body .= $text "\r\n";
        
        
    // html
        
    $body .= "--$frontier\r\n";
        
    $body .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
        
    $body .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $body .= $html_body "\r\n";

        
    $body .= "--$frontier--";

        return 
    mail($to$subject$body$headers);
    Thank you!

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not sure how acceptable divs are in html emails.

    Why don't you start with the simplest thing you can do, and then gradually work your way up to where you want to be.

    PHP Code:
    $to = <your email address here> ;
    $subject "I'm only testing you ...."
    $body="<html><body>Boo!</body></html>";

    mail($to$subject$body); 
    If the problem is not there, then it is somewhere else ...

  3. #3
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Not sure how acceptable divs are in html emails.

    Why don't you start with the simplest thing you can do, and then gradually work your way up to where you want to be.

    PHP Code:
    $to = <your email address here> ;
    $subject "I'm only testing you ...."
    $body="<html><body>Boo!</body></html>";

    mail($to$subject$body); 
    If the problem is not there, then it is somewhere else ...
    Yes this code works, but I do not understand why my code does not work. I find no error in me but the text does not appear in moin email client

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You have 2 choices. Either do what I suggested, gradually add more complexity from that simple example which does work - checking as you go (incremental development) or comment out each line one a time till you find the error (a basic debugging tactic).

    These are some of the simple practices you have to adopt if you want to make personal progress.

  5. #5
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I did, and I noticed that this problem is caused because there was a line before the promière <html>
    I replace this piece code
    PHP Code:
       $html_body = <<<EOF
        <html>
          <head>
            <title>Test</title>
          </head>
          <body>
            <div style="text-align:center">
    $text</div>
          </body>
        </html>
    EOF; 
    by

    PHP Code:
       $html_body = <<<EOF
    <html>
          <head>
            <title>Test</title>
          </head>
          <body>
            <div style="text-align:center">
    $text</div>
          </body>
        </html>
    EOF; 
    It works now

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,050
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by laresistance2 View Post
    Hello,
    Here is my script to send emails in text and HTML format, but the problem is that I have no content for HTML email when the email client supports it.
    Can you tell me if you find any errors (or advise) in my code.

    Here's my PHP code:
    PHP Code:
    <?php
       $frontier 
    "-----=" md5(mt_rand());
       
    $to "me@mail.com";
       
    $subject "My Subject";
       
    $text "Hello World";
        
       
    $html_body = <<<EOF
        <html>
          <head>
            <title>Test</title>
          </head>
          <body>
            <div style=\"text-align:center\">
    $text</div>
          </body>
        </html>
    EOF;

        
    $headers "From: <{$_SERVER['SERVER_ADMIN']}>\r\n";
        
    $headers .= "Reply-To: <{$_SERVER['HTTP_HOST']}>\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: multipart/alternative; boundary=\"$frontier\"";
        
        
    // text
        
    $body "--$frontier\r\n";
        
    $body .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
        
    $body .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $body .= $text "\r\n";
        
        
    // html
        
    $body .= "--$frontier\r\n";
        
    $body .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
        
    $body .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $body .= $html_body "\r\n";

        
    $body .= "--$frontier--";

        return 
    mail($to$subject$body$headers);
    Thank you!
    Correct me if I am wrong, but I believe $body should be part of $headers. From what I recall from doing something similar, I had to have '' for $body, like so:
    PHP Code:
    <?php
       $frontier 
    "-----=" md5(mt_rand());
       
    $to "me@mail.com";
       
    $subject "My Subject";
       
    $text "Hello World";
        
       
    $html_body = <<<EOF
        <html>
          <head>
            <title>Test</title>
          </head>
          <body>
            <div style=\"text-align:center\">
    $text</div>
          </body>
        </html>
    EOF;

        
    $headers "From: <{$_SERVER['SERVER_ADMIN']}>\r\n";
        
    $headers .= "Reply-To: <{$_SERVER['HTTP_HOST']}>\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: multipart/alternative; boundary=\"$frontier\"\r\n";
        
        
    // text
        
    $headers .= "--$frontier\r\n";
        
    $headers .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
        
    $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $headers .= $text "\r\n";
        
        
    // html
        
    $headers .= "--$frontier\r\n";
        
    $headers .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
        
    $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $headers .= $html_body "\r\n";

        
    $headers .= "--$frontier--";

        return 
    mail($to$subject''$headers);
    ?>

  7. #7
    SitePoint Enthusiast laresistance2's Avatar
    Join Date
    May 2011
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Correct me if I am wrong, but I believe $body should be part of $headers. From what I recall from doing something similar, I had to have '' for $body, like so:
    PHP Code:
    <?php
       $frontier 
    "-----=" md5(mt_rand());
       
    $to "me@mail.com";
       
    $subject "My Subject";
       
    $text "Hello World";
        
       
    $html_body = <<<EOF
        <html>
          <head>
            <title>Test</title>
          </head>
          <body>
            <div style=\"text-align:center\">
    $text</div>
          </body>
        </html>
    EOF;

        
    $headers "From: <{$_SERVER['SERVER_ADMIN']}>\r\n";
        
    $headers .= "Reply-To: <{$_SERVER['HTTP_HOST']}>\r\n";
        
    $headers .= "MIME-Version: 1.0\r\n";
        
    $headers .= "Content-Type: multipart/alternative; boundary=\"$frontier\"\r\n";
        
        
    // text
        
    $headers .= "--$frontier\r\n";
        
    $headers .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
        
    $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $headers .= $text "\r\n";
        
        
    // html
        
    $headers .= "--$frontier\r\n";
        
    $headers .= "Content-Type: text/html; charset=\"utf-8\"\r\n";
        
    $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        
    $headers .= $html_body "\r\n";

        
    $headers .= "--$frontier--";

        return 
    mail($to$subject''$headers);
    ?>

    Sorry, I edited my post because it was incorrect.
    Now see my new post with the solution.
    Thank you for your help.


Tags for this Thread

Bookmarks

Posting Permissions

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