WooCommerce: Attach Files (PDF, etc.) To Emails

Sure, you can add content to a given WooCommerce transactional email such as the Customer Processing Order email. You can even edit the email subject, for example by adding the first name. No matter what you need, there are many cool snippets to customize the default WooCommerce emails.

But what we haven’t seen yet is how to “attach a file” to a WooCommerce Order email. It seems like a very much needed admin option, but there is nothing to achieve that in the WooCommerce dashboard. And maybe that is because it’s super easy to do that with a few lines of PHP or a super simple plugin. Enjoy!

The New Order email now features a fancy PDF attachment.

PHP Snippet: Attach File to WooCommerce Order Emails

First of all, you need to upload the file somewhere. In my case, I decided to upload it via the Media > Add New section of the WordPress dashboard. You can also choose to place the file in your child theme folder for example, in which case you will need to work with get_stylesheet_directory().

In the example below, my file is called example.pdf and has been placed under /wp-content/uploads/2020/09/example.pdf – inside the snippet you will see the wp_upload_dir() function that is needed to return the uploads folder “path” (not the absolute URL otherwise it won’t work).

Also, I decided to attach such file only in the “new_order” and “customer_processing_order” emails. You can pick yours, and you can find the email IDs here: https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/

/**
 * @snippet       File Attachment @ WooCommerce Emails
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_email_attachments', 'bbloomer_attach_pdf_to_emails', 10, 4 );

function bbloomer_attach_pdf_to_emails( $attachments, $email_id, $order, $email ) {
    $email_ids = array( 'new_order', 'customer_processing_order' );
    if ( in_array ( $email_id, $email_ids ) ) {
        $upload_dir = wp_upload_dir();
        $attachments[] = $upload_dir['basedir'] . "/2020/09/example.pdf";
    }
    return $attachments;
}

Mini-Plugin: Business Bloomer WooCommerce Attach Files To Order Emails

You don’t feel confident with coding? You don’t want to purchase yet another bloated, expensive plugin? Great!

Business Bloomer WooCommerce Attach File To Order Emails is a mini WooCommerce plugin, without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. No admin dashboard.

Screenshot of the settings:

Quick demo:

As you can see the settings are pretty straight forward. Select a WooCommerce email from the dropdown, and add the attachment URL beside it. Add as many attachments as you wish. Simple!

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Content to a Specific Order Email
    Customizing WooCommerce emails via the WordPress dashboard is not easy and – sometimes – not possible. For example, you can’t edit or add content to them unless you’re familiar with code. Well, here’s a quick example to learn how to add content to any WooCommerce default order email. In this case study, our goal is […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce Visual Hook Guide: Emails
    WooCommerce customizers: the Visual Hook Guide is back! Here’s a visual HTML hook guide for the WooCommerce Emails. This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations. Let me know in the comments if this […]
  • WooCommerce: Add To: Cc: Bcc: Email Recipients
    The WooCommerce Email Settings allow you to add custom recipients only for New Order, Cancelled Order, Failed Order and all admin-only emails. But what if you want to add an email recipient to a customer email e.g. the Completed Order one? For example, you need to send it to your dropshipper. Also, you might want […]
  • WooCommerce: Remove Link to Product @ Order Table
    There is a slightly annoying thing on the WooCommerce Thank-You Page and WooCommerce emails. Users looking at the order table can actually click on the Products they just purchased and abandon the page before taking the action you want them to take (see image below). So, I coded a simple PHP snippet to remove such […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

37 thoughts on “WooCommerce: Attach Files (PDF, etc.) To Emails

  1. Hi Rodolfo,
    is it also possible to attach the pdf invoice to the completed order? because this pdf invoice is not in the media library.

    Thank you and regards, York

    1. Hello York. Doesn’t your PDF invoice plugin let you attach the invoice to whatever WooCommerce email out of the box?

  2. Hi

    Does this snippet only work with .pdf files? I need to send .docx

    1. Works with any file extension, as long as the email client doesn’t block it (for example, Gmail doesn’t allow for .ade, .adp, .apk, .appx, .appxbundle, .bat, .cab, .chm, .cmd, .com, .cpl, .diagcab, .diagcfg, .diagpack, .dll, .dmg, .ex, .ex_, .exe, .hta, .img, .ins, .iso, .isp, .jar, .jnlp, .js, .jse, .lib, .lnk, .mde, .msc, .msi, .msix, .msixbundle, .msp, .mst, .nsh, .pif, .ps1, .scr, .sct, .shb, .sys, .vb, .vbe, .vbs, .vhd, .vxd, .wsc, .wsf, .wsh, .xll)

  3. Hi Rodolfo,
    This is a pearl!! I have been looking for…! But I think it conflicts with my plugin Woocommerce Invoice?
    My site crashes when I add this in and the file path is correct and the file in place 🙂
    Maybe it’s using the same var names or so?
    Is there some way to change the names or is it the “return [dollar-sign]attachments;” that only is allowed once?
    WooCommerce Invoices do attach a invoice to certain mails (customer_Completed).
    WooCommerce Invoices can attached the terms page as well – But it looks crap (all the shortcode from Enfold get printed)
    So I created a TERMS page to attach with the invoice mail (two attachments) as well… That is the file I want to add

    Best Regards

    Netzie

    1. Hi Peter, what error do you get?

  4. Hi,
    Once an order is placed, I’d like to add the packing-slip PDF automatically generated, and attach it to the first email the customer receives after ordering.
    So by what file could I replace your line with example.pdf ?
    Thanks

    Patrick

    1. Hi Patrick! Your packing slips plugin should already give you that option?

  5. He Rodolfo,
    is it possible to specify the snippet to a specific product? So the attachment is only send when this specific product is in the order?

    1. Hi Cees-Jan, sure, that’s possible. I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂

  6. The Snippet does not work. Ive attached it to functions.php and it doesnt work

    1. Is the file URL correct? Can you share your code?

  7. Hello,
    It’s working great, nice snippet. Thank you.

  8. Hey!
    First of all, it works fine!
    But i have a problem: The attachment is identified as Phishing and the email ends up in the spam folder (i use sendinblue).

    Any thoughts on this?

    1. Not sure. Does that happen even if you attach a JPG for example?

  9. 16th July 2021. this is not working for me. anyone else having any issues

    1. Works for me. Can you try disable all plugins but Woo?

  10. Not working may I know whats the problem

    1. Works for me. Have you tested on a fresh WP install with only Woo active and a default theme?

  11. It works partially. I (the seller) receive the email with the attachment but customer receives an email without the attachment. Why is that?

    1. ok, customer_on_hold_order is the answer.

  12. Thanks a lot! How to add two files?

    1. Hi Artur, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  13. Hello,
    Can I send a dynamically generate pdf file using this? I mean pdf name is varying.

    1. Hi Krishna, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  14. Hello, what if we need to add different PDFs as attachments to the “Completed Order” email, e.g. I use external software to generate invoices and I’d like to send them to the clients with the “Completed Order” email?

    1. Uhm not sure

  15. Hello men,
    Your site has helped us countless times.

    And now he would help us if you give us code for multiple pdf files per email order.

    Thx
    Bojan

    1. Hi Bojan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  16. Hello Rodolfo,
    as usual you are such an helpful ressource when it comes to customise WooCommerce. This works like a charm !
    All the bests

    1. Yay!

      1. What if I wanted to attach different invoices for each specific order, when adding a new order in WooCommerce instead of a default attachment for every new order? How would I go about that?

        1. Hi David, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

          1. Yo Rodolfo,

            What about if we have dynamically created file, which have to be deleted after download?

            1. Hey Tesla, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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