附件不是用php发送的吗?

附件不是用php发送的吗?,php,Php,此代码将html文件作为附件发送,工作正常。 但是在下一个代码片段中,当我将附件添加到图像时,它没有被发送。为什么会这样 <?php $file_path = "file.html"; // server path where file is placed $file_path_type = "text/html"; // File Type $file_path_name = "newfilename.html"; // this file name will be used at re

此代码将html文件作为附件发送,工作正常。 但是在下一个代码片段中,当我将附件添加到图像时,它没有被发送。为什么会这样

<?php
$file_path = "file.html"; // server path where file is placed
$file_path_type = "text/html"; // File Type
$file_path_name = "newfilename.html"; // this file name will be used at reciever end 

$from = "xyz@gmail.com"; // E-mail address of sender
$to = "abc@gmail.com"; // E-mail address of reciever
$subject = "Please check the Attachment."; // Subject of email
$message = "This is the message body.&lt;br&gt;&lt;br&gt;Thank You!&lt;br&gt;&lt;a href='http://7tech.co.in'&gt;7tech.co.in Team&lt;/a&gt;"; 

$headers = "From: ".$from; 

$file = fopen($file_path,'rb');
$data = fread($file,filesize($file_path));
fclose($file); 

$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x"; 

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\""; 

$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" .
$message .= "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$message .= "--{$mime_boundary}\n" .
"Content-Type: {$file_path_type};\n" .
" name=\"{$file_path_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$file_path_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";  

if(@mail($to, $subject, $message, $headers)) {
echo "File send!";

} else {
echo 'Failed';
}
?>
咨询

当然,您可以继续使用您的手工邮件构造代码。但是,请不要将额外的调试工作卸载到其他人身上。你的选择,你的问题


如果你稍微聪明一点,使用一个现有的解决方案而不是带帮助代码。

你收到了什么错误?一个问题,虽然可能不相关,但正在走向尾端:<代码> $DATA.=“\\N\N”……/代码>不应该有等号,所以:<代码> $DATA。“\n\n”[…]
@fredrik我收到的消息是失败的!不,那是你打印的。
mail
怎么说?真的没有理由自己编写这些东西。几乎是PHP中邮寄的黄金标准。有易于使用、经过良好测试的库可以为您完成所有这些。请不要告诉我这是不可能的。请告诉我错误。我希望以这种方式学习。我可能稍后使用phpmailer或swift!但是这段代码有什么错误@用户2280276当然有可能。但你可以先自己筛选。你还没有这么做。这可能没有什么帮助,因为它们都太本地化了,就像你的问题一样。
<?php
    $file_path = "screenshot.png"; // server path where file is placed
    $file_path_type = "image/png"; // File Type
    $file_path_name = "screenshot.png"; // this file name will be used at reciever end 

    $from = "xyz@gmail.com"; // E-mail address of sender
    $to = "abc@gmail.com"; // E-mail address of reciever
    $subject = "Please check the Attachment."; // Subject of email
    $message = "This is the message body.&lt;br&gt;&lt;br&gt;Thank You!&lt;br&gt;&lt;a href='http://7tech.co.in'&gt;7tech.co.in Team&lt;/a&gt;"; 

    $headers = "From: ".$from; 

    $file = fopen($file_path,'rb');
    $data = fread($file,filesize($file_path));
    fclose($file); 

    $rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$rand}x"; 

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\""; 

    $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" .
    $message .= "\n\n"; 

    $data = chunk_split(base64_encode($data)); 

    $message .= "--{$mime_boundary}\n" .
    "Content-Type: {$file_path_type};\n" .
    " name=\"{$file_path_name}\"\n" .
    "Content-Disposition: attachment;\n" .
    " filename=\"{$file_path_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data .= "\n\n" .
    "--{$mime_boundary}--\n";  

    if(@mail($to, $subject, $message, $headers)) {
    echo "File send!";

    } else {
    echo 'Failed';
    }
    ?>