images and table problem in html email

Discussion in 'PHP' started by dizyn, Aug 7, 2010.

  1. #1
    Hi,

    I am trying to send html email using php.

    the problem is that on gmail and hotmail it gives me white spaces in 1st table.

    here is my code... Please help

    // Example 
    function sendHTMLemail($HTML,$from,$to,$subject)
    {
    // First we have to build our email headers
    // Set out "from" address
    
        $headers = "From: $from\r\n"; 
    
    // Now we specify our MIME version
    
        $headers .= "MIME-Version: 1.0\r\n"; 
    
    // Create a boundary so we know where to look for
    // the start of the data
    
        $boundary = uniqid("HTMLEMAIL"); 
        
    // First we be nice and send a non-html version of our email
        
        $headers .= "Content-Type: multipart/alternative;".
                    "boundary = $boundary\r\n\r\n"; 
    
        $headers .= "This is a MIME encoded message.\r\n\r\n"; 
    
        $headers .= "--$boundary\r\n".
                    "Content-Type: text/plain; charset=ISO-8859-1\r\n".
                    "Content-Transfer-Encoding: base64\r\n\r\n"; 
                    
        $headers .= chunk_split(base64_encode(strip_tags($HTML))); 
    
    // Now we attach the HTML version
    
        $headers .= "--$boundary\r\n".
                    "Content-Type: text/html; charset=ISO-8859-1\r\n".
                    "Content-Transfer-Encoding: base64\r\n\r\n"; 
                    
        $headers .= chunk_split(base64_encode($HTML)); 
    
    // And then send the email ....
    
        mail($to,$subject,"",$headers);
        
    }
    
    $HTML         = "<div style=\"width:746px; margin:0px auto; font-family:Verdana, Geneva, sans-serif;\">
    <table width=\"746\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
      <tr>
        <td>
    	
    	<table width=\"746\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
    	  <tr>
    		<td><img src=\"http://www.dizyn.com/newsletter/images/header.gif\" border=\"0\" /></td>
    	  </tr>
    	  <tr>
    		<td><img src=\"http://www.dizyn.com/newsletter/images/text.gif\" border=\"0\"  /></td>
    	  </tr>
    	  <tr>
    		<td><img src=\"http://www.dizyn.com/newsletter/images/textBottom.gif\" border=\"0\"  /></td>
    	  </tr>
    	</table>
    	</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
          <tr>
            <td align=\"left\"><a href=\"#\"><img src=\"http://www.dizyn.com/newsletter/images/video1.gif\" width=\"152\" height=\"222\" border=\"0\" /></a></td>
            <td align=\"center\"><a href=\"#\"><img src=\"http://www.dizyn.com/newsletter/images/video2.gif\" width=\"152\" height=\"222\" border=\"0\" /></a></td>
            <td align=\"center\"><a href=\"#\"><img src=\"http://www.dizyn.com/newsletter/images/video3.gif\" width=\"152\" height=\"222\" border=\"0\" /></a></td>
            <td align=\"right\"><a href=\"#\"><img src=\"http://www.dizyn.com/newsletter/images/video4.gif\" width=\"152\" height=\"222\" border=\"0\" /></a></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Test</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table>
    </div>";
    $from         = "info@example.com";
    $to           = "example@example.com";
    $subject     = "I'm sending a test HTML email";
    
    sendHTMLemail($HTML,$from,$to,$subject);
    sendHTMLemail($HTML,$from,"dizyn@hotmail.com",$subject);
    sendHTMLemail($HTML,$from,"dizyn1@yahoo.com",$subject);
    
    echo $HTML;exit;
    
    ?>
    PHP:
     
    dizyn, Aug 7, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Looks really nice! someone has done a good job :)

    I'd start by not escaping all those quotes and code it like
    $HTML = "<div style='width:746px; margin:0px auto; font-family:Verdana, Geneva, sans-serif;'>
    <table width='746' border='0' align='center' cellpadding='0' cellspacing='0'>
      <tr>
        <td>
        <table width='746' border='0' cellspacing='0' cellpadding='0'>
          <tr>
            <td><img src='http://www.dizyn.com/newsletter/images/header.gif' border='0' /></td>
          </tr>
          <tr>
            <td><img src='http://www.dizyn.com/newsletter/images/text.gif' border='0'  /></td>
          </tr>
          <tr>
            <td><img src='http://www.dizyn.com/newsletter/images/textBottom.gif' border='0'  /></td>
          </tr>
        </table>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><table width='100%' border='0' cellspacing='0' cellpadding='0'>
          <tr>
            <td align='left'><a href='#'><img src='http://www.dizyn.com/newsletter/images/video1.gif' width='152' height='222' border='0' /></a></td>
            <td align='center'><a href='#'><img src='http://www.dizyn.com/newsletter/images/video2.gif' width='152' height='222' border='0' /></a></td>
            <td align='center'><a href='#'><img src='http://www.dizyn.com/newsletter/images/video3.gif' width='152' height='222' border='0' /></a></td>
            <td align='right'><a href='#'><img src='http://www.dizyn.com/newsletter/images/video4.gif' width='152' height='222' border='0' /></a></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Test</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table>
    </div>";
    PHP:
    It'll be alot more readable

    Then I'd experiment with giving the images their sizes

    but you may just be running into the age old problem of different mail clients displaying differently. You don't have anything too odd in there though so I'm surprised its broken for you.
     
    sarahk, Aug 7, 2010 IP