Last active
June 10, 2023 22:47
-
-
Save wpmudev-sls/7a32dc5407324cc902f8b9ad8970ef62 to your computer and use it in GitHub Desktop.
[Forminator] - Send File Uploaded As An Attached In Email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator] - Send File Uploaded As An Attached In Email | |
* Description: [Forminator] - Send File Uploaded As An Attached In Email | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_forminator_send_file_uploaded_as_an_attached_in_email_func', 100 ); | |
function wpmudev_forminator_send_file_uploaded_as_an_attached_in_email_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_Forminator_Send_Uploaded_As_Attachment{ | |
private $delete_file_after_sent = true; | |
private $files = []; | |
public function __construct(){ | |
add_action( 'forminator_custom_form_mail_before_send_mail', array( $this, 'retrive_uploaded_file_from_data' ), 10, 4 ); | |
} | |
public function retrive_uploaded_file_from_data( $front_mail, $custom_form, $data, $entry ){ | |
$form_ids = array( 2910, 2911 ); //Add your comma-separated form IDs here | |
if ( ! in_array( $custom_form->id, $form_ids ) ) { | |
return; | |
} | |
$upload_fields = $custom_form->get_fields_by_type('upload'); | |
if( $upload_fields ){ | |
foreach( $upload_fields as $field ){ | |
$file = $entry->get_meta($field->slug); | |
if( ! empty( $file['file'] ) ){ | |
$this->files[] = $file['file']['file_path']; | |
} | |
} | |
add_filter( 'wp_mail', array( $this, 'add_attachment_custom_form' ) ); | |
add_filter( 'forminator_entry_ignored_fields', array( $this, 'ignore_upload_fields' ) ); | |
add_action( 'forminator_custom_form_mail_admin_sent', array( $this, 'after_sent_email') ); | |
// for user use hook forminator_custom_form_mail_user_sent see front-mail.php | |
} | |
} | |
public function add_attachment_custom_form( $atts ){ | |
if( $this->files ){ | |
$atts['attachments'] = $this->files; | |
} | |
return $atts; | |
} | |
public function after_sent_email(){ | |
if( $this->files ){ | |
if( $this->delete_file_after_sent ){ | |
foreach( $this->files as $path ){ | |
// remove file from the upload directory | |
@unlink( $path ); | |
} | |
} | |
$this->files = []; | |
} | |
} | |
public function ignore_upload_fields( $ignore_field_types ){ | |
$ignore_field_types[] = 'upload'; | |
return $ignore_field_types; | |
} | |
} | |
$run = new WPMUDEV_Forminator_Send_Uploaded_As_Attachment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment