我正在尝试使用PHP通过电子邮件发送附件。我一直关注一些在线教程页面,我的mail()
函数返回true。但是,电子邮件没有发送。我一直在检查我的标题和身体,无法确定我做错了什么。
$from_email = 'admin@felixxiao.com'; //sender email
$recipient_email = 'admin@felixxiao.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body
//get file details we need
$file_tmp_name = $_FILES['data']['tmp_name'];
$file_name = $_FILES['data']['name'];
$file_size = $_FILES['data']['size'];
$file_type = $_FILES['data']['type'];
$file_error = $_FILES['data']['error'];
$user_email = filter_var("admin@felixxiao.com", FILTER_SANITIZE_EMAIL);
if($file_error>0)
{
echo 'upload error';
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: ".$from_email."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
echo $headers;
//plain text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: ".$file_type."; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
echo $body;
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
echo 'Thank you for your email';
}else{
echo 'Could not send mail! Please check your PHP mail configuration.';
}
当我打印出$headers
和$body
时,这就是打印出来的内容:
MIME-Version: 1.0
From: admin@felixxiao.com
Reply-To: admin@felixxiao.com
Content-Type: multipart/mixed; boundary = 8de2a431c506316063ec3a4044192e46
--8de2a431c506316063ec3a4044192e46
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: base64
VGhpcyBpcyBib2R5IG9mIHRoZSBtZXNzYWdl
--8de2a431c506316063ec3a4044192e46
Content-Type: application/pdf; name=blob
Content-Disposition: attachment; filename=blob
Content-Transfer-Encoding: base64
X-Attachment-Id: 32091
Thank you for your email