Use cforms2_admin_email_filter and check its $mail->to.
-
This reply was modified 1 year, 11 months ago by
bgermann.
Also use its $pid argument to get the post that you are coming from if you have AJAX enabled.
Thread Starter
Robert
(@rsimpson)
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
Thread Starter
Robert
(@rsimpson)
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 1 year, 11 months ago by
Robert.
Thread Starter
Robert
(@rsimpson)
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;
}
Thread Starter
Robert
(@rsimpson)
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;
}
Thread Starter
Robert
(@rsimpson)
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;
}
Thread Starter
Robert
(@rsimpson)
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;
}
In the Core Form Settings, enable “Extra variables” to have $pid available with Ajax switched on.
Thread Starter
Robert
(@rsimpson)
Excellent, all is working as expected and I’ve now got a level of control I didn’t have previously.
Thanks again 🙂