I am trying to send in PHP an HTML email but it always shows the sourcecode of the email in the email program. But it should render the html email as html and not show the sourcecode as email content.
I send my emails like this:
$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail");
fputs($fd, "To: ".$to2." \n");
fputs($fd, "From: \"Test <test@test.com>\" \n");
fputs($fd, "Subject: ".$subject." \n");
fputs($fd, "X-Mailer: PHP5 \n");
fputs($fd, "Mime-Version: 1.0 \n");
fputs($fd, " \n");
fputs($fd, "--".$mime_boundary."");
fputs($fd, "Content-Type: text/html; charset=\"utf-8\"; boundary=\"".$mime_boundary."\" \n");
fputs($fd, "Content-Transfer-Encoding: quoted-printable \n");
fputs($fd, " \n");
fputs($fd, $sendmail_body." \n");
fputs($fd, "".$mime_boundary."--");
pclose($fd);
The content of the html file looks like this:
<!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" xml:lang="de" lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<style type="text/css">
body { font: normal 12px Verdana, Arial, Helvetica, sans-serif; }
</style>
</head>
<body>
</body>
</html>
It worked now:
$fd = popen(“/var/qmail/bin/sendmail -t”,”w”) or die(“Couldn’t Open
Sendmail”); fputs($fd, “To: “.$to1.” \n”); fputs($fd, “From:
\”Test \” \n”);
fputs($fd, “Subject: “.$subject.” \n”); fputs($fd, “X-Mailer:
PHP5 \n”); fputs($fd, “Mime-Version: 1.0 \n”);
fputs ($fd, “Content-Type: multipart/alternative; boundary=\””.$mime_boundary.”\” \n”); fputs($fd, ” \n”);
fputs($fd, “–“.$mime_boundary.”\n”); fputs($fd, “Content-Type:
text/html; charset=\”utf-8\” \n”); fputs($fd,
“Content-Transfer-Encoding: quoted-printable \n”); fputs($fd, ”
\n”); fputs($fd, $sendmail_body.” \n”); fputs($fd,
“–“.$mime_boundary.”–\n”); pclose($fd);
And the first line of my html file is empty or I add an \n before the html content.
I think you should consider sending multipart since some clients do not support html mails or just prefer plain text:
As long as the sendmail path and params are configured correctly in php.ini this will send the mail via sendmail in multipart/alternative type.