Home > Back-end >  send email to multiple wordpress contacts via filtered db query - contact form 7
send email to multiple wordpress contacts via filtered db query - contact form 7

Time:01-09

the problem is this forum is more irritating with each return visit. hopefully something better comes along. anyway. i want to send email to multiple contacts via db query. i was trying to do so using Contact Form 7. i'd like to save myself hours of trial and error coding. okay 2 things at issue: 1.) the Contact Form 7 validation won't allow multiple email address. 2.) i don't need multiple "From:" headers. i need multiple "To:", or "Cc:". That's my fault for being stupid.

<?php if (current_user_can('edit_users')) { 
        $email_array = array();
        $email_concat = '';
        $userlogin_array = array();
        $userlogin_concat = '';
        foreach ($bulk_user_ids as $user_id) {
            $user = get_user_by('id', $user_id); 
            $email_concat .= $user->user_email .','; 
            $userlogin_concat .= $user->user_login .', ';
        }         
?> 
<form action="users.php" method="post"  aria-label="Contact form" novalidate="novalidate" data-status="init">
<input size="40"  autocomplete="name" aria-required="true" aria-invalid="false" value="<?php 
echo $userlogin_concat; 
?>" type="text" name="your-name">
</p>
<p><label>Your email<br>
<input size="40"  value="<?php 
echo $email_concat; 
?>" type="email" name="your-email"> 

I was expecting to learn what happens when this code is executed. It results in error. I suppose it's javascript validation. Then I realize I'm using the wrong field anyway. Before I start over, I'd like to know if anyone has a recommended solution, at least. I don't understand why people are forced away from this forum. Your CEO or whatever. That's sad. It's not a newsgroup, or an email list. Though, it clearly presumes to be. I'll be banned, or the question will be marked off topic. It's a waste of time. That's the problem. If it makes it through, and you can read a 3rd-grade book about trees, or ever used WordPress to send email, i'm sure you can understand my question w/out further detail.

CodePudding user response:

Inorder to allow multiple email addresses in the Contact Form 7 validation, you can modify the wpcf7_email_validation_filter , see below.

        add_filter( 'wpcf7_email_validation_filter', 'custom_email_validation_filter', 20, 2 );
function custom_email_validation_filter( $result, $tag ) {
    $type = $tag['type'];
    $name = $tag['name'];

    if ( 'email' == $type || 'email*' == $type ) {
        $value = isset( $_POST[$name] ) ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) : '';

        if ( $tag['required'] && '' == $value ) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
        } elseif ( '' != $value && ! wpcf7_is_email( $value ) ) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );
        } else {
            $emails = explode( ',', $value );
            foreach ( $emails as $email ) {
                if ( ! wpcf7_is_email( $email ) ) {
                    $result['valid'] = false;
                    $result['reason'][$name] = wpcf7_get_message( 'invalid_email' );
                    break;
                }
            }
        }
    }

    return $result;
}

You should be able to add more than one email by adding a comma in between.I am assuming you know this code needs to be added to the functions.php in your theme folder.

Cheers !

  • Related