PHP / Jquery:创建 CSV 文件并作为电子邮件附件发送

PHP / Jquery: Creating a CSV file and Sending as an email Attachment

需要有关我当前尝试创建 CSV 文件然后将其作为附件发送的代码的帮助。在执行此过程时运气不佳。每次我靠近时,它都会发出电子邮件,HTML 部分正在工作,但没有附件,只是电子邮件中的乱码。我想要 CSV 附件和电子邮件

注意:$txt 下面是一个 HTML table,加上我们想导入 CSV 的内容。

注意:我使用的是wordpress。

此时此刻我不想使用 PHPmailer(),也不想使用 FPDF、MPDF 或任何类似的东西。如果您确实建议使用 PHPmailer(),那么请给我一份非常详细的文档或关于如何使其工作的答案,因为我之前尝试过多种方法但没有成功。

我尝试了多种方法,但似乎无法正常工作。我真的,真的需要任何人的帮助,对我做错了什么的感叹和一些示例代码或固定代码。

我的代码如下:

这是我尝试创建 CSV 文件的部分:

 $jurisdiction = $_POST['jurisdiction']; // required 
    $state = $_POST['state']; // required 
    $representative = $_POST['sales_rep']; // required
    $from = $_POST['email']; // required
    $client = $_POST['client']; // required     
    $client_email = $_POST['client_email']; // not required 
    $client_phone = $_POST['client_phone']; // not required
    $date = date("F j, Y"); 

    //Mail To
    $to = "email@someemail.com"; 

    //Table Data
    $txt = $_POST['html_div_table_content'];

    //The Attachment                                 
    $data = "<center><table align=\"center\"  style=\"width:85%; ;\"><tr><td colspan=\"4\" align=\"center\" style=\"text-align:center;\" ><span style=\"text-transform:uppercase; font-weight:bold; font-size:20px; text-align:center;\">Contact Information</span></td></tr><tr><td align=\"left\" style=\"background-color:#eee; padding-left:10px; width:20%;\"><b>Representative:</b></td><td align=\"left\" style=\"padding-left:10px; background-color:#ffffff; width:30%;\">".clean_string($representative)."</td><td align=\"left\" style=\"background-color:#eee; padding-left:10px; width:20%; \"><b>Representative E-Mail:</b></td><td align=\"left\" style=\"padding-left:10px; background-color:#ffffff; width:30%;\">".clean_string($from)."</td></tr><tr><td align=\"left\" style=\"background-color:#eee; padding-left:10px; width:20%; \"><b>client Name:</b></td><td align=\"left\" style=\"padding-left:10px; background-color:#ffffff; width:30%;\">".clean_string($client)."</td><td align=\"left\" style=\"background-color:#eee; padding-left:10px; width:20%; \"><b>Jurisdiction, State:</b></td><td align=\"left\" style=\"padding-left:10px; background-color:#ffffff; width:30%;\">".clean_string($jurisdiction).", ".clean_string($state)."</td></tr><tr><td align=\"left\" style=\"background-color:#eee; padding-left:10px; width:20%; \"><b>client Phone:</b></td><td align=\"left\" style=\"padding-left:10px; background-color:#ffffff; width:30%;\">".clean_string($client_phone)."</td><td align=\"left\" style=\"background-color:#eee; padding-left:10px; width:20%; \"><b>client E-mail:</b></td><td align=\"left\" style=\"padding-left:10px; background-color:#ffffff; width:30%;\">".clean_string($email_from)."</td></tr></tr></table></center><br /><br /><P style=\"font-size:10px; color:#ff0000; text-align:center; margin-bottom:4px;\">* Prices below are only an estimated price and is in no way an official cost from Vanguard Appraisals, Inc.</p><br />".$txt."<br /><center><p style=\"font-family: 'Century Gothic',CenturyGothic,AppleGothic,sans-serif; font-size:18px; width:90%; text-align:left;\">For an official price quote, or to get a quote on hardware items, please contact Vanguard Appraisals home office at (319)365-8625 or by e-mail at <a href=\"mailto:info@camavision.com\">info@camavision.com</a>.</p></center>"; 

    $fp = fopen('Price_Estimate.csv','a'); 
    fwrite($fp,$data); 
    fclose($fp);                            

    $attachments[] = Array( 
        'data' => $data, 
        'name' => 'Price_Estimate.csv', 
        'type' => 'application/vnd.ms-excel' 
    ); 

    //Generate a boundary string 
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    //$attachment = chunk_split(base64_encode(file_get_contents('Vanguard_Appraisals_Price_Estimate.csv')));

