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.

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

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面