I’am sending an e-mail via cron job. The attachment works and is added to the e-mail message. However the html part that is referenced with $body2
does not come through.
The code looks like follows:
$body2 = '<!DOCTYPE html><html>...</html>';
$email = '[email protected]';
$attachment = '/path/to/file/file.txt';
$content = file_get_contents($attachment);
$prefix = "part_"; // This is an optional prefix
$boundary = uniqid($prefix, true);
// headers
$headers = implode("rn", [
'From: [email protected]',
'Reply-To: [email protected]',
'X-Mailer: PHP/' . PHP_VERSION,
'MIME-Version: 1.0',
// boundary parameter required, must be enclosed by quotes
'Content-Type: multipart/mixed; boundary="' . $boundary . '"',
'Content-Transfer-Encoding: BINARY',
'This is a MIME encoded message.' // message for restricted transports
]);
// message and attachment
$message = implode("rn", [
"--" . $boundary, // header boundary delimiter line
//'MIME-Version: 1.0',
'Content-Type: text/html; charset="utf8"',
'Content-Transfer-Encoding: 8bit',
//$body2 holds the html part of the e-mail
$body2,
'--' . $boundary, // content boundary delimiter line
'Content-Type: application/octet-stream; name="file.txt"',
'Content-Transfer-Encoding: BINARY',
'Content-Disposition: attachment',
$content,
"--" . $boundary . "--" // closing boundary delimiter line
]);
mail($email, $subject, $message, $headers); // send the email
I’ve spent hours with configuring this. I also tried plain text, with the same result. The text as well as the html won’ t show up in the e-mail message. How do i have to set this up, that the html and the attachment come through? Thanks for any hint.