How do I successfully create a hook for an email override?

I’m creating an online apartment rental application using Visual Form Builder Pro. I need to have completed forms go to certain managers based on the property selection. Once a user selects a property location and submits the form, it must be routed via email to the appropriate apartment manager. For example, I choose Arlington Arms Apartments in my application, complete the form and submit it. The Arlington Arms manager should be the only individual who receives my completed application via email. I started out with creating a vfb_override_email_114.php file but I’m not sure where to save it so that my code will be picked up once a submission occurs. I’m inexperienced with code so any help would be greatly appreciated!

Application: http://simco-apts.com/testsite/?page_id=140

Code so far:

<?php
/*
Email Override for Applications 
*/
?>
<?php 
add_action( 'vfb_override_email_114', 'vfb_action_override_email', 10, 5 );

function vfb_action_override_email( $emails_to, $form_subject, $message, $headers, $attachments ){
    // Checks radio button. Use Merge Tag to get $_POST id
    if ( 'Arlington Arms' == $_POST['vfb-5'] )
        $emails_to = array( 'arlingtonarms@simco-apts.com' );
    elseif ( 'Crestview' == $_POST['vfb-5'] )
        $emails_to = array( 'crestview@simco-apts.com' );

    // Send the mail
    foreach ( $emails_to as $email ) {
        wp_mail( $email, $form_subject, $message, $headers, $attachments );
    }
}

1 Answer
1

WordPress uses the PHPMailer class for sending mail. Before calling wp_mail, you can manipulate the $phpmailer object using the phpmailer_init filter.

//Place this inside of your form callback before wp_mail()
add_filter('phpmailer_init', 'foobar_phpmailer', 99999, 1);

function foobar_phpmailer(&$phpmailer){
    //Manipulate the phpmailer object here
}

Leave a Comment