How to add uploaded file URL in WP CF7’s Email

Hello Guys,

Hope you all are doing good.

Today we are going to discuss regarding adding the uploaded media URL into our contact form 7 email.

First of all you need to install the WordPress plugin “Contact Form 7 Database Addon – CFDB7“. Once you install this plugin. All your form submissions will be store into database including files. In the case of files upload, a reference can be stored into file system or directory.

After installing above mentioned plugin “Contact Form 7 Database Addon – CFDB7“. We going to use following code to add the uploaded media file URL to the email sent on contact form 7 submission.

Email body of contact form should have some text which will get replace with the uploaded file URL. Here I am using a text “uploadedFileHere”. You can choose any text of your choice.

add_action('cfdb7_before_save', 'getUploadedFilePath');
function getUploadedFilePath($form_data){
	$_SESSION['getUploadedFilePathFormData'] = isset($form_data['uploadFilecfdb7_file']) && strlen(trim($form_data['uploadFilecfdb7_file'])) > 0 ? $form_data['uploadFilecfdb7_file'] : '';
}

Replace the file text placeholder with File URL

The below code will add/replace reference text “uploadedFileHere” in email with file name the users have uploaded. This could be change into email

add_filter("wpcf7_before_send_mail", "wpcf7_get_n_change_upload_file_path");

function wpcf7_get_n_change_upload_file_path($WPCF7_ContactForm){
    $uploadFileDirPath = home_url('wp-content/uploads/cfdb7_uploads/');
    //Get current form
    $wpcf7CurrentForm = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();
    if($submission){
        $posted_data = $submission->get_posted_data();
        if ( empty( $posted_data ) ){ return; }
	$uploadFilePath = isset($_SESSION['getUploadedFilePathFormData']) && strlen(trim($_SESSION['getUploadedFilePathFormData'])) > 0 ? '<strong>Uploaded File:</strong> <a href="'.($uploadFileDirPath.''.$_SESSION['getUploadedFilePathFormData']).'">'.$_SESSION['getUploadedFilePathFormData'].'</a>' : '';

        $searchText = "uploadedFileHere";
	$replaceText = $uploadFilePath;
	// Got e-mail text
	$mailBody = $wpcf7CurrentForm->prop( "mail" );
	// Replace text with search text inside e-mail text
	$updatedMail = str_replace( $searchText, $replaceText, $mailBody );
	// Set New Mail Content to set updated mail
	$wpcf7CurrentForm->set_properties( array( "mail" => $updatedMail ) );
        return $wpcf7CurrentForm;
    }
}

Read Here: How To Hide Badges From WordPress Max Mega Menu

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

eight + one =