Contact Form 7 -continued

Contact form 7 save uploaded files in server and display uploaded image in mail(not as mail attachment)

Action hook: wpcf7_before_send_mail

The best way is to create a custom post type and save posted datas.

First, create contact form. Here my form name is ‘Application Form’.
add-new-contact-form-wordpress-site-wordpress

Then create post type:

function create_posttype() {
   register_post_type( 'online_applications',
   array(
     'labels' => array(
         'name' => __( 'Online Applications' ),
         'singular_name' => __( 'Online Application' )
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'online_applications'),
      'supports' => array('title','thumbnail')
    )
    );
}
add_action( 'init', 'create_posttype' );

Then call function to save posted datas:

add_action('wpcf7_before_send_mail', 'save_application_form' );
function save_application_form($wpcf7)
{
    global $wpdb;
    $formtitle  = $wpcf7->title;
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
        $submited = array();
        $formData       = $submission->get_posted_data();
        $uploaded_files = $submission->uploaded_files(); 
    
        if ( $formtitle == 'Application Form') {
          $newpost = array(
          'post_type' => 'online_applications',
          'post_title' => $formData['name']. ' - ' .$formData['email'],
          'post_status' => 'pending');

          $newpostid = wp_insert_post($newpost);
          add_post_meta($newpostid, 'name', $formData['name']);
          add_post_meta($newpostid, 'email', $formData['email']);
          add_post_meta($newpostid, 'photograph', $formData['photograph']);

          //save photograph
          $wud = wp_upload_dir();
          $image_name     = $formData['photograph'];
          $image_location = $uploaded_files["photograph"];
          $image_content  = file_get_contents($image_location);
          $upload = wp_upload_bits($image_name, null, $image_content);
          $chemin_final = $upload['url'];
          $filename = $upload['file'];
          if ($filename > '') {
              require_once(ABSPATH . 'wp-admin/includes/admin.php');
              $wp_filetype = wp_check_filetype(basename($filename), null);
              $attachment = array(
                  'post_mime_type' => $wp_filetype['type'],
                  'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                  'post_content' => '',
                  'post_status' => 'inherit'
              );
              $attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
              require_once(ABSPATH . 'wp-admin/includes/image.php');
              $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
              wp_update_attachment_metadata($attach_id, $attach_data);
              update_post_meta($newpostid, "_thumbnail_id", $attach_id);
          }

          //code to display uploaded image in mail template - starts here
          $photo_url = wp_get_attachment_url( get_post_thumbnail_id($newpostid) );
          $photo_url = isset($formData['photo_url']) ? $formData['photo_url'] : $photo_url;
          $photo_url = '<img src="'.$photo_url.'" alt="photo" width="250" height="250" />';
          // do some replacements in the cf7 email body
          $mail         = $wpcf7->prop('mail');
          // Find/replace the "[photo_url]" tag as defined in your CF7 email body
          // and add changes name
          $mail['body'] = str_replace('[photo_url]', $photo_url, $mail['body']);
          $wpcf7->set_properties(array(
              "mail" => $mail
          ));
          //code to display uploaded image in mail template - ends here
        }
        // return current cf7 instance
        return $wpcf7;
  }
}