• Hi,

    I had a support request on my plugin regarding this one not adding attachments to CF7 emails beyond the default two. Could you try the following patch which DRYs out your code a little and supports as many messages as exist for that form?

    --- a/inc/dnd-upload-cf7.php 	2021-05-17 17:14:26.884043789 +0930
    +++ b/inc/dnd-upload-cf7.php	2021-05-17 18:03:19.334104611 +0930
    @@ -226,6 +226,22 @@
     		@rmdir( $dir );
     	}
     
    +	// Get all active mail properties from a contact form 7 object
    +	function dnd_cf7_get_mail_props( $wpcf7 ) {
    +		$mail_props = [];
    +		$mail_prop_name = 'mail';
    +		$mail_prop_n = 2;
    +
    +		while ( $mail_prop = $wpcf7->prop( $mail_prop_name ) ) {
    +			if ( $mail_prop['active'] ) {
    +				$mail_props[$mail_prop_name] = $mail_prop;
    +			}
    +			$mail_prop_name = 'mail_' . $mail_prop_n++;
    +		}
    +
    +		return $mail_props;
    +	}
    +
     	// Hooks before sending the email - ( append links to body email )
     	function dnd_cf7_before_send_mail( $wpcf7 ){
     		global $_mail;
    @@ -257,8 +273,10 @@
     			$links = array();
     
     			// Prop email
    -			$mail = $wpcf7->prop('mail');
    -			$mail_2 = $wpcf7->prop('mail_2');
    +			$mail_props = dnd_cf7_get_mail_props( $wpcf7 );
    +			if ( ! $mail_props ) {
    +				return $wpcf7;
    +			}
     
     			// Default upload path
     			$simple_path = dirname( $upload_path['upload_url'] ); // dirname - remove duplicate form dir (/wpcf-dnd-uploads/wpcf7-dnd-uploads/example.jpg)
    @@ -271,26 +289,18 @@
     						// Get posted_data files
     						$files = $submitted['posted_data'][$field->name];
     
    -						// Links - 1
    -						$mail_links = dnd_cf7_links( $files, $mail['use_html'] );
    -						$mail['body'] = str_replace( "[$field->name]", "\n" . implode( "\n", $mail_links ), $mail['body'] );
    -
    -						// Links - 2
    -						if( $mail_2['active'] ) {
    -							$mail_links_2 = dnd_cf7_links( $files, $mail_2['use_html'] );
    -							$mail_2['body'] = str_replace( "[$field->name]", "\n" . implode( "\n", $mail_links_2 ), $mail_2['body'] );
    +						// Links
    +						foreach ( $mail_props as &$mail_prop ) {
    +							$mail_links = dnd_cf7_links( $files, $mail_prop['use_html'] );
    +							$mail_prop['body'] = str_replace( "[$field->name]", "\n" . implode( "\n", $mail_links ), $mail_prop['body'] );
     						}
    +						unset($mail_prop);
     					}
     				}
     			}
     
    -			// Save the email body
    -			$wpcf7->set_properties( array("mail" => $mail) );
    -
    -			// if mail 2
    -			if( $mail_2['active'] ) {
    -				$wpcf7->set_properties( array("mail_2" => $mail_2) );
    -			}
    +			// Save the email bodies
    +			$wpcf7->set_properties( $mail_props );
     		}
     
     		return $wpcf7;
    @@ -344,17 +354,16 @@
     			return $components;
     		}
     
    -		// Get mail,mail_2 attachment [tags]
    -		$mail = array('mail','mail_2');
    -		$props_mail = array();
    -
    -		foreach( $mail as $single_mail ) {
    -			$props_mail[] = $form->prop( $single_mail );
    +		// Get email attachment [tags]
    +		$mail_props = dnd_cf7_get_mail_props();
    +		if ( ! $mail_props ) {
    +			return $components;
     		}
     
    -		// Get email attachments (mail, mail_2)
    -		$mail = $props_mail[ $_mail ];
    -		if( $mail['active'] && $mail['attachments'] ) {
    +		// Get email attachments
    +		$mail = array_keys( $mail_props );
    +		$mail = $mail_props[ $mail[$_mail] ];
    +		if( $mail['attachments'] ) {
     
     			// Loop fields get mfile only.
     			foreach( $fields as $field ) {
    @@ -1067,4 +1076,4 @@
     		register_setting( 'drag-n-drop-upload-file-cf7', 'drag_n_drop_error_max_file','sanitize_text_field' );
     		register_setting( 'drag-n-drop-upload-file-cf7', 'drag_n_drop_error_min_file','sanitize_text_field' );
     		register_setting( 'drag-n-drop-upload-file-cf7', 'drag_n_drop_disable_btn','sanitize_text_field' );
    -	}
    \ No newline at end of file
    +	}

    Thanks,
    Roy

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Support more than 2 mails?’ is closed to new replies.