Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 函数发送带有附件错误的电子邮件_Php_Html_Email_Mime_Email Attachments - Fatal编程技术网

Php 函数发送带有附件错误的电子邮件

Php 函数发送带有附件错误的电子邮件,php,html,email,mime,email-attachments,Php,Html,Email,Mime,Email Attachments,我正在尝试通过PHP发送带有附件文件的邮件,但有一个错误,当邮件发送到邮箱时,邮件到达时没有任何附件,这里是邮箱的屏幕截图 <?php function mail_file($to, $from, $subject, $body, $file){ $boundary = md5(rand()); $headers = array( 'MIME-Version: 1.0', "From: {$from}", "Content

我正在尝试通过PHP发送带有附件文件的邮件,但有一个错误,当邮件发送到邮箱时,邮件到达时没有任何附件,这里是邮箱的屏幕截图

<?php
function mail_file($to, $from, $subject, $body, $file){

    $boundary = md5(rand());

    $headers = array(
        'MIME-Version: 1.0',
        "From: {$from}",
        "Content Type : multipart/mixed; boundary=\"{$boundary}\""

                    );

$message = array(
"--{$boundary}",
'Content-Type: text/html; charset="utf-8"',
'Content-transfer-Encoding: 7-bit',
'',
chunk_split($body),
"--{$boundary}",
"Content-Type: {file['type']}; name=\"{$file['name']}\"",
"Content-Disposition: attachment; filename=\"{$file['name']}\"",
"Content-Transfer-Encoding: base64",
'',
@chunk_split(base64_encode(file_get_contents($file['path']))),
"--{$boundary}--"
);

@mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers));     

        }

        if($_SERVER['REQUEST_METHOD'] =='POST'){
    $file = array (

    'name' => $_FILES['file']['name'],
    'size' => $_FILES['file']['size'],
    'type' => $_FILES['file']['type'],
    'path' => $_FILES['file']['tmp_name']
    );



    mail_file('fahmy.farahat@gmail.com', 'File from <fahmy.farahat@gmail.com>', 'a file up', $body, $file);
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>send mail</title>
</head>

<body>


<form action="" method="post" enctype="multipart/form-data">
<label>Name</label><br />

<input type="text" name="name" id="name" />
<br /><br />


<input type="file" name="file" id="file"/>
<br />

<input type="submit" value="email file" />

</form>
</body>
</html>


寄信
名称




邮件标题应为
内容类型
,而不是
内容类型

我不确定,可能是什么问题,但是为什么不使用任何邮件库来发送带有附件的电子邮件呢?它将需要更少的代码,并提供附加文件的能力,您将不会面临这样的问题。一个流行的lib是Swift,你说“有一个错误”。。。错误是什么?日志文件是怎么说的?同样在这种情况下,您应该首先将负载的最终组件转储到一个文件中,并检查它们是否真的包含您期望的内容。将这些信息添加到上面的问题中。顺便说一句,为什么在没有文本正文部分的内容时创建多部分?这是否意味着您只能在邮件客户端中查看文件大小?或者您甚至无法从电子邮件中保存文件?文件到达邮件时的大小为0k,但这确实是一个模棱两可的答案。