How to attach files to your email campaigns

Sending newsletters to your subscribers can be an effective way to keep them informed about your business, products, or services.

But what if you want to include more information than just text and images?

In that case, adding attachments to your newsletter emails can be a great solution. Attachments can be anything from PDFs and spreadsheets to videos.

Attach files to email campaigns

By including attachments in your emails, you can provide your subscribers with additional resources that help them better understand your message and take action.

Better still, you can use attachments as lead magnets.

This way, newsletter subscribers must provide their email addresses in exchange for access to a given file.

There are no limits to how many attachments you can add to a given email campaign.

In this tutorial, we’ll show you how to add attachments to your newsletter emails, step by step. So, let’s get started!

This is a premium feature.

That means that you have to purchase a plan before you can use it.

Add attachments to an email campaign

First, open the email in the email editor. It doesn’t matter whether this is a one-time newsletter campaign, an automated email, or a sequence of emails.

You can add attachments to any email, whether you’re sending it to newsletter subscribers, WooCommerce customers, or WordPress users.

Next, locate the attachments settings panel and click the “Add Attachment” button.

adding attachments to email campaign
Screenshot showing the email editor

You can attach as many files as you need. Clicking on the upload icon allows you to upload a new file, whereas clicking on the delete icon will delete the relevant attachment.

Attachments settings panel

The attachment should be on the same URL as your website’s URL. Remote files are not supported. It is also recommended that you limit your attachments since some email servers have limits on the maximum allowed email size.

Programmatically attach PDF URLs in the Email as attachments

Add the following PHP code snippet to your site:-

add_filter('noptin_get_email_prop_attachments', function($attachments, $email) {
    // Ensure attachments is an array
    if (!is_array($attachments)) {
        $attachments = [];
    }

    // Get email content in both formats
    $content_plain = $email->get_content();
    $content_visual = $email->get_content('visual');

    // Combine both contents for URL extraction
    $combined_content = $content_plain . ' ' . $content_visual;

    // Get current site URL dynamically
    $site_url = preg_quote(get_site_url(), '#');

    // Build regex to match PDF URLs in wp-content/uploads/[year]/[month]/
    $pattern = '#'.$site_url.'/wp-content/uploads/\d{4}/\d{2}/[^\s\'"]+\.pdf#i';

    // Find all matches
    preg_match_all($pattern, $combined_content, $matches);

    if (!empty($matches[0])) {
        // Remove duplicates and sanitize URLs
        $pdf_urls = array_unique($matches[0]);

        foreach ($pdf_urls as $url) {
            $attachments[] = esc_url_raw($url);
        }
    }

    return $attachments;
}, 10, 2);

Related Guides