Add file attachment to email

Adding an arbitrary file attachment to an email – REQUIRES WPPizza v3.2.10

wppizza_filter_mail_attachments

alter the filepath and file contents as well as the recipient key according to your requirements.

@param: array (current attachments)
@param: str (recipient)
@param: array (email settings)
@param: array (formatted order details)
@param: array (unformatted order details)
@return: array

example:

/* add attachment */
add_filter('wppizza_filter_mail_attachments', 'my_prefix_my_add_attachment', 10, 5);
function my_prefix_my_add_attachment( $wp_mail_attachments, $recipient_key, $email_settings, $order_formatted, $order_raw){
    /* attaching a file to emails to the shop */
    if($recipient_key == 'shop'){
        $data = json_encode($order_formatted, JSON_PRETTY_PRINT);//some data to store 
        $file_path = '/some/path/to/some/temp/file.json';//file path to the attachment file you will be sending
        file_put_contents($file_path, $data);//store the file
        $wp_mail_attachments[]=$file_path;//attach the file to email
    }
 return $wp_mail_attachments;
}

wppizza_on_email_sent

perhaps wrap the unlink into a file_exist or something too. if you want to keep the file we created above, simply omit this action (you’ll probably want to name the file something more unique though, like using the order id available in the $order_formatted parameter)

@param: str (recipient key)
@return: void

example:

/* unlink attachment again after email was sent*/
add_action('wppizza_on_email_sent', 'my_prefix_my_unlink_attachment');
function my_prefix_my_unlink_attachment($recipient_key){
    if($recipient_key == 'shop'){
        $file_path = '/some/path/to/some/temp/file.json'; //path to the file we created  above
        @unlink($file_path); //delete file after we're done with it 
    }
return;
}