【问题标题】:How can I send the uploaded file (by filepond) with phpMailer?如何使用 phpMailer 发送上传的文件(通过文件池)?
【发布时间】:2021-09-15 10:26:54
【问题描述】:

我正在使用 filepond (https://pqina.nl/filepond/) 在我的服务器上上传文件。 我能够使用 php 样板 (https://github.com/pqina/filepond-server-php) 并且能够使用 filepond 上传我的文件,但想知道如何 用phpMailer 发送它?如何将此代码(Rik 的 send.php)与 phpMailer 结合起来?

我注意到下面的这段代码将文件放在服务器上。顺便说一句,我不需要存储它,我只想在临时文件夹中找到它并发送它。之后就可以删除了……

<?php

// Comment if you don't want to allow posts from other domains
header('Access-Control-Allow-Origin: *');

// Allow the following methods to access this file
header('Access-Control-Allow-Methods: POST');

// Load the FilePond class
require_once('FilePond.class.php');

// Load our configuration for this server
require_once('config.php');

// Catch server exceptions and auto jump to 500 response code if caught
FilePond\catch_server_exceptions();
FilePond\route_form_post(ENTRY_FIELD, [
    'FILE_OBJECTS' => 'handle_file_post',
    'BASE64_ENCODED_FILE_OBJECTS' => 'handle_base64_encoded_file_post',
    'TRANSFER_IDS' => 'handle_transfer_ids_post'
]);

function handle_file_post($files) {

    // This is a very basic implementation of a classic PHP upload function, please properly
    // validate all submitted files before saving to disk or database, more information here
    // http://php.net/manual/en/features.file-upload.php

    foreach($files as $file) {
        FilePond\move_file($file, UPLOAD_DIR);
    }
}

function handle_base64_encoded_file_post($files) {

    foreach ($files as $file) {

        // Suppress error messages, we'll assume these file objects are valid
        /* Expected format:
        {
            "id": "iuhv2cpsu",
            "name": "picture.jpg",
            "type": "image/jpeg",
            "size": 20636,
            "metadata" : {...}
            "data": "/9j/4AAQSkZJRgABAQEASABIAA..."
        }
        */
        $file = @json_decode($file);

        // Skip files that failed to decode
        if (!is_object($file)) continue;

        // write file to disk
        FilePond\write_file(
            UPLOAD_DIR,
            base64_decode($file->data),
            FilePond\sanitize_filename($file->name)
        );
    }

}

function handle_transfer_ids_post($ids) {

    foreach ($ids as $id) {

        // create transfer wrapper around upload
        $transfer = FilePond\get_transfer(TRANSFER_DIR, $id);

        // transfer not found
        if (!$transfer) continue;

        // move files
        $files = $transfer->getFiles(defined('TRANSFER_PROCESSOR') ? TRANSFER_PROCESSOR : null);
        foreach($files as $file) {
            FilePond\move_file($file, UPLOAD_DIR);
        }

        // remove transfer directory
        FilePond\remove_transfer_directory(TRANSFER_DIR, $id);
    }

}

【问题讨论】:

    标签: php phpmailer filepond


    【解决方案1】:

    这是两个独立的操作:

    • 从远程位置获取文件
    • 将其作为附件添加到电子邮件中

    使用 PHPMailer 最直接的方法:

    $path = tempnam(sys_get_temp_dir(), 'emailfile');
    if (file_put_contents($path, file_get_contents('https://example.com/path/to/file.png'))) {
        $mail->addAttachment($path);
    }
    

    请注意,PHPMailer 明确避免成为 HTTP 客户端本身; addAttachment() 不接受远程 URL,只接受本地路径。使用适当的 HTTP 客户端类(如 guzzle)获取数据并将其存储在文件中,然后将路径名传递给 PHPMailer。

    或者,您可以将远程内容作为二进制字符串获取并通过addStringAttachment() 将其传递给 PHPMailer,这样可以避免写入磁盘,但您应该只对您知道适合内存的小项目执行此操作。

    【讨论】:

      【解决方案2】:

      如果您愿意,可以在 config.php 或 submit.php 中使用 define('TRANSFER_PROCESSOR', 'send_phpmailer'); 定义文件处理器。

      function send_phpmailer($files,$meta){
        $mail = new PHPMailer();
      
        foreach($files as $file){
          $mail->addAttachment($file['tmp_name'], $file['name']);
        }
      
        // additional PHPMailer config
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 2017-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多