我正在尝试使用 php 的邮件功能发送带有 pdf 附件的邮件。我在邮件中收到其他东西,但附件大小为零。任何人都可以查看我的代码并建议这里有什么问题吗?
谢谢!下面是代码:
public static function sendMailWithAttachment($to, $from, $from_name, $subject, $msg,$attachmentName, $attachmentPath){
// Setting a timezone, mail() uses this.
//date_default_timezone_set('America/New_York');
$semi_rand = md5(time());
$data = chunk_split(base64_encode($data));
$fileatt_type = "application/pdf"; // File Type
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// set header ........................
$headers = "From: ".$from;
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// set email message......................
//$email_message = "Thanks for visiting ";
//$email_message .= "Thanks for visiting.<br>";// Message that the email has in it
$email_message=$msg;
$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_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$attachmentPath}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$attachmentName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$sent = @mail($to, $subject, $email_message, $headers);
if($sent) {
echo "Your email attachment send successfully.";
} else {
die("Sorry but the email could not be sent. Please try again!");
}
}