I am attempting to send out HMTL emails with php's mail()
function and notice that some things are broken in the email. I found out from reading the original in gmail, that lines in the body of the message are all the same length, and this cuts HTML tags in inconvenient places.
My headers are multipart with boundary between html and plain, with 7-bit. An example of what I'm sending is:
<a href="site.com/unsub">Unsubscribe</a>
But because of its position, the message splits it like
<a href="site.co
m/ubsub">unsubscribe</a>
Which breaks the hyperlink. Is there something I'm missing or not getting? I've seen around about using a mail library like swiftmailer--and I can use it--but I want to actually understand this behavior.
EDIT:
How mail()
is being called:
$headers = 'From: Me <me@site.com>' . "
" . 'MIME-Version: 1.0'
$headers .= "
" . "Content-Type: multipart/mixed; charset=ISO-8859-1; boundary=$boundary"
$subject = 'subject';
$html_message = '--' . $boundary . "
";
$html_message .= 'Content-Type: text/html; charset=ISO-8859-1' . "
";
$html_message .= 'Content-Transfer-Encoding: 7bit' . "
" . "
";
$html_message .= $message . "
";
// plaintext added similarly, with different content-type. Omitted.
mail($to,$subject,$html_message, $headers);
EDIT #2:
Expanding on $message
.
$message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" /><title>"
$message .= "Page Title</title></head><body style=\"margin: 0; padding: 0; font-family: open \'Open Sans\', \'Open Sans Bold\';\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" ><tr><td><table align=\"center\" bgcolor=\"#FFFFFF\" border=\"0\"cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tr><td align=\"right\" style=\"padding: 20px 20px 20px 20px;\"> <font color=\"#EA5D0F\">[</font> <a href=\"http://SITE.com\" style=\"color:#000001; text-decoration: none !important;\">Sign In </a><font color=\"#EA5D0F\">][</font> <a href=\"http://about.SITE.com\" style=\"color:#000001; text-decoration: none !important;\">About </a><font color=\"#EA5D0F\">][</font> <a href=\"http:/SITE.com/search\" style=\"color:#000001; text-decoration: none !important;\">Search</a><font color=\"#EA5D0F\">]</font></td></tr><tr><td bgcolor=\"#FFFFFF\" align=\"center\" style=\"padding: 40px 0px 0px 0px;\"><img src=\"http://about.SITE.com/wp-content/uploads/2013/08/SITE.plain_.png\" alt=\"SITE Logo\" width=\"300\" style=\"display: block;\" /></td></tr><tr><td style=\"padding: 10px 20px 20px 20px;\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" ><tr><td bgcolor=\"#FFFFFF\" style=\"padding: 30px 10px 10px 10px; text-align:center; font-size: 1.2em; font-weight:bold;\"> <hr color=\"#EA5D0F\"/>"
This continues for quite a while. Not sure how much to include.
I did have that problem in the past. While i don't know what the cause is, it was solved by replacing every >
by >
:
$message = str_replace(">", ">
", $message);