WooCommerce: Attach File (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 default WooCommerce emails

But what we haven’t seen yet is how to “attach a file” to a default 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. So, 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 4.5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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;
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

29 thoughts on “WooCommerce: Attach File (PDF, etc.) To Emails

  1. 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?

  2. 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 🙂

  3. 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?

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

  5. 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?

  6. 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?

  7. 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?

  8. 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.

  9. 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!

  10. 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!

  11. 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

  12. 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!

  13. 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!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.