PHP 带附件的邮件返回 True,未发送

PHP Mail with Attachment Returning True, Not Sending

我正在尝试使用 PHP 通过电子邮件发送附件。我一直在关注一些在线教程页面,我的 mail() 函数正在返回 true。但是,电子邮件未发送。我一直在检查我的 headers 和 body,但无法确定我做错了什么。

$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

@mail 调用前的 @ 可抑制错误(请参阅 this)。 (它将错误设置为 0)。删除@并重试。