I'm using a gift card plugin for my woocommerce shop and I would like to add a generated pdf file to allow customers to download the gift card as a pdf.

The idea is to use TCpdf or Fpdf to generate the pdf and output it as a string.

// Close and output PDF document
// This method has several options, check the source code documentation for more information.

echo $pdf->Output('S');

The problem is to get this file as an attachment with wp_mail. Right now, I have this code from the plugin, and i don't know how to call the pdf :

 * Hooks before the email is sent
 * @since 2.1
do_action( 'kodiak_email_send_before', $this );

$subject = $this->parse_tags( $subject );
$message = $this->parse_tags( $message );
$message = $this->build_email( $message );

$attachments = ??;

$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments );
$log_errors = apply_filters( 'kodiak_log_email_errors', true, $to, $subject, $message );

if( ! $sent && true === $log_errors ) {
  if ( is_array( $to ) ) {
    $to = implode( ',', $to );

  $log_message = sprintf(
    __( "Email from Gift Cards failed to send.\nSend time: %s\nTo: %s\nSubject: %s\n\n", 'kodiak-giftcards' ),
    date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ),

Is someone can help me with this ?

Thank you for the answer !


Usually I proceed like this

$attachment = array(filepath);

Where filepath is the path to your file.

| improve this answer | |

This is kind of tricky to do, but it is possible. Write a class which will extend PHPMailer. Override the addAttachment method, so in a certain condition (ex. you make a Boolean class variable) it will call addStringAttachment from the original class and in every other cases the addAttachment class. All you have to do is to set

$attachment = array("your content goes here");

When this is done, make sure that the global $phpmailer variable is an instance of your class and then create the specific condition. wp_mail will call the addAttachment from your class, and together with your special condition the attachment will be sent without saving the file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.