0

I have a problem with wp_mail and sending a PDF. The code below occasionally works and when it does, you can only see the message content within the iPhone, and not Yahoo or Gmail.

function submit_paycerts( $post_id ) {
    $status          = get_field( 'paycert_status', $post_id );
    $certName        = get_field( 'paycert_name', $post_id );
    $certaddress     = get_field( 'paycert_address', $post_id );
    $certPostcode    = get_field( 'paycert_postcode', $post_id );
    $certAccount     = get_field( 'paycert_account', $post_id );
    $certPayMethod  = get_field( 'paycert_payment_method', $post_id );
    $weekEnding      = get_field( 'paycert_week_ending', $post_id );
    $certPayRate     = get_field( 'paycert_pay_rate', $post_id );
    $certEmail   = get_field( 'paycert_email', $post_id );
    $certUTR         = get_field( 'paycert_utr', $post_id );
    $certTaxStat     = get_field( 'paycert_tax_status', $post_id );
    $certVer         = get_field( 'paycert_verification_date', $post_id );
    $certExp         = get_field( 'paycert_expiry_date', $post_id );
    $certTotalHours = get_field( 'paycert_total_hours', $post_id );
    $cert4th         = get_field( 'paycert_fourth_meter', $post_id );
    $cert5th         = get_field( 'paycert_fifth_meter', $post_id );
    $cert6th         = get_field( 'paycert_sixth_meter', $post_id );
    $certTotalOn     = get_field( 'paycert_total_oncall', $post_id );
    $certTotalStand = get_field( 'paycert_standby', $post_id );
    $certStandWeek  = get_field( 'paycert_standby_weekend', $post_id );
    $certTotal   = get_field( 'paycert_total', $post_id );
    $certBonus   = get_field( 'paycert_bonus', $post_id );  
    $certAdd         = get_field( 'paycert_additions', $post_id );
    $certDeduct      = get_field( 'paycert_deductions', $post_id );
    $certExp         = get_field( 'paycert_expenses', $post_id );
    $certUTRLess     = get_field( 'less_cis_tax', $post_id );
    $certUTRTotal    = get_field( 'paycert_total_paid', $post_id );
    $certNote        = get_field( 'paycert_bonus_note', $post_id );

    $pdf = new FPDF( 'P','mm','A4' );
    $pdf->AddPage();
    $pdf->AddFont( 'Courier', '', 'courier.php' );
    $pdf->SetFont( 'courier', '', 14 );
    $pdf->Cell( 40, 10, 'Name: ' . $certName );
    $pdf->Cell( 40, 40, 'Address: ' . $certaddress . ', ' . $certPostcode );

    // email stuff (change data below)
    $to = $certEmail;

    $subj = 'Payment Certification - ' . $weekEnding;
    $headers = array( 'Content-Type: text/html; charset=UTF-8' );
    $headers[] = 'From: AES Admin <[email protected]>';

    $message = "<p>Please find your Payment Certification attached.</p>";

    // a random hash will be necessary to send mixed content
    $separator = md5( time() );

    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;

    // attachment name
    $filename = "PaymentCertification.pdf";

    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output( "", "S" );
    $attachment = chunk_split( base64_encode( $pdfdoc ) );

    // main header
    $headers[] = "MIME-Version: 1.0" . $eol; 
    $headers[] = "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"";

    // no more headers after this, we start the body! //
    $body  = "--" . $separator . $eol;
    $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;

    // message
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
    $body .= $message . $eol;

    // attachment
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol; 
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol . $eol;
    $body .= $attachment . $eol;
    $body .= "--" . $separator . "--"; 

    // send message
    if ( $status == 'Final' ) {
        wp_mail( $to, $subj, $body, $headers );
    } elseif ( $status == 'Draft' ) {
        // 
    }
}

1 Answer 1

1

Instead of attaching the attachments to body content. Pass it as separate parameter. Like the below one.

      @wp_mail($to, $subject, $msg, $headers,$mail_attachment);

I guess it solves your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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