Hey guys,
I've got this code together to send emails with a predetermined file attachment. Going to use it to send out resumes, etc, random stuff. I'd like to be able to add addtional attachments to the e-mail, but I'm not sure about the best way to do it. Can anyone give me an example of adding additional attachments? Thanks 🙂
(I know it's a bit a mess at top with the variables, I haven't yet had a chance to clean it up. Just got it working for now.)
<?php
$from="myaddress@mywebsite.com";
$to=$email;
$attachment="file1.doc";
$message=stripslashes($message);
$message = nl2br($message);
$fileatt = $attachment; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/');
$fileatt_name = $attachment; // Filename that will be used for the file as the attachment
$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $text;
$email_to = $to; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$msg_txt="\n\n$message";
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_txt .= $msg_txt;
$email_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" .
$email_txt . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
$email_subject="Copy of email " . $email_subject . "Sent to $email_to";
$ok2 = @mail($email_from, $email_subject, $email_message, $headers);
if($ok) {
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
echo "<p><center><html><font face=\"arial\">Your e-mail has been sent!<br><br>";
echo "<html><font face=\"arial\">Your message:<br>";
echo "subject: $subject <br>";echo "message: $text <br><br>";
echo "<i>Thanks!<br>- Robert</i></font></html></p></center><br><br>";
?>