user753676
user753676

Reputation:

Trying to send / sending html emails with sendmail but shows sourcecode of email

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 <[email protected]>\" \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.

Upvotes: 1

Views: 2316

Answers (3)

Hikaru-Shindo
Hikaru-Shindo

Reputation: 1901

I think you should consider sending multipart since some clients do not support html mails or just prefer plain text:

$headers = "From: Example <[email protected]>\r\n
    MIME-Version: 1.0\r\n
    Content-Type: multipart/alternative; boundary={$mime_boundary}\r\n
    X-Mailer: PHP5";

$message = "This is a MIME-Message. If you can read this your client does not support the MIME format.\r\n
\r\n
{$mime_boundary}\r\n
Content-Transfer-Encoding: quoted-printable\r\n
Content-Type: text/plain; charset=utf8;\r\n
\r\n
Text Content encoded in quoted printable
\r\n
\r\n
{$mime_boundary}\r\n
Content-Transfer-Encoding: quoted-printable\r\n
Content-Type: text/html;charset=utf8;\r\n
\r\n
HTML Content encoded in quoted printable
\r\n
--{$mime_boundary}";

mail($to, $subject, $message, $headers);

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.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

This worked for me:

<?php
$message=<<<EOL
--frontier
Content-type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
<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>
--frontier--
EOL;

$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail");

fputs($fd, "To: ".$to." \n");
fputs($fd, "From: \"Example\" <[email protected]> \n");
fputs($fd, "Subject: ".$subject." \n");

fputs($fd,"MIME-Version: 1.0\n");
fputs($fd,"Content-type: multipart/alternative; boundary=\"frontier\"\n\n");
fputs($fd,"This is a message with multiple parts in MIME format.\n");

fputs($fd, $message);
pclose($fd);
?>

I hope it will be helpful

Upvotes: 0

belgther
belgther

Reputation: 2534

fputs($fd, "X-Mailer: PHP5 \n\n"); 

Try removing the second \n because it is a sign of header termination.

Upvotes: 0

Related Questions