Search code examples
phpcssemailstyles

php mail() not sending styles


I am trying to send style information along with an email via PHP mail() function. Unfortunately, even though my mail client is set to accept styled emails it still just renders as plain html text.

$to = $email;
    $subject = "Subject Details";
    $message='<html><body><table width="600" height="840" border="0" cellpadding="5" cellspacing="5">';
    $message.='<tr><td height="110"><img src="https://user.co.uk/images/logo.jpg" alt="SignDox" width="300" height="100" /></td></tr>';
    $message.='<tr><td height="29" bgcolor="#5C1561">&nbsp;</td></tr>';
    $message.='<tr><td height="537" valign="top">';
    $message.="Message Goes Here\n\n";
    $message.="Username: ".$new_agent_id."\n";
    $message.="Password: ".$pass1."\n\n";
    $message.="To sign in to your user panel follow this link: \n";
    $message.="Inside your admin section you will be able to change your username and password.\n";
    $message.='</td></tr>';
    $message.='<tr><td height="140" bgcolor="#5C1561">&nbsp;</td></tr></table></body></html>';
    $from = "no-reply@sender.co.uk";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);

Solution

  • You need to define the correct headers in order to send HTML formatted emails. This can be done very simply using:

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    

    However, I would urge you to use the PHPMailer Class. This class will handle all headers and anything else you may need. You can easily add attachments, embed images, send via SMTP etc...

    It is a fantastic class, and not to mention the amount you will learn with objects and classes, expecially if you are a newbie to PHP :-)

    See here for the PHPMailer Class