0

I'm using filepond (https://pqina.nl/filepond/) to upload files on my server. I was able to use the php boilerplate (https://github.com/pqina/filepond-server-php) and was able to upload my file with filepond but was wondering how to send it with phpMailer? How to combine this code (Rik's send.php) with phpMailer?

As I noticed this code below puts the file on the server. By the way I do not need to store it I would like only to reach it in the temp folder and send it. After it can be deleted...

<?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);
    }

}
UWS
  • 25
  • 6

2 Answers2

0

This is two separate operations:

  • Fetch a file from a remote location
  • Add it to an email as an attachment

The most straightforward way to do this with 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);
}

Note that PHPMailer explicitly avoids being an HTTP client itself; addAttachment() will not accept remote URLs, only local paths. Use a proper HTTP client class (like guzzle) to fetch your data and store it in a file, then pass the pathname to PHPMailer.

Alternatively you can fetch the remote content as a binary string and pass it to PHPMailer via addStringAttachment(), which avoids writing to disk, though you should only do that for small items that you know will fit in memory.

Synchro
  • 35,538
  • 15
  • 81
  • 104
0

You can define a file processor with define('TRANSFER_PROCESSOR', 'send_phpmailer'); in config.php or in submit.php if you prefer.

function send_phpmailer($files,$meta){
  $mail = new PHPMailer();

  foreach($files as $file){
    $mail->addAttachment($file['tmp_name'], $file['name']);
  }

  // additional PHPMailer config
}
Andy Gee
  • 3,149
  • 2
  • 29
  • 44