dstjh46606
dstjh46606
2016-10-26 21:01

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

已采纳

Need help with my current code below of trying to create a CSV file and then send it out as an attachment. Been having not luck in doing this process. Everytime I get close, it sends out the email, the HTML portion is working but there is no attachement, just jibble jabble code within the email. I want the CSV attachment along with the email

Note: below that $txt is a HTML table, plus contents we suppose to bring over into a CSV.

Note: I am using wordpress.

I do not want to use PHPmailer() at this moment in time, nor FPDF, MPDF or anything of those. If you do suggest PHPmailer() then please give me a really detail document or answer on how to make it work cause I tried multiple things before and no luck at this.

I have tried multiple things, but cannot seem to get it to work. I really, really need anyone help, an exclamation on what I am doing wrong and some sample code or fixed code.

My Code is below:

Here is the part where I try to create the CSV file:

 $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')));

Here is where I send out the HTML Email and CSV file:

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

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

    $message .= "This is a multi-part message in MIME format.

" . 
     "--{$mime_boundary}
" . 
    "Content-Type: text/html; charset=\"iso-8859-1\"
" . 
    "Content-Transfer-Encoding: 7bit

" . 
    $text . "

";

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

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

" . 
    $content . "

" ;

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

    // create Representative email headers and HTML part of Email Message
    //$headers = "From: ".$from. "
";
    $headers .= "MIME-Version: 1.0
";
    $headers .= "Content-Type: text/html; charset=UTF-8
";
    $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>";
    }

I have a feeling that is either one of two things, that the CSV is not working or the headers are incorrect.

Like I mention before, it prints out the message of the HTML part which is a table of the email, but it doesn't send the attachment with it. Just has all the jarble. This is an example of some of jibble jarble I am getting in the email:

  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 dGQgY29sc3Bhbj0iNCIgYWxpZ249ImNlbnRlciIgc3R5bGU9InRleHQtYWxpZ246Y2VudGVyOyIg PjxzcGFuIHN0eW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lPSJiYWNrZ3Jv dW5kLWNvbG9yOiNlZWU7IHBhZGRpbmctbGVmdDoxMHB4OyB3aWR0aDoyMCU7ICI+PGI+SnVyaXNk aWN0aW9uLCBTdGF0ZTo8L2I+PC90ZD48dGQgYWxpZ249ImxlZnQiIHN0eWxlPSJwYWRkaW5nLWxl ZnQ6MTBweDsgYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmOyB3aWR0aDozMCU7Ij5UZXN0IENvdW50 eSwgSW93YTwvdGQ+PC90cj48dHI+PHRkIGFsaWduPSJsZWZ0IiBzdHlsZT0iYmFja2dyb3VuZC1j b2xvcjojZWVlOyBwYWRkaW5nLWxlZnQ6MTBweDsgd2lkdGg6MjAlOyAiPjxiPkFzc2Vzc29yIFBo b25lOjwvYj48L3RkPjx0ZCBhbGlnbj0ibGVmdCIgc3R5bGU9InBhZGRpbmctbGVmdDoxMHB4OyBi

What am I doing wrong. Please help me understand this sending email and attachment better. Please, I need your help!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duan1979768678 duan1979768678 5年前

    Hmm, it seems that you fopen Price_Estimate.csv but then call file_get_contents on V_A_Price_Estimate.csv which is a file that your code does not prove exists.

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

    ...

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

    ...

    It's valid to call it whatever you will when you send it as the attachment, but right now, you're opening a file that (seemingly) does not exist. So, this part is okay (for an on-the-fly renaming anyway):

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

    If there is something else wrong, I don't know at this point. But you have to make sure you file_get_contents() something that exists - as a start.

    点赞 评论 复制链接分享

相关推荐