HTML email through Mail()

Discussion in 'PHP' started by vassili, Feb 11, 2008.

  1. #1
    i've looked everywhere online, and every guide seems to tell you how to set it up but not really tell you how the execution is nailed down.

    so i have a whole bunch of variables from a form via post. one variable, $order is a large querystring of HTML. it looks like this.

    <table align=\"center\" cellpadding=\"0\" border=\"0\"><tr><td><form action=\"cart.php?action=update\" method=\"post\" id=\"cart\"><table align=\"center\" cellpadding=\"10\" width=\"820\" border=\"0\"><tr><td style=\"border:1px solid #999;\"><a href=\"cart.php?action=delete&id=CSFT03CHI\" class=\"r\">Remove</a></td><td width=\"400\" style=\"border:1px solid #999;\">Men\'s 1/4 Zip Half Sleeve Pullover (albatross) in CHARCOAL/IVORY</td><td style=\"border:1px solid #999;\">$18.00</td><td width=\"50\" style=\"border:1px solid #999;\"><input type=\"text\" name=\"qtyCSFT03CHI\" value=\"11\" size=\"3\" maxlength=\"3\" /></td><td style=\"border:1px solid #999;\">$198.00</td></tr></table><p align=\"right\">Subtotal: <strong>$198.00</strong></p><div align=\"right\"><button type=\"submit\" id=\"mysubmit\">Update cart</button></div><p align=\"right\"><a href=\"memberindex.php\" >Back to Shopping...</a></p>
    </form></td></tr></table>
    
    Code (markup):
    the rest are simple strings to ID the sender. my submit code looks like this.

    <?php
    
    if(isset($_POST['submit'])) { 
    $to = "xxx@gmail.com"; 
     
    $name_field = $_POST['name']; 
    $email_field = $_POST['email']; 
    $message = $_POST['Comment']; 
    $order = $_POST['order']; 
    $subject = "New Order From $name_field";
    $headers = "From: $name_field <order@xxx> \r\n";
      
    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message \n $order"; 
      
    echo "Data has been submitted to $to!"; 
    mail($to, $subject, $body, $headers); 
    } else { 
    echo "error: mail not delivered"; 
    } 
    ?>
    Code (markup):
    how do i format this so everything in $body shows up as html correctly in the email? now i know $order doesn't pass the validation right now, but all that needs to work is for me to see the the order...i wasn't thinking ahead when i added the function that spits out the $order querystring and now it's too late to change it.
     
    vassili, Feb 11, 2008 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,900
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I've tried the DIY approach and had problems getting my emails looking good in all email clients. Outlook, in particular, would break my links. My solution was to use phpMailer

    One hint when writing your html is to throw away classes and explicitly state which font, color, size etc.
     
    sarahk, Feb 11, 2008 IP
  3. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want the message to be displayed as HTML then you could just set the content type in the header.

    <?php
    
    if(isset($_POST['submit'])) { 
    $to = "xxx@gmail.com"; 
     
    $name_field = $_POST['name']; 
    $email_field = $_POST['email']; 
    $message = $_POST['Comment']; 
    $order = $_POST['order']; 
    $subject = "New Order From $name_field";
    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
    $headers .= "From: $name_field <order@xxx>\n";
      
    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message \n $order"; 
      
    echo "Data has been submitted to $to!"; 
    if (mail($to, $subject, $body, $headers)) {
    } else { 
    echo "error: mail not delivered"; 
    } 
    ?>
    
    PHP:
     
    stoli, Feb 12, 2008 IP
  4. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    phpmailer isn't working for me. it cant seem to connect to send the mail. and i know the smtp server is the right one....man i can't catch a break. anything else i can do?
     
    vassili, Feb 12, 2008 IP
  5. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've personally had the best luck with pear mail
     
    imvain2, Feb 12, 2008 IP
  6. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    this works! thanks!
     
    vassili, Feb 14, 2008 IP
  7. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Good to know. You're welcome, glad to be able to help.
     
    stoli, Feb 14, 2008 IP
  8. Tim Rogers

    Tim Rogers Guest

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    For reference for others, it is necessary to set the headers if you want HTML to be rendered in emails.
     
    Tim Rogers, Feb 15, 2008 IP