Hello @robaxxx
Could you please try to add the following snippet in a new MU plugin file (https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins) and test it first in a staging/dev environment?
<?php
/**
* Plugin Name: [Forminator] - Attach upload file to email
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds two new options in Uplaod's Edit Field popup under the Advanced tab. One option is for adding the uploaded file as an attachment. Second option is for deleting the file after upload
* Author: Panos Lyrakis & Prashant Singh @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Frominator_Upload_Email_Attachment' ) ) {
class WPMUDEV_Frominator_Upload_Email_Attachment {
public $attachments = array();
public $attachments_to_delete = array();
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Frominator_Upload_Email_Attachment();
}
return self::$_instance;
}
private function __construct() {
add_filter( 'wp_mail', array( $this, 'filter_email_args' ) );
add_filter( 'forminator_field_upload_general_settings', array( $this, 'add_attachment_settings' ) );
add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'manage_field_data' ), 20, 2 );
add_action( 'forminator_custom_form_mail_after_send_mail', array( $this, 'delete_uploaded_files' ) );
}
public function add_attachment_settings( $settings ) {
$attachment_option = array(
'type' => 'Toggle',
'label' => 'Attach to email',
'name' => 'use_as_attachment'
);
$delete_upload_option = array(
'type' => 'Toggle',
'label' => 'Delete file after upload',
'name' => 'delete_uploaded_file'
);
array_push( $settings, $attachment_option, $delete_upload_option );
return $settings;
}
public function manage_field_data( $data, $form_id ) {
foreach ( $data as $key => $field_data ) {
if ( ! isset( $field_data['name'] ) ) {
continue;
}
$field = Forminator_API::get_form_field( $form_id, $field_data['name'] );
if ( is_wp_error( $field ) ) {
continue;
}
$field_type = Forminator_Field::get_property( 'type', $field );
if ( 'upload' != $field_type ) {
continue;
}
if( !is_array( $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) ){
if ( $file_path = $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) {
if ( Forminator_Field::get_property( 'use_as_attachment', $field ) ) {
$this->attachments[] = $file_path;
}
if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) {
$this->attachments_to_delete[] = $file_path;
}
}
} else {
foreach( $field_data[ 'value' ][ 'file' ][ 'file_path' ] as $fi_k => $fi_val ){
if ( Forminator_Field::get_property( 'use_as_attachment', $field ) ) {
$this->attachments[] = $fi_val;
}
if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) {
$this->attachments_to_delete[] = $fi_val;
}
}
}
}
return $data;
}
public function filter_email_args( $mail_args ) {
if ( ! empty( $this->attachments ) ) {
$mail_args[ 'attachments' ] = $this->attachments;
}
return $mail_args;
}
public function delete_uploaded_files() {
if ( ! empty( $this->attachments_to_delete ) ) {
foreach ( $this->attachments_to_delete as $file_path ) {
unlink( $file_path );
}
}
if ( ! empty( $this->attachments ) ) {
unset( $this->attachments );
}
}
}
if ( ! function_exists( 'wpmudev_forminator_upload_email_attachment' ) ) {
function wpmudev_forminator_upload_email_attachment() {
return WPMUDEV_Frominator_Upload_Email_Attachment::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_upload_email_attachment', 10 );
}
}
Thank you,
Dimitris