使用fpdf在php中发送带有attachemnt的电子邮件?

时间:2017-05-06 07:22:07

标签: php pdf-generation sendmail email-attachments fpdf

This is my code for sending an email with dynamic generated pdf using fpdf in php.

我创建了一个动态pdf使用fpdf,它已成功创建。     这段代码完美地适用于我的第一次托管,但是当我在另一个域上使用时,这不是仅发送显示消息发送。     我在电子邮件标题中使用了无回复。

if (isset($_POST['submit'])) {
       $payment = $_POST['payment'];
       if ($errorMsg == "") {
          require("fpdf181/fpdf.php");
          $pdf        = new FPDF();
          $pdf -> AddPage();
          $pdf -> SetFont('Arial', 'B', 16);
          $pdf -> SetFont('Arial', 'B', 16);
          $pdf -> Image('images/logo.png', 95, 15, 20, 0);
          $pdf -> SetFontSize(10);
          $pdf -> SetTextColor(30, 30, 100);
          $pdf -> SetFont('Arial', 'B', 15);
          $pdf -> Line(10, 51, 200, 51);
          $pdf -> SetFontSize(10);
          $pdf -> SetTextColor(30, 30, 100);
          $pdf -> SetFont('Arial', 'B', 15);
          $pdf -> Line(10, 247, 200, 247);
          $pdf -> SetXY(88, 37);
          $pdf -> SetFontSize(17);
          $pdf -> SetTextColor(0, 0, 0);
          $pdf -> Write(7, 'Website');
          $pdf -> SetXY(75, 10);
          $pdf -> SetDrawColor(50, 60, 100);
          $pdf -> SetXY(90, 55);
          $pdf -> SetFontSize(13);
          $pdf -> SetTextColor(0, 0, 0);
          $pdf -> Write(7, 'Order Information');
          $pdf -> SetFontSize(11);
          $pdf -> SetXY(10, 65);
          $pdf -> Write(7, 'Order Details');
          $pdf -> SetXY(10, 75);
          $pdf -> Write(7, 'Order ID : ');
          $pdf -> SetXY(50, 75);
          $pdf -> Write(7, '#12121920120');
          $pdf -> SetXY(10, 82);
          $pdf -> Write(7, 'Date Added : ');
          $pdf -> SetXY(50, 82);
          $pdf -> Write(7, '11/12/2017');
          $pdf -> SetXY(10, 89);
          $pdf -> Write(7, 'Payment Method : ');
          $pdf -> SetXY(50, 89);
          $pdf -> Write(7, 'Direct Transfer');
          $pdf -> SetXY(10, 120);
          $pdf -> Write(7, 'Billing Address');
          $pdf -> SetXY(10, 130);
          $pdf -> Write(7, 'Name : ');
          $pdf -> SetXY(50, 130);
          $pdf -> Write(7, 'ABC');
          $pdf -> SetXY(10, 137);
          $pdf -> Write(7, 'Address : ');
          $pdf -> SetXY(50, 137);
          $pdf -> Write(7, 'Hisar');
          $pdf -> SetXY(10, 144);
          $pdf -> Write(7, 'City : ');
          $pdf -> SetXY(50, 144);
          $pdf -> Write(7, 'Hisar city');
          $pdf -> SetXY(10, 159);
          $pdf -> Write(7, 'Amount Payable : ');
          $pdf -> SetXY(50, 159);
          $pdf -> Write(7, '$5587');
          $pdf -> SetXY(10, 175);
          $pdf -> Write(7, 'Display Location ');
          $pdf -> SetXY(10, 185);
          $pdf -> Write(7, 'city');
          $pdf -> SetXY(50, 185);
          $pdf -> Write(7, 'City_name');
          $pdf -> SetXY(10, 192);
          $pdf -> Write(7, 'Location');
          $pdf -> SetXY(50, 192);
          $pdf -> Write(7, 'Locality');
          $pdf -> SetFontSize(20);
          $pdf -> SetTextColor(0, 0, 0);
          $pdf -> SetXY(50, 250);
          $pdf -> Write(7, 'Thank You For Choosing Us');
          $pdf -> SetFontSize(12);
          $pdf -> SetTextColor(0, 0, 0);
          $pdf -> SetXY(10, 260);
          $pdf -> Write(7, 'Contact Email : info@domain.in');
          $pdf -> SetXY(132, 260);
          $pdf -> Write(7, 'Mobile No. : +91-1201210921');
          $pdf -> Ln();
          $to         = 'abc@gmail.com';
          $from       = "info@domain.in";
          $message    = "Test email with attachment";
// 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   = 'Test.pdf';
// encode data (puts attachment in proper format)
          $pdfdoc     = $pdf -> Output("", "S");
          $attachment = chunk_split(base64_encode($pdfdoc));
// main header
          $headers    = "From: Website <" . $from . ">" . $eol;
//
          $headers    .= "MIME-Version: 1.0" . $eol;
          $headers    .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"";
//  $headers = "MIME-Version: 1.0" . "\r\n";
// $headers.="Content-type:text/html;charset=UTF-8" . "\r\n";
// no more headers after this, we start the body! //
          $body       = "--" . $separator . $eol;
          $body       .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
          $body       .= "" . $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
          $okk        = mail($to, $subject, $body, $headers);
          if ($okk) {
             echo "send";
             if ($payment == 'Direct Bank Transfer' || $payment == 'Cheque Payment') {
//            echo '<script type="text/javascript">alert("' . $order2 . '");</script>';
                echo "<script type='text/javascript'>window.location.assign('index.php?session_id=$order_session');</script>";
             }
             else {
                echo "<script type='text/javascript'>window.location.assign('index.php');</script>";
             }
          }
          else {
             echo "fail";
          }
       }
    }

此代码正在全部用于新注册。

    $to_ad = "info@domain.in";
    $subject_ad = "One New User Registered";
    $message_ad = "Name : $name<br /><br/>E-mail : $email<br/><br />Password : $password<br />";
    $to2 = $email;
    $subject = "Welcome to Website";
    $message = 'Thank you for joining us.<br/><br />Name : ' . $name . '<br /><br/>E-mail : ' . $email . '<br /><br/>Password : ' . $password . '<br /><br />
    <strong style="font-size:18px;"><a target="_blank" href="http://domain.in/email=' . $email . '&hash=' . $hash . '">Click here to email verification</a></strong>';
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers.="Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: Website <no-reply@domain.in>' . "\r\n";
    mail($to_ad, $subject_ad, $message_ad, $headers);
    mail($to2, $subject, $message, $headers);

如何解决?

1 个答案:

答案 0 :(得分:0)

如果您使用相同的域名发送和接收电子邮件,则可能无效。 还尝试将from标头更改为新域。 最后一个原因可能是与您的代码和php版本不兼容(如果您使用不同的托管)。