Last active
September 13, 2019 09:03
-
-
Save dancameron/048ee1ac3b476858cd54 to your computer and use it in GitHub Desktop.
Adding String Attachments (AddStringAttachment) with wp_mail (part 2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'phpmailer_init', '_add_string_attachments' ); | |
function _add_string_attachments( $phpmailer ) { | |
// the previous attachment will fail since it's not an actual file | |
// we will use the error to attach it as a string | |
if ( '' !== $phpmailer->ErrorInfo ) { | |
// error info | |
$error_info = $phpmailer->ErrorInfo; | |
$translations = $phpmailer->getTranslations(); | |
// See if there was an error attaching something to the email | |
if ( false !== stripos( $error_info, $translations['file_access'] ) ) { | |
// remove the messaging | |
$attachment_string = str_replace( $translations['file_access'], '', $error_info ); | |
// the result will be the json encoded string that was attempted to be attached by default | |
$attachment = json_decode( $attachment_string ); | |
// sanity check to see if there is an actual id that we're looking for | |
if ( isset( $attachment->pdf_doc_attachment_id ) ) { | |
// get the doc id so that you can create the content string for the attachment | |
$doc_id = $attachment->pdf_doc_attachment_id; | |
// do something to create the string attachment | |
$file_name = 'stuff.pdf'; | |
$pdf_string = 'stuff'; | |
try { | |
$phpmailer->AddStringAttachment( $pdf_string, $file_name, 'base64', 'application/pdf' ); | |
} catch ( phpmailerException $e ) { | |
continue; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment