Support » Plugin: cformsII » programmatically change admin email address or add cc

  • Resolved Robert

    (@rsimpson)


    Hi there,

    I’ve got a generic form set up that I’ll be using for multiple possible recipients (the recipient email addresses are held in post meta). I’ve been trawling through the plugin’s code trying to find a way to programmatically (and temporarily) replace the admin email address for a given form upon submission. I’ve used the action cforms2_after_processing_action for additional functionality before, but that doesn’t appear to be the way to achieve what I’m trying to do.

    Any ideas?

    Cheers,
    Robert

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author bgermann

    (@bgermann)

    Use cforms2_admin_email_filter and check its $mail->to.

    • This reply was modified 7 months, 3 weeks ago by bgermann.
    Plugin Author bgermann

    (@bgermann)

    Also use its $pid argument to get the post that you are coming from if you have AJAX enabled.

    I’ve tried using the filter to alter the address but it’s throwing an error. Here’s my code followed by the error.

    add_filter( 'cforms2_admin_email_filter', 'change_cforms_admin_email' );
    function change_cforms_admin_email( $mail ) {
    	$mail->to[0][0] = 'new@email.address'; // this isn't the address in my code
    }

    Fatal error: Call to a member function send() on null in /wordress-content-path/plugins/cforms2/lib_validate.php on line 712

    I’ve just edited to to the following:

    add_filter( 'cforms2_admin_email_filter', 'change_cforms_admin_email' );
    function change_cforms_admin_email( $mail, $no, $pid ) {
    	$mail->to[0][0] = 'new@email.address'; // this isn't the address in my code
    	return $mail;
    }

    The errors are now as follows:

    Warning: Missing argument 2 for change_cforms_admin_email() in /theme-path/functions/cforms.php on line 11
    
    Warning: Missing argument 3 for change_cforms_admin_email() in /theme-path/functions/cforms.php on line 11

    It seems that the apply_filters in lib_validate.php on line 712 aren’t passing $no and $pid.

    Any ideas?

    • This reply was modified 7 months, 3 weeks ago by Robert.

    Got it working 🙂

    add_filter( 'cforms2_admin_email_filter', 'change_cforms_admin_email', 10, 3 );
    function change_cforms_admin_email( $mail, $no, $pid ) {
    	$mail->to[0][0] = 'new@email.address'; // this isn't the address in my code
    	return $mail;
    }

    I’m trying to add CC and BCC addresses but the methods are private. Any suggestions? This is throwing the expected error:

    add_filter( 'cforms2_admin_email_filter', 'change_cforms_admin_email', 10, 3 );
    function change_cforms_admin_email( $mail, $no, $pid ) {
    	$mail->to[0][0] = 'new@email.address'; // this isn't the address in my code
    	$mail->add_bcc( 'another@email.address' ); // this isn't the address in my code
    	return $mail;
    }
    Plugin Author bgermann

    (@bgermann)

    The PHP manual can be a real pain, but using what you gave me as a starting point I was able to find what I needed to get it working 🙂

    For anyone who needs to achieve something similar, here’s what I came up with (the branch part is specific to my needs, but this should be enough to get you going):

    add_filter( 'cforms2_admin_email_filter', 'change_cforms_admin_email', 10, 3 );
    function change_cforms_admin_email( $mail, $no, $pid ) {
    	if( strlen( $no ) < 1 ) { // if this is the default form
    		$branch = get_branch( $pid ); // get the post data for the current branch (custom function)
    		$branch = $branch[0];
    		if( isset( $branch['email'] ) ) {
    			$original_admin_email = $mail->to[0][0];
    			$mail->to[0][0] = $branch['email']; // this updates the main admin email
    			if( $mail->to[0][0] !== $original_admin_email ) { // this adds the original admin email as a bcc if it doesn't match the new address being pulled from the db
    				$reflector = new ReflectionObject( $mail );
    				$bcc = $reflector->getProperty( 'bcc' );
    				$bcc->setAccessible( TRUE );
    				$bcc->setValue( $mail, array( array( $original_admin_email ) ) );
    			}
    		}
    	}
    	return $mail;
    }

    I’ve made a further update that instead of completely replacing any existing BCC addresses, it appends the original admin email to any which already exist in the BCC array.

    It’s worth noting that this filter only seems to run when AJAX is switched off. It’s not a huge issue but it’d be nice to use it without having the page reload. Any ideas?

    add_filter( 'cforms2_admin_email_filter', 'change_cforms_admin_email', 10, 3 );
    function change_cforms_admin_email( $mail, $no, $pid ) {
    	if( strlen( $no ) < 1 ) {
    		$branch = get_branch( $pid );
    		if( isset( $branch[0]['email'] ) ) { // only proceed if this meta data exists
    			$original_admin_email = $mail->to[0][0];
    			$mail->to[0][0] = $branch[0]['email'];
    			if( $mail->to[0][0] !== $original_admin_email ) { // only make changes if the new address doesn't match the original admin address
    				$reflector = new ReflectionObject( $mail );
    				$bcc = $reflector->getProperty( 'bcc' );
    				$bcc->setAccessible( TRUE );
    				$current_bcc = $bcc->getValue( $mail );
    				foreach( $current_bcc as $cbcc ) { // check BCC for original admin address
    					if( $cbcc[0] == $original_admin_email ) {
    						$oa_already_exists = 1;
    					}
    				}
    				if( !isset( $oa_already_exists ) ) { // add original admin address to BCC
    					$new_bcc = $current_bcc;
    					$new_bcc[] = array( $original_admin_email, '' );
    					$bcc->setValue( $mail, $new_bcc );
    				}
    			}
    		}
    	}
    	return $mail;
    }
    Plugin Author bgermann

    (@bgermann)

    In the Core Form Settings, enable “Extra variables” to have $pid available with Ajax switched on.

    Excellent, all is working as expected and I’ve now got a level of control I didn’t have previously.

    Thanks again 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘programmatically change admin email address or add cc’ is closed to new replies.