Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Forminator] - Send File Uploaded As An Attached In Email
<?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 ){
$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
You can’t perform that action at this time.