这里是我发送 HTML 电子邮件和 CSV 文件的地方:

    //Subject Line
    $subject = "Price Quote Submission";;
    $subject2 = "Representative Copy of Price Quote Submission";

    $headers = "MIME-Version: 1.0\n" . 
    "From: {$from}\n" . 
    "Cc: homeoffice@camavision.com\n". 
    "Content-Type: multipart/mixed;\n" . 
    " boundary=\"{$mime_boundary}\"";

    $message .= "This is a multi-part message in MIME format.\n\n" . 
     "--{$mime_boundary}\n" . 
    "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
    "Content-Transfer-Encoding: 7bit\n\n" . 
    $text . "\n\n";

    //Add sttachments
    $content = chunk_split(base64_encode(file_get_contents('Price_Estimate.csv'))); 
    //$name = $attachment['name']; 
    //$type = $attachment['type'];

    $message .= "--{$mime_boundary}\n" . 
    "Content-Type: {$type};\n" . 
    " name=\"{$name}\"\n" .               
    "Content-Transfer-Encoding: base64\n\n" . 
    $content . "\n\n" ;

    $message .= "--{$mime_boundary}--\n";

    // create Representative email headers and HTML part of Email Message
    //$headers = "From: ".$from. "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    $headers .= "Content-Transfer-Encoding: 8bit";

    mail($to,$subject,$message,$headers);

    if(mail($to,$subject,$message,$headers))
    {
         echo "<p style=\"font-family: 'Century Gothic',CenturyGothic,AppleGothic,sans-serif; font-size:18px; margin-top:-50px;\" >Thank you for submitting the following price quote on ".$date.". It has been successfully submited to the home office. </p>";
    } else {
        echo "<p style=\"font-family: 'Century Gothic',CenturyGothic,AppleGothic,sans-serif; font-size:18px; margin-top:-50px;\" >We are very sorry, but there were error(s) found with the price quote you submitted. Please return back to the price quote page and try re-submitting the form again.  </p>";
    }

我感觉是两种情况之一,CSV 不工作或 headers 不正确。

就像我之前提到的,它打印出 HTML 部分的消息,这是电子邮件的 table 部分,但它不会发送附件。只是有所有的杂音。这是我在电子邮件中收到的一些乱码的示例:

  This is a multi-part message in MIME format.     --==Multipart_Boundary_xd05a16d079ea07ef8e66bcf5fcc544c2x Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_xd05a16d079ea07ef8e66bcf5fcc544c2x Content-Type: portfolio; name="price-quote-submission" Content-Transfer-Encoding: base64 PGNlbnRlcj48dGFibGUgYWxpZ249ImNlbnRlciIgIHN0eWxlPSJ3aWR0aDo4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ja2dyb3VuZC1jb2xvcjojZmZmZmZmOyB3aWR0aDoz MCU7Ij5KYW1lcyBFYXJsIEpvbmVzPC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJiYWNrZ3Jv dW5kLWNvbG9yOiNlZWU7IHBhZGRpbmctbGVmdDoxMHB4OyB3aWR0aDoyMCU7ICI+PGI+SnVyaXNk aWN0aW9uLCBTdGF0ZTo8L2I+PC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJwYWRkaW5nLWxl ZnQ6MTBweDsgYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmOyB3aWR0aDozMCU7Ij5UZXN0IENvdW50 eSwgSW93YTwvdGQ+PC90cj48dHI+PHRkIGFsaWduPSJsZWZ0IiBzdHlsZT0iYmFja2dyb3VuZC1j b2xvcjojZWVlOyBwYWRkaW5nLWxlZnQ6MTBweDsgd2lkdGg6MjAlOyAiPjxiPkFzc2Vzc29yIFBo b25lOjwvYj48L3RkPjx0ZCBhbGlnbj0ibGVmdCIgc3R5bGU9InBhZGRpbmctbGVmdDoxMHB4OyBi

我做错了什么。请帮助我更好地理解此发送电子邮件和附件。拜托我需要你的帮忙!

嗯,您似乎打开了 Price_Estimate.csv,但随后在 V_A_[=27= 上调用了 file_get_contents ] 这是一个您的代码无法证明存在的文件。

$fp = fopen('Price_Estimate.csv','a'); 

...

$content = chunk_split( 
      base64_encode(
          file_get_contents(
              'Price_Estimate.csv'
          )
      )
 ); 

...

当您将它作为附件发送时,您可以随意命名它,但是现在,您正在打开一个(看似)不存在的文件。所以,这部分没问题(无论如何都可以进行即时重命名):

$attachments[] = Array( 
    'data' => $data, 
    'name' => 'Price_Estimate.csv', 
    'type' => 'application/vnd.ms-excel' 
); 

如果还有什么不对的,暂时还不知道。但是你必须确保 file_get_contents() 存在的东西 - 作为开始。