0

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 protected]"; 

    //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:[email protected]\">[email protected]</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\n" . 
    "From: {$from}\n" . 
    "Cc: [email protected]\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>";
    }

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 PjxzcGFuIHN0eWxlPSJ0ZXh0LXRyYW5zZm9ybTp1cHBlcmN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IHBhZGRpbmctbGVmdDoxMHB4OyB3aWR0aDoyMCU7ICI+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
  • Please note that edits are preserved in the edit history for all to see. If you flag for moderator attention, they might remove the text from the original post for you. Commented Oct 26, 2016 at 22:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunatly that wasn't it... Was trying to do on-the-fly re-naming conventions for security of the business. Must have left them out. I have edited the naming for safety sake. On the original version code, all the .csv files where named correctly. So that wasn't the issue.
I suggest you run an if block using file_exists() before you file_open_contents to be sure the file is there. If it is, trace it through your file to the point at which you attach the data to be sure it's there. If it is, then it's a matter of troubleshooting the attachment method - which of course - a library will help you do much more smoothly.
I can certainly try and if statement. Meanwhile, can you give me an example of a library and explain or documentation on how would I use it. I am new to these libraries. Like I mention before, I tried playing with them but the libraries doesn't seem to work.
I was able to download the csv, which the file is an excel file and it downloaded all the HTML code from that one page.... Meaning I was able to code the download and have the csv to downloaded to the internet and pull it up in excel. but problem now is that it didn't read teh data content but printed the previous page html into the csv file. Oh and still not file attachment in the email, just jarble.
since there is not a file naming issue and you are not having a problem writing the file, then you should look at this duplicate question with answers both php::mail() as well as how to use a library stackoverflow.com/questions/12301358/send-attachments-with-php-mail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.