无法在Wordpress上使用wp_mail功能发送带有pdf附件的电子邮件

时间:2019-12-11 22:42:35

标签: php wordpress email session pdf

希望大家都做的很好。我试图在Wordpress上使用fpdf发送带有pdf附件的电子邮件。

系统正在将带有pdf附件的电子邮件发送给所有者,但未向客户发送电子邮件。

代码如下:

<?php


    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->Image(get_stylesheet_directory() . '/images/xyz.jpg',10,10,30);
    $pdf->SetFont('Arial','',8);


    $pdf->Cell( 0, 5, 'Some text', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->Cell( 0, 5, 'Some text', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->Cell( 0, 5,'Tel. +01 (0123) 123456', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->Cell( 0, 5,'Internet: www.abc.com, E-Mail: xyz@exampl.com', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->Cell( 0, 3,' Some text', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->Cell( 0, 3,' Some text', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->Cell( 0, 3,' Some text', 0, 0, 'R' ); 
    $pdf->Ln();
    $pdf->SetFont('Arial','B',12);
    $pdf->Cell( 0, 10,'Some text ');
    $pdf->Ln();
    $pdf->Cell(40,6,'Some text');

   //To owner

    $to = 'john@example.com';
    $from = "Company"; 
    $subject = "This is subject"; 
    $message[] = '<strong>' . $_SESSION['registrationData']['first_name'] . ' ' . $_SESSION['registrationData']['last_name'] . '</strong><br />';
    $message[] = 'datefrom   <strong>' . $_SESSION['registrationData']['dtfromA'] . ' to ' . $_SESSION['registrationData']['dtfromB'] . '</strong><br />';
    $message[] = 'Number of Days: <strong>' . $_SESSION['registrationData']['days'] . '</strong><br /><br />';

    $message[] = 'The customer ID is: <strong>' . $customer->id . '</strong><br /><br />';
    $message[] = 'payment received to the amount of: <strong>' . $finalPrice / 100 . ' </strong><br>';
    $message[] = 'gender: <strong>' . $_SESSION['registrationData']['gender'] . '</strong><br />';
    $message[] = 'country: <strong>' . $_SESSION['registrationData']['citizenship'] . '</strong><br />';
    $message[] = 'Date of birth: <strong>' . $_SESSION['registrationData']['date_of_birth'] . '</strong><br />';
    $message[] = 'Phone: <strong>' . $_SESSION['registrationData']['phone'] . '</strong><br />';
    $message[] = 'Email: <strong>' . $_SESSION['registrationData']['email'] . '</strong><br />';


    $message = join('', $message);
    // 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 = "invoice.pdf";


    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));
    // main header
    $headers  = "From: ".$from.$eol;
    $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;
    $body .= "Purchase of a travel Insurance for .".$eol;

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

    $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."--";

    //mail($to, $subject, $body, $headers);
    wp_mail($to, $subject, $body, $headers);
    /************************************************************/
    // send email to customer with attachment
    /************************************************************/


    $toCustomer = $_SESSION['registrationData']['email'];
    //$toCustomer = 'example@example.com';
    $subjectCustomer = 'This is subject';
    // main header
    $headersCustomer  = "From: John <john@example.com> ".$eol;
    $headersCustomer .= "MIME-Version: 1.0".$eol; 
    $headersCustomer .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

    $messageCustomer = "--".$separator.$eol;
    $messageCustomer .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $messageCustomer .= "--".$separator.$eol;
    $messageCustomer .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $messageCustomer .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $messageCustomer .= 'Dear ' . $_SESSION['registrationData']['first_name'] . ' ' . $_SESSION['registrationData']['last_name'] .'<br>'.$eol.$eol;

    $messageCustomer .= 'Thank you for choosing our company.<br><br>'.$eol.$eol;

    $messageCustomer .= 'Enclosed please find your PDF.<br>'.$eol.$eol;

    $messageCustomer .= 'This document can be used to get Product A,B and C <br><br>'.$eol.$eol;

    $messageCustomer .= "Some Info text A.<br><br>".$eol.$eol;
    $messageCustomer .= 'Some Info text B:<br><br>'.$eol.$eol;
    $messageCustomer .= 'Some Info text C<br>'.$eol.$eol;
    $messageCustomer .= 'Some Info text D<br>'.$eol.$eol;


    $messageCustomer .= join('', $messageCustomer);
    // attachment
    $messageCustomer .= "--".$separator.$eol;
    $messageCustomer .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $messageCustomer .= "Content-Transfer-Encoding: base64".$eol;
    $messageCustomer .= "Content-Disposition: attachment".$eol.$eol;
    $messageCustomer .= $attachment.$eol;
    $messageCustomer .= "--".$separator."--";

    wp_mail($toCustomer, $subjectCustomer, $messageCustomer, $headersCustomer);

从上周开始,我一直在尝试解决问题,但找不到解决方案。任何人都可以调查一下为什么它在向所有者发送电子邮件时不向客户发送电子邮件,并且会话变量工作正常,并指出我在做什么错误吗?

0 个答案:

没有答案