Support » Plugin: VS Contact Form » Sending email to 2 email addresses

  • Resolved barnez

    (@pidengmor)


    Hi,

    I have the form set-up and sending successfully. The email is then received. To send the submission to 2 email addresses, I have added email_to="first-email-here, second-email-here" to the shortcode, which now looks like this [contact class="form-font-1" email_to="first-email-here, second-email-here"].

    When I go to Settings >> General >> Email, and add two comma-separated email addresses, save messes with the format. e.g. I enter address1@email.com, address2@email.com (before save). When I save this becomes address1@email.comaddress2email.com (after save).

    Is this a bug?

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

Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter barnez

    (@pidengmor)

    Update.

    It seems like the email addresses need to be included in the shortcode ๐Ÿ™‚

    So, I have removed the two email addresses from Settings >> VSCF >> General >> Email, and added them into the shortcode as follows:

    
    [contact class="form-font-1" email_to="email-1@email.com, email-2@email.com"]
    

    The form sends correctly, but the only email that arrives is email-1@email.com, which is the site admin email address that appears as a placeholder in Settings >> VSCF >> General >> Email.

    Note, I have:
    – disabled all other plugins, and the theme is Twenty-Twenty
    – used a different second email address
    – change the order of the second email address in the shortcode

    • This reply was modified 6 months, 4 weeks ago by barnez. Reason: Added troubleshooting steps
    Plugin Author Guido

    (@guido07111975)

    Hi,

    This is a mistake in plugin code, I have used the native sanitize_email() function and that one strips the comma between the email addresses. This means currently multiple email addresses doesn’t work. Have to look into this, because email address must be properly checked before using it. Guess this has not been working for quite some time now.

    Guido

    Thread Starter barnez

    (@pidengmor)

    Hi,

    Ah, glad to hear it wasn’t a user error. It doesn’t sound like a quick fix, but I’ll keep an eye on the development updates as sending to two emails is useful in my case.
    Other than that, enjoy the rest of your weekend and thanks for developing this simple and clean contact form.

    Plugin Author Guido

    (@guido07111975)

    Hi,

    When I add CC/BCC attributes instead, will this be enough for you? This way you can send a CC and/or BCC of the submission to another email address.

    Gido

    Thread Starter barnez

    (@pidengmor)

    The ability to add CC/BCC attributes would be great. And I think a good option in most use cases. Is this currently possible within the shortcode? Or is this a solution you are planning?

    Plugin Author Guido

    (@guido07111975)

    Hi,

    No, currently not possible. I will add this in next update. But first on holiday for a week ๐Ÿ˜‰

    Guido

    Thread Starter barnez

    (@pidengmor)

    Wonderful. Enjoy your time off and goede reis!

    Actually the email_to value in the shortcode attributes does not appear to be sanitized but it is validated using is_email, which will reject a list.

    I think you need to add a custom cleanup function to apply to the email_to attribute, something like the (untested) code below. The idea is that it breaks the string at commas into an array, validates each address separately and generates an array containing only the valid addresses. The result could be an empty array. wp_mail will take a string or an array as the $to value.

    You could use this for CC and BCC fields, adding them to the $header array.

    Note that the email_settings value should never be a list because you want the FROM: field to be a single address, so the sanitization you have there is correct.

    This is code that could go in vscf-submission.php

    <?php
    function email_list_array( $list ) {
        $clean_list = array();
        if ( $list !== '' ) {
            $emails = explode( ',', $list );
            // Build the TO array from valid addresses in the list.
            foreach ( $emails as $email ) {
                $clean = sanitize_email( $email );
                if ( is_email( $clean ) ) {
                    $clean_list[] = $clean;
                }
            }
        }
        return $clean_list;
    }
    
    // In vscf-submission.php
        $email_admin = get_option('admin_email');
        $email_settings = get_option('vscf-setting-22');
        $email_to = email_list_array( $vscf_atts['email_to'] );
        $from_header = $vscf_atts['from_header'];
        // email address admin
        if ( count( $email_to) ) {
            $to = $email_to;
        } else {
            if ( is_email( $email_settings ) ) {
                $to = $email_settings;
            } else {
                $to = $email_admin;
            }
        }
    Plugin Author Guido

    (@guido07111975)

    @pidengmor – thanks! To be continued.
    @skirridsystems – that looks great, will take this into consideration. Thanks a lot! But I prefer to limit the number of email addresses. Otherwise a user can abuse this attribute and send submissions to many recipients. So maybe I only add the cc/bcc attributes.

    Guido

    Good point. I guess the cleanup function could stop once it’s got 3 addresses, or whatever you set as the limit.

    if ( is_email( $clean ) ) {
        $clean_list[] = $clean;
        if ( count( $clean_list ) >= 3 ) break;
    }

    I use this plugin quite a bit for club websites where the email goes to several members of the management committee. They’re all volunteers so the hope is that at least one person will respond! So even if you use CC or BCC, you still might want it to go to several recipients.

    Plugin Author Guido

    (@guido07111975)

    Hi @skirridsystems

    How about adding this in file submission:

    
    // email variables
    $email_admin = get_option('admin_email');
    $email_settingspage = get_option('vscf-setting-22');
    $email_to_attribute = $vscf_atts['email_to'];
    $to = '';
    
    // admin email address
    if ( !empty($email_to_attribute) ) {
    	if (strpos($email_to_attribute, ',') == true) {
    		$email_list_clean = array();
    		$email_list = explode(',', $email_to_attribute);
    		foreach ( $email_list as $email_single ) {
    			$email_clean = sanitize_email( $email_single );
    			if ( is_email( $email_clean ) ) {
    				$email_list_clean[] = $email_clean;
    			}
    		}
    		if ( count($email_list_clean) < 6 ) {
    			$to = implode(',', $email_list_clean);
    		}
    	} else {
    		$email_clean = sanitize_email( $email_to_attribute );
    		if ( is_email( $email_clean ) ) {
    			$to = $email_to_attribute;
    		}
    	}
    }
    if ( empty($to) ) {
    	if ( is_email($email_settingspage) ) {
    		$to = $email_settingspage;
    	} else {
    		$to = $email_admin;
    	}
    }
    

    So:
    If attribute value contains a comma > treat it as an array.
    Max 5 email addresses allowed.
    If attribute contains no (valid) email address > fallback to email from settingspage.

    Guido

    In the single address case for attribute you should have:
    $to = $email_clean;
    otherwise you’re checking the sanitized version but using the the non-sanitized version.

    Apart from that everything looks good to me. I like the new variable names too ๐Ÿ™‚

    if (strpos($email_to_attribute, ',') == true)
    is perfectly correct, but strpos will never actually return true, only false or an integer. I prefer !== false, but that’s just a personal style thing.

    Simon

    Plugin Author Guido

    (@guido07111975)

    Hi Simon,

    Many thanks again!

    otherwise youโ€™re checking the sanitized version but using the the non-sanitized version

    My mistake!

    but strpos will never actually return true, only false or an integer

    The condition must be valid, so will change this!

    Guido

    Plugin Author Guido

    (@guido07111975)

    Hi guys,

    I have fixed/updatend plugin. Please let me know if it works properly. Did not add CC/BCC attribute yet.

    Guido

    Thread Starter barnez

    (@pidengmor)

    Hi Guido.

    I can confirm that 2 emails are now received at two different comma-separated email addresses entered into the shortcode as email_to="xxx@xxx.com, xxx@xxx.com". Excellent!
    Thanks for fixing this.

    And diolch i chi to Simon for your input on this for someone who grew up just down the A40 in Carmarthen ๐Ÿ™‚

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Sending email to 2 email addresses’ is closed to new replies.