dongyashun2559 2016-07-09 22:48
浏览 26
已采纳

带附件的PHP邮件返回True,不发送[重复]

This question already has an answer here:

I am attempting to send an attachment via email using PHP. I have been following some online tutorial pages and my mail() function is returning true. However, the email is not sending. I have been examining my headers and body and can't pinpoint what I am doing wrong.

$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
"; 
    $headers .= "From: ".$from_email."
"; 
    $headers .= "Reply-To: ".$user_email."" . "
";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary

"; 

    echo $headers;
    //plain text 
    $body = "--$boundary
";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1
";
    $body .= "Content-Transfer-Encoding: base64

"; 
    $body .= chunk_split(base64_encode($message)); 

    //attachment
    $body .= "--$boundary
";
    $body .="Content-Type: ".$file_type."; name=".$file_name."
";
    $body .="Content-Disposition: attachment; filename=".$file_name."
";
    $body .="Content-Transfer-Encoding: base64
";
    $body .="X-Attachment-Id: ".rand(1000,99999)."

"; 

    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.';  
}

When I print out my $headers and $body, this is what prints out:

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
</div>
  • 写回答

1条回答 默认 最新

  • doumeng3188 2016-07-09 23:05
    关注

    The @ before the @mail call suppresses the error (see this). (It sets the error to 0). Remove the @ and try again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值