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


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

需要帮助我的当前代码下面试图创建一个CSV文件,然后将其作为附件发送出去。在这个过程中一直没有运气。每次我接近,它发送电子邮件,HTML部分是工作的,但没有附件,只是在电子邮件中杂乱的代码。我想要CSV附件以及电子邮件

注意:$txt下面是一个HTML表,加上我们想要带入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不工作或头是不正确的。

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

  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 PGNlbnRlcj48dGFibGUgYWxpZ249ImNlbnRlciIgIHN0eWxlPSJ3aWR0aDo4NSU7IDsiPjx0cj48 dGQgY29sc3Bhbj0iNCIgYWxpZ249ImNlbnRlciIgc3R5bGU9InRleHQ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zPC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJiYWNrZ3Jv dW5kLWNvbG9yOiNlZWU7IHBhZGRpbmctbGVmdDoxMHB4OyB3aWR0aDoyMCU7ICI+PGI+SnVyaXNk aWN0aW9uLCBTdGF0ZTo8L2I+PC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJwYWRkaW5nLWxl ZnQ6MTBweDsgYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmOyB3aWR0aDozMCU7Ij5UZXN0IENvdW50 eSwgSW93YTwvdGQ+PC90cj48dHI+PHRkIGFsaWduPSJsZWZ0IiBzdHlsZT0iYmFja2dyb3VuZC1j b2xvcjojZWVlOyBwYWRkaW5nLWxlZnQ6MTBweDsgd2lkdGg6MjAlOyAiPjxiPkFzc2Vzc29yIFBo b25lOjwvYj48L3RkPjx0ZCBhbGlnbj0ibGVmdCIgc3R5bGU9InBhZGRpbmctbGVmdDoxMHB4OyBi

我做错了什么?请帮助我更好地理解这个发送邮件和附件。求你了,我需要你的帮助!

嗯,似乎你打开Price_Estimate.csv但然后调用file_get_contents在V_A_Price_Estimate.csv这是一个文件,你的代码不证明存在。

$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()是存在的东西-作为开始