Follow us on Facebook, X and YouTube
for tips and promotions

Build a WordPress contact form with attachments

2 minute read

Looking for a way to allow your users to upload files through your form and receive them directly in your inbox? You’re in the right place. In this article, we’ll discuss how Happyforms handles file uploads out of the box, the reasons behind it, and most importantly, how to send an email with attachments in WordPress.

Quick recap: file uploads with Happyforms

Happyforms offers a versatile file upload field that makes it easy to accept files of different sizes and types. When a user submits a file through your upload form, Happyforms stores it in your designated upload folder. Form uploads are then immediately available in your Media Library, Submissions screen, and email alerts.

You may be puzzled by the fact that file uploads aren’t directly attached to email alerts. Instead, Happyforms simply displays a link to the file in your Media Library screen. This may require a few additional clicks – but why is that?

The intricacies of email attachments

There’s a reason services like WeTransfer exist: sending emails with attachments in a reliable way is a real challenge. Here’s a brief list of things that could go wrong when you attach a file to your email and click the send button:

  • The attachment is too large.
  • The attachment is considered potentially dangerous.
  • The email provider doesn’t allow attachments at all.
  • Somewhere along the road to your recipient, the attachment was handled incorrectly.

When any of that happens, there’s a good chance that your email won’t land at all. That’s why Happyforms only displays a simple link: it’s the safest, most reliable, and provider-friendly way to add files to your emails. Additionally, your files are “locked up” in your dashboard. If your email alert is ever forwarded to a third recipient, they won’t have access to its attachments — unless, of course, you’ve given them access.

It all makes sense, but I really want to get the files in my inbox.

Alright, alright! We can’t deny that getting your files directly in your inbox makes for a much friendlier experience. Luckily for you, Happyforms can be configured to attach files to emails with a little code snippet. But first, let’s walk through this checklist:

  1. Configure WordPress to send emails through SMTP. Here’s a handy guide.
  2. Reach out to your email provider, and verify their attachment policy. In short, just make sure that files uploaded through your form don’t violate the requirements.
  3. Run a quick test, and make sure WordPress can send emails.

When you’ve ticked all the boxes, you’re ready for the final step: popping in a little code snippet. Open your theme’s functions.php file, and copy-paste this code at the very bottom:

add_filter( 'happyforms_email_alert', function( $email_message ) {
    $controller = happyforms_get_attachment_controller();
    $attachments = $controller->get( array(
        'response_id' => $email_message->message['ID'],
    ) );
    $attachment_ids = wp_list_pluck( $attachments, 'ID' );
    $paths = array_map( 'get_attached_file', $attachment_ids );
    foreach( $paths as $path ) {
        $email_message->add_attachment( $path );
    }

    return $email_message;

} );

That’s it! All that’s left is testing your form email alerts. Try uploading different files of different sizes, and double-check that they show up as attachments in your email client.

Build your high-conversion WordPress forms

Take a look at our three different plans and learn how Happyforms can help you get the most out of all of your WordPress website traffic today.

Discover Happyforms for WordPress