=======UPDATE========
The problem ended up being that I was using the header ContentType when I should have been using Content-Type.
Thanks for your help guys.
Derp derp derp.
=======/UPDATE=======
I apologize in advance for the obnoxiously long question.
I’m attempting to send emails with attachments through PHP. I’ve done this successfully before with a single attachment. But I can’t get it to work with multiple attachments, or for that matter now with a single attachment using this method. So, what am I doing wrong?
I understand that there are other libraries for these functions available. But I want to know WHY this one is not working.
Here is the method that generates the actual email:
function gen_mail ()
{
$files = $this->files();
$has_attachments = (bool) $files;
$EOL = "\r\n"; // tried \n, PHP_EOL
$message = $this->message;
$headers = $this->gen_headers();
$headers .= "MIME-Version: 1.0$EOL";
if ($has_attachments) {
$hash = md5(time());
$sep = "=={$hash}==";
$headers .= "ContentType: multipart/mixed; boundary=\"$sep\"$EOL$EOL";
$headers .= "This is a MIME-encoded message.$EOL$EOL";
$headers .= "--$sep$EOL";
}
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"$EOL";
$headers .= "Content-Disposition: inline$EOL";
$headers .= "Content-Transfer-Encoding: 7bit$EOL$EOL";
$headers .= "$message$EOL$EOL";
if ($has_attachments) { // here go attachments
foreach ($files as $key => $value) {
$filename = is_numeric($key)? basename($value) : $key;
$file = fopen($value, 'rb');
$data = fread($file, filesize($value));
fclose($file);
$content = chunk_split(base64_encode($data));
$headers .= "--$sep$EOL";
$headers .= "Content-Type: application/octet-stream; ";
$headers .= "name=\"$filename\"$EOL";
$headers .= "Content-Transfer-Encoding: base64$EOL";
// tried Content-Disposition, attachment, and every combination
$headers .= "Content-Description: inline; $EOL ";
$headers .= "filename=\"$filename\"$EOL$EOL";
$headers .= "$content$EOL$EOL";
}
$headers .= "--$sep--$EOL$EOL";
}
return $headers;
}
And this is what the headers returned by the above function look like:
From: test@urbido.es
MIME-Version: 1.0
ContentType: multipart/mixed; boundary="==cd42448edf10bca4640bc7d6365e1961=="
This is a MIME-encoded message.
--==cd42448edf10bca4640bc7d6365e1961==
Content-Type: text/html; charset="iso-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Travis, you are so cool!!!1!
--==cd42448edf10bca4640bc7d6365e1961==
Content-Type: application/octet-stream; name="html.login.php"
Content-Transfer-Encoding: base64
Content-Description: inline;
filename="html.login.php"
PD9waHANCgkNCiRoZWFkZXJfY29uZmlnID0gYXJyYXkoKTsNCiRtc2cgPSAnUmVnaXN0ZXJlZCBP
cGVyYXRvcnMsIGVudGVyIHlvdXIgT3BlcmF0b3IgSUQgYW5kIHBhc3N3b3JkIHRvIGxvZ2luJzsN
CiRtc2dfY2xhc3MgPSAnbm9ybWFsJzsNCmlmICgkbG9nZ2VkX2luKSB7DQoJJGhlYWRlcl9jb25m
aWdbJ2NvbnRyb2xzJ10gPSBhcnJheSAoDQoJCWFycmF5ICgndGV4dCcgPT4gJ1N1Ym1pdCBFbnRy
aWVzJywgJ2hyZWYnID0+ICdlbnRyeScpLA0KCSk7DQoJJG1zZyA9ICdZb3UgYXJlIGxvZ2dlZCBp
bi4gVXNlIHRoZSBhYm92ZSBsaW5rcywgb3IgDQoJCWNsaWNrIDxhIGhyZWY9ImVudHJ5Ij5oZXJl
PC9hPiB0byBiZWdpbiBvciByZXN1bWUgYSBqb2InOw0KCSRtc2dfY2xhc3MgPSAnc3VjY2Vzcyc7
DQp9DQoNCj8+
--==cd42448edf10bca4640bc7d6365e1961==--
I’ve tried functions written by other people, and they produce seemingly identical output. But when I send those headers with
mail($address, $subject, ”, $headers)
I get this in my inbox:
This is a MIME-encoded message.
--==cd42448edf10bca4640bc7d6365e1961==
Content-Type: text/html; charset="iso-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Travis, you are so cool!!!1!
--==cd42448edf10bca4640bc7d6365e1961==
Content-Type: application/octet-stream; name="html.login.php"
Content-Transfer-Encoding: base64
Content-Description: inline;
filename="html.login.php"
PD9waHANCgkNCiRoZWFkZXJfY29uZmlnID0gYXJyYXkoKTsNCiRtc2cgPSAnUmVnaXN0ZXJlZCBP
cGVyYXRvcnMsIGVudGVyIHlvdXIgT3BlcmF0b3IgSUQgYW5kIHBhc3N3b3JkIHRvIGxvZ2luJzsN
CiRtc2dfY2xhc3MgPSAnbm9ybWFsJzsNCmlmICgkbG9nZ2VkX2luKSB7DQoJJGhlYWRlcl9jb25m
aWdbJ2NvbnRyb2xzJ10gPSBhcnJheSAoDQoJCWFycmF5ICgndGV4dCcgPT4gJ1N1Ym1pdCBFbnRy
aWVzJywgJ2hyZWYnID0+ICdlbnRyeScpLA0KCSk7DQoJJG1zZyA9ICdZb3UgYXJlIGxvZ2dlZCBp
bi4gVXNlIHRoZSBhYm92ZSBsaW5rcywgb3IgDQoJCWNsaWNrIDxhIGhyZWY9ImVudHJ5Ij5oZXJl
PC9hPiB0byBiZWdpbiBvciByZXN1bWUgYSBqb2InOw0KCSRtc2dfY2xhc3MgPSAnc3VjY2Vzcyc7
DQp9DQoNCj8+
--==cd42448edf10bca4640bc7d6365e1961==--
You’ve got the wrong header. It’s “Content-Disposition”, not “Content-Description”. Try:
instead. Content-Description is to add a short DESCRIPTION of what the attachment is, e.g. “This is my resume” and has nothing to do at all with telling the mail client how to deal with the attachment.