以前我应该使用确切的文件名和文件类型,我能够发送电子邮件并能够打开文件。但是,如果我从数据库中获取文件的名称,我可以发送包含正确文件名(和类型)但无法打开文件的文档的电子邮件。
$to = "$email"; //Recipient Email Address
$subject = "$subject"; //Email Subject
$headers = "From: example@example.com\r\nReply-To: example@example.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-" . $random_hash . "\"";
$attachment = chunk_split(base64_encode(file_get_contents('resume/$fileName'))); // Set your file path here
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n" . "Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $content;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n" . "Content-Type: application/zip; name=\"$fileName\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
mail($to, $subject, $message, $headers);
$ fileName是从数据库获取文件名的变量。