WordPress

How to Automatically Send Multiple PDF Attachments via Email in WPForms Using Custom Code

Written by AskYouNow

In today’s digital world, automating the process of sending necessary documents to users upon form submission can save time and improve user experience. If you’re using WPForms on your WordPress website, you might want to send multiple PDF attachments automatically when a user submits a form. Whether it’s registration forms, acknowledgment forms, or any other documents, you can achieve this with custom code.

Step-by-Step Guide to Sending Multiple PDF Attachments

In this tutorial, we’ll guide you through the process of sending multiple PDF attachments via email in WPForms using a custom code snippet. We’ll also cover how to handle multiple forms and email fields dynamically.

Below is the custom PHP code that allows you to send multiple PDF attachments via email in WPForms. We’ll break down each part of the code to help you understand its functionality.

				
					function custom_wpforms_send_multiple_attachments( $fields, $entry, $form_data, $entry_id ) {
    // Specify the form IDs where you want to attach the PDFs
    $target_form_ids = array(9156, 6580, 6578, 6576); // Replace with your form IDs
    $kidsform_ids = array(6600, 6598, 6590, 6584); // Replace with your form IDs

    // Check if the current form matches any of the target forms
    if ( in_array( $form_data['id'], $target_form_ids ) ) {
        $email_field_ids = array('24'); // Replace with your email field keys for target forms
		//Replace url with your pdf url and name with pdf file name
        $pdf_files = array(
            array(
                'url'  => 'https://testwebsite.qa/wp-content/uploads/2024/09/dummy1.pdf', 
                'name' => 'dummy1.pdf'
            ),
            array(
                'url'  => 'https://testwebsite.qa/wp-content/uploads/2024/09/dummy.pdf',
                'name' => 'dummy.pdf'
            ),
        );
    } elseif ( in_array( $form_data['id'], $kidsform_ids ) ) {
        $email_field_ids = array('26', '27'); // Replace with your email field keys for kids forms
        $pdf_files = array(
            array(
                'url'  => 'https://testwebsite.qa/wp-content/uploads/2024/09/dummy1.pdf',
                'name' => 'dummy1.pdf'
            ),
            array(
                'url'  => 'https://testwebsite.qa/wp-content/uploads/2024/09/dummy.pdf',
                'name' => 'dummy.pdf'
            ),
        );
    }

    // If the form matches one of the specified form IDs, process the email
    if ( isset($email_field_ids) ) {
        $name = $fields['0']['value']; // Assuming the name field has ID '0'
        
        // Loop through each email field ID to get the email addresses
        foreach ( $email_field_ids as $email_field_id ) {
            if ( isset($fields[ $email_field_id ]['value']) ) {
                $user_email = $fields[ $email_field_id ]['value'];
                $attachments = array();

                // Download the PDFs and store them with the correct file names
                foreach ( $pdf_files as $pdf ) {
                    $tmp_file_path = download_url( $pdf['url'] );
                    if ( ! is_wp_error( $tmp_file_path ) ) {
                        $pdf_file_path = sys_get_temp_dir() . '/' . $pdf['name'];
                        rename( $tmp_file_path, $pdf_file_path );
                        $attachments[] = $pdf_file_path;
                    }
                }

                // Set up the email parameters
                $to      = $user_email;
                $subject = 'Here Are your Attchment';
                $message = '
                    <html>
                    <body>
                        <p>Dear [Name],</p>
                        <p>Thank you for registering Attached are the following documents:</p>
                        <p><b>Best Regards,</b><br>Website name</p>
                    </body>
                    </html>';
                $headers = array(
                    'Content-Type: text/html; charset=UTF-8',
                    'From: Website Name <no-reply@websiteurl.qa>'
                );

                // Send the email with multiple attachments
                wp_mail( $to, $subject, $message, $headers, $attachments );

                // Clean up the temporary files
                foreach ( $attachments as $file_path ) {
                    @unlink( $file_path );
                }
            }
        }
    }
}
add_action( 'wpforms_process_complete', 'custom_wpforms_send_multiple_attachments', 10, 4 );
				
			
How the Code Works
  • Form ID Checking: The code first checks if the submitted form matches any of the specified form IDs. This is useful if you want to send different PDF attachments based on different forms.
  • Email Field Handling: The code then checks the email fields associated with the form and sends the attachments to the emails entered in these fields.
  • PDF Attachment Handling: It downloads the PDFs from the specified URLs, renames them appropriately, and attaches them to the email.
  • Email Sending: Finally, it sends an HTML-formatted email to the user, with the PDFs attached.
  • Clean-Up: After sending the email, the temporary files are deleted to avoid cluttering your server.
Customizing the Code
  • Form IDs: Replace the form IDs in $target_form_ids and $kidsform_ids with the actual IDs of the forms you want to target.
  • Email Field IDs: Update $email_field_ids with the IDs or keys of the email fields used in your forms.
  • PDF URLs: Modify the $pdf_files array with the URLs and file names of the PDFs you want to send.
  • Email Content: Customize the $subject and $message variables to fit your specific needs.
Implementing the Code

To implement this custom functionality, you can add the above code to your theme’s functions.php file or use a site-specific plugin. Be sure to test thoroughly to ensure everything works as expected.

Conclusion

Automating the sending of PDF attachments in WPForms is a powerful feature that can enhance user experience and streamline your operations. With the custom code provided in this blog, you can easily set up this functionality to work with multiple forms and email fields dynamically.

Feel free to adapt the code to suit your specific requirements, and enjoy the convenience of automated document delivery!

We hope this article helped you learn how to change the sender’s name and email address in outgoing WordPress emails.

About the author

AskYouNow

Leave a Comment

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code