发送PDF时出现WP_MAIL问题

时间:2015-11-15 作者:user83662

我有个问题wp_mail 并发送PDF。下面的代码偶尔会起作用,当它起作用时,你只能在iPhone中看到消息内容,而不能在Yahoo或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 个回复
SO网友:Kvvaradha

而不是将附件附加到正文内容。将其作为单独的参数传递。就像下面的一样。

      @wp_mail($to, $subject, $msg, $headers,$mail_attachment);
我想这解决了你的问题。

相关推荐

无法使用FPDF将pdf文件写入上传目录

PHP Warning: file_put_contents(http://lendersmatch.ca/wp-content/uploads/deals/deal1.pdf): failed to open stream: HTTP wrapper does not support writeable connections in /home/t21jv08zz60b/public_html/wp-content/plugins/mortgage/fpdf181/fpdf.php on line 1