I have completed writing a job application which sends form data as an html attachment. You can see the truncated (but thorough) code in this thread.
The next step: I need to include a second attachment (a resume) included in the form by the applicant. Here is the relevant html:
<label class="textfield">Attach your resume: </label><input type="file" name="attachedfile" accept="application/pdf, application/msword, text, text/html" class="textfield" size="25">
And here is the PHP that creates an html file using the submitted data, then attaches it to the email:
$attachments[] = Array(
'data' => $data,
'name' => $filename . '.html',
'type' => 'text/html'
);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "MIME-Version: 1.0\n" .
'From: ' . $name . ' <' . $email . '>' . "\r\n" .
"Cc: test@domain.com\n".
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$text . "\n\n";
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ;
}
$message .= "--{$mime_boundary}--\n";
$mail = mail($to, $subject, $message, $headers);
Normally to add attachments, I would use the old stand-by:
foreach($_FILES as $userfile){
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
if (file_exists($tmp_name)){
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
...but that seems only applicable while adding files from the form, so it destroys the new file I've created using form data. Has anyone else tackled this before? Am I missing something fairly obvious?