Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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 如何调试(看似)有故障的SwiftMailer附件?_Php_Email_Debugging_Swiftmailer - Fatal编程技术网

Php 如何调试(看似)有故障的SwiftMailer附件?

Php 如何调试(看似)有故障的SwiftMailer附件?,php,email,debugging,swiftmailer,Php,Email,Debugging,Swiftmailer,我在试图弄清楚我的应用程序到底出了什么问题以及哪里出了问题时遇到了一点问题 我试图通过表单字段上传一个文件。然后对文件进行base64编码并将其附加到邮件 症状: 附件显示在邮件中,但尝试打开它会导致“文件不是.pdf或已损坏”错误,并且无法读取 其他信息: 附件含义如邮件中所示(使用pdf作为示例,它正确显示png和其他文件的文件类型): 编码方法: public function encode_attachments($files) // $files = $_FILES { $at

我在试图弄清楚我的应用程序到底出了什么问题以及哪里出了问题时遇到了一点问题

我试图通过表单
字段上传一个文件。然后对文件进行base64编码并将其附加到邮件

症状:

附件显示在邮件中,但尝试打开它会导致“文件不是.pdf或已损坏”错误,并且无法读取

其他信息:

附件含义如邮件中所示(使用pdf作为示例,它正确显示png和其他文件的文件类型):

编码方法:

public function encode_attachments($files) // $files = $_FILES
{
    $attachments = array();
    $legal_files = array('input', 'ids');
    $msg = array(
        1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
        2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
        3 => "The uploaded file was only partially uploaded",
        4 => "No file was uploaded",
        6 => "Missing a temporary folder"
    );

    foreach ($legal_files as $file) {
        if (isset($files[$file]) && !empty($files[$file]['tmp_name'])) {
            // get file details we need
            $file_tmp_name = $files[$file]['tmp_name'];
            $file_name = $files[$file]['name'];
            $file_size = $files[$file]['size'];
            $file_error = $files[$file]['error'];

            if ($file_error > 0) {
                throw new Exception($msg[$file_error]);
            } else {
                // 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));
                // now we know we have the file contents for attachment
                $attachments[$file_name] = $encoded_content;
            }
        }
    }

    return $attachments;
}
附件在邮件中的构造方式(初始部分):

我尝试过的一些事情:

最令人困惑的是,编码文件内容上的md5散列在发送的内容和接收的内容之间是不同的

我真的找不到一个文件内容可能格式错误或损坏的地方。这可能是我试图通过SSL发送它们的事实吗?浏览SwiftMailer代码不会显示任何特定内容,它只会在文件内容中做一些事情,如果它是
Swift\u OutputByteStream
(但它是一个字符串)的实例

常规文本文件打开没有问题,但是它们的内容是Base64编码的(这引起了我的怀疑,文件在某个地方被编码了两次,但我在任何地方都找不到)。图像和pdf文件根本不会打开

如果有人给我指出了正确的方向,我会非常感激(我想这完全是平庸的事情,我总是把大部分时间浪费在平庸的事情上)。我将根据需要提供任何其他信息

编辑:


解决方案相当简单(正如Marc B所指出的)。我所要做的就是停止使用以前的维护人员在项目中放在SwiftMailer周围的多余包装,恢复直接调用它的类。

是的。你做错了。没有需要自己进行base64编码等等。从字面上说,您所需要的是(分成多行以便于阅读):


这就是Swiftmailer和PHPMailer的全部要点——他们做所有繁重的工作,你就像老板一样:“去那里,做这个,让我来承担所有的功劳。”

我感谢Marc B。我知道这必须是一件简单的事情,而且我正在做的项目在Swiftmailer周围有一些额外的包装,这是完全不必要的(并不是说我自己试图对文件进行编码有任何帮助)。
public function encode_attachments($files) // $files = $_FILES
{
    $attachments = array();
    $legal_files = array('input', 'ids');
    $msg = array(
        1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
        2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
        3 => "The uploaded file was only partially uploaded",
        4 => "No file was uploaded",
        6 => "Missing a temporary folder"
    );

    foreach ($legal_files as $file) {
        if (isset($files[$file]) && !empty($files[$file]['tmp_name'])) {
            // get file details we need
            $file_tmp_name = $files[$file]['tmp_name'];
            $file_name = $files[$file]['name'];
            $file_size = $files[$file]['size'];
            $file_error = $files[$file]['error'];

            if ($file_error > 0) {
                throw new Exception($msg[$file_error]);
            } else {
                // 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));
                // now we know we have the file contents for attachment
                $attachments[$file_name] = $encoded_content;
            }
        }
    }

    return $attachments;
}
    if (isset($this->attachments) && is_array($this->attachments) && !empty($this->attachments)) {
        foreach ($this->attachments as $file_name => $file_content) {
            $mime_type = File::mime_by_ext(pathinfo($file_name, PATHINFO_EXTENSION));
            $attachment = Swift_Attachment::newInstance($file_content, $file_name, $mime_type ? $mime_type : "text/html");

            // Attach it to the message
            $message->attach($attachment);
        }
    }
$swift
  ->attach(Swift_Attachment::fromPath($_FILES['foo']['tmp_name'])
  ->setFilename('Name you want to appear in the email.pdf');