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

时间:2016-10-26 21:01:14

标签: php jquery html email email-attachments

在下面尝试创建CSV文件然后将其作为附件发送时,需要有关我当前代码的帮助。在这个过程中没有运气。每当我接近,它发出电子邮件,HTML部分正在工作,但没有附件,只是电子邮件中的jibble jabble代码。我想要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 PGNlbnRlcj48dGFibGUgYWxpZ249ImNlbnRlciIgIHN0eWxlPSJ3aWR0aDo4NSU7IDsiPjx0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b2xvcjojZmZmZmZmOyB3aWR0aDoz MCU7Ij5KYW1lcyBFYXJsIEpvbmVzPC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJiYWNrZ3Jv dW5kLWNvbG9yOiNlZWU7IHBhZGRpbmctbGVmdDoxMHB4OyB3aWR0aDoyMCU7ICI+PGI+SnVyaXNk aWN0aW9uLCBTdGF0ZTo8L2I+PC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJwYWRkaW5nLWxl ZnQ6MTBweDsgYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmOyB3aWR0aDozMCU7Ij5UZXN0IENvdW50 eSwgSW93YTwvdGQ+PC90cj48dHI+PHRkIGFsaWduPSJsZWZ0IiBzdHlsZT0iYmFja2dyb3VuZC1j b2xvcjojZWVlOyBwYWRkaW5nLWxlZnQ6MTBweDsgd2lkdGg6MjAlOyAiPjxiPkFzc2Vzc29yIFBo b25lOjwvYj48L3RkPjx0ZCBhbGlnbj0ibGVmdCIgc3R5bGU9InBhZGRpbmctbGVmdDoxMHB4OyBi

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

1 个答案:

答案 0 :(得分:1)

嗯,您似乎打开了 Price_Estimate.csv ,但随后在 V_A_Price_Estimate.csv 上调用了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()存在的东西 - 作为一个开始。