2
\$\begingroup\$

Based on several different sources, I have compiled the following as my basic HTML email template. Please let me know if I have missed anything important. I am not sure if I am using \n and \r\n correctly.

$semi_rand = uniqid();
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
$mime_boundary_header = chr(34) . $mime_boundary . chr(34);
$boundary = "nextPart";
$headers = "From: ".$from."\n";
$headers .= "To: ". $to ."\n";
$headers .= "CC: ". $CC ." \r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: <". $data['from'] .">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative;\n boundary=" . $mime_boundary_header ;
$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\n--$boundary\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";

$body = "

--$mime_boundary
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

". strip_tags($message) ."

--$mime_boundary
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding:base64

". chunk_split(base64_encode(
  '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'.
  $style.'</head><body>'.$message.'</body></html>'  )) ."

--$mime_boundary--";

mail(null,$subject,$body,$headers,"-f".$email);

Questions:

  1. Will switching base64_encode() to quoted_printable_encode() work or will I also need to convert the string to an 8-bit string somehow?
  2. Should I just remove these extra headers?
  3. Could/should I use \r\n\ at every line break, including the ones in the multiline string?
\$\endgroup\$
0

1 Answer 1

6
\$\begingroup\$

Some notes:

base64

It's not usual to base64_encode HTML. While the way you've done it will work, you run the risk of being marked as spam as spam filters see attempts at obfuscating emails as shady. Consider Quoted-Printable instead.

$boundary?

This section doesn't make sense:

$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\n--$boundary\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";

You are correctly defining and using $mime_boundary_header, these extra headers are unnecessary and will break the email.

New Lines

You've ended 'To' and 'From' with "\n", these should be "\r\n". You're also using a multiline string for $body. This is ok, but make sure your text editor is using CRLF ("\r\n") for new lines.

To

You still need to pass $to into mail() as the email 'To' header and the recipient can technically be different:

mail($to,$subject,$body,$headers,"-f".$email);

Extra Notes (This is stuff that doesn't really matter)

  • The way you generate $semi_rand is acceptable, but you might want to consider using uniqid() instead
  • If your return-path and reply-to values are the same as your from, you don't need to specify them

Edits in Light of New Questions

Base 64 to Quoted Printable

Will switching base64_encode() to quoted_printable_encode() or will I also need to convert the string to an 8-bit string somehow?

The PHP function quoted_printable_encode() will do that for you - quoted printable is designed to produce 7bit output

Make sure you also update the header to Content-Transfer-Encoding:quoted-printable as well

Removing Unnecessary headers

Should I just remove these extra headers?

Yes, they aren't required for anything

New Lines

Could/Should I use \r\n\ at every line break, including the ones in the multiline string?

You don't have to if you're sure your text editor will use CRLFs, however I prefer to explicitly use "\r\n" for my emails, as you have done for the $headers variable, as it tends to be clearer and reduces the risk of the line endings being changed if you / someone else ever resaves the file with a different editor, etc

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.