Search code examples
phpwordpresscontact-form-7

Contact form 7- Multi File Upload Values as Links in email


I am using the 'Contact Form 7 Drag and Drop FIles Upload - Multiple Files Upload' (https://codecanyon.net/item/contact-form-7-drag-and-drop-files-upload-multiple-files-upload/20683653) plugin within my form. The uploads could be quite big, so I do not want it attached to the mail but rather have links to the uploaded files in the mail.

I removed the tag from the mail attachment field and added it in the mail body, with the hopes that it will output the uploaded file links:

 <p><strong>IMAGES</strong><br/><br/>[dropfiles-291]</a></p>

But it only outputs the file names separated by a "|". eg: 'imagename1.jpg|imagename2.jpg|imagename3.jpg|imagename4.jpg'.

After some searching I found this code within the drag and drop plugin code:

add_filter('wpcf7_mail_tag_replaced_dropfiles', array($this, 'wpcf7_mail_tag_replaced'), 100, 3);

function wpcf7_mail_tag_replaced($text, $submitted, $html ){
    $upload_dir   = wp_upload_dir();
    $datas = explode("|",$text);
    $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
    $text_custom = array();

    foreach ($datas as $value) {
        $text_custom[] = $url.$value;
    }
    if($html){
        return implode(" <br>", $text_custom);
    }else{
        return implode(" | ", $text_custom);
    }           

}

I am not a php developer but the code looks to me like it is supposed to wrap the uploaded files with a link in the email. Hoever, it does not work. And after extensive online research I changed the code to:

add_filter('wpcf7_mail_tag_replaced', array($this, 'wpcf7_mail_tag_replaced_dropfiles'), 100, 3);

function wpcf7_mail_tag_replaced_dropfiles( $text, $submitted, $html ){        

    $upload_dir = wp_upload_dir();
    $datas = explode("|",$text);
    $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
    $text_custom = array();

    foreach ($datas as $value) {
        $text_custom[] = $url.$value;
    }
    if($html){
        return implode(" <br>", $text_custom);}
        else{return implode(" | ", $text_custom);}               

}

The email now has the desired link effect but on ALL the submitted fields in the form, not just the [dropfiles] fields. The [dropfiles] fields looks great with each uploaded file's link in a new line etc. But I, obviously, don't also want the normal text, textarea, checkbox, radio etc fields to also be links.

I have sent a support ticket to the plugin developer but have not heard anything back yet. What approach can I take to get this working?


Solution

  • I have found a temp solution - just until I get a permanent solution from the plugin developer. I changed the code as follows:

    add_filter('wpcf7_mail_tag_replaced', array($this, 'wpcf7_mail_tag_replaced_dropfiles'), 100, 3);
    
    function wpcf7_mail_tag_replaced_dropfiles( $text, $submitted, $html ){        
    if(preg_match('/\.(jpg|png|jpeg|gif)$/', $submitted))  {
        $upload_dir = wp_upload_dir();
        $datas = explode("|",$text);
        $url = $upload_dir["baseurl"]."/cf7-uploads-save/";
        $text_custom = array();
    
        foreach ($datas as $value) {
            $text_custom[] = $url.$value;
        }
        if($html){
            return implode(" <br>", $text_custom);}
            else{return implode(" | ", $text_custom);}               
        } 
    
        return $text; 
    }