Jump to content


PHP not attaching full csv file

  • Please log in to reply
1 reply to this topic

#1 davidb2002

  • Members
  • Pip
  • Newbie
  • 1 posts

Posted 02 March 2006 - 04:49 PM

I have a script that uses fopen to open a csv file, replace the text within it with 2 lines. Then we use PHPMailer to send the file as an attachement via email. Doing this, the csv only contains about 1/4th of the content, yet when I view the csv file on the server, it contains all the information. If I just attach without the fopening and all, it attaches the full file.

So I thought that it might be a timing issue, maybe i was attaching the file before php could complete writing to it, so I paused it for 5 seconds, then 10 seconds, no progress, it still stops at the same point.

Can anyone explain what is going on?

Here is the coding:

    $headerline = '"slno","trackingCode","currdate","currtime","referField","amtRequired","loanPurpose","fName","mName","lName","dob","maritalStatus","empStatus","postCode","address","area","town","hlongyears","hlongmonths","hometype","telephone","mobile","wnumber","email","contacttime","numbpref","jointapplication","occupation","grossincome","s2pcode","s2address","s2area","s2town","s2empstatus","s2occupation","s2grossincome","s2houseworth","s2mortage","s2findus","s2ppi","s2fName","s2mName","s2lName","s2dob","s2empstatus1","s2occupation1","s2grossincome1","firstAppEmpName","secondAppEmpName","firstAppEmpAddr","secondAppEmpAddr","firstAppEmpNo","secondAppEmpNo","firstAppEmpDur","secondAppEmpDur","firstAppPrevEmpName","secondAppPrevEmpName","firstAppPrevEmpAddr","secondAppPrevEmpAddr","firstAppPrevEmpDur","secondAppPrevEmpDur","bankName","bankAddr","hlYears","hlMonths","children","mrp","mct","chkCredit","type1","name1","amount1","monthlypymt1","settle1","type2","name2","amount2","monthlypymt2","settle2","type3","name3","amount3","monthlypymt3","settle3","type4","name4","amount4","monthlypymt4","settle4","loan_archive","loan_delete","stageform","app_status","app_lock","call_attd","app_stat_feed","app_attd_date","addn_addr","ppi","addn_info","app_attd_time","app_comp_stat","loanTerm","keywords"';

    $headerline = $headerline."\r\n";

    $norton = mysql_query("SELECT * FROM email WHERE lender='Nor'") or die(mysql_error());
    $nrow = mysql_fetch_array($norton);
    $ncount = $nrow['count'];
    $number = $ncount + 1;
    mysql_query("UPDATE email SET count='$number' WHERE lender='Nor'") or die(mysql_error());

    $ndate = date("d/m/y");
    $ntime = date("H:i:s");

    $entry_line = '"'.$number.'","test","'.$ndate.'","'.$ntime.'","","'.$amount.'","'.$reason.'","'.$name.'","","'.$surname.'","'.$dob.'","'.$marital.'","'.$empstatus.'","'.$postcode.'","'.$addline1.'","'.$addline2.'","'.$addline3.'","","","homeowner","'.$homephone.'","'.$mobilephone.'","'.$workphone.'","'.$email.'","","","","'.$occupation.'","'.$monthlyincome.'","","","","","","","","'.$propvalue.'","'.$mortout.'","","","","","","","","","","","","","","","","'.$emptime.'","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","new","","","","","","","","","","0",""';

    $content = $headerline.$entry_line;

    //add header line to csv
    $fp = fopen("export.csv", "w");
    fputs($fp, $content);


$mail = new PHPMailer();
$mail->IsMAIL(); // telling the class to use SMTP
$mail->From = "mu@email.com";

$mail->Subject = "Test CSV";
$mail->Body = "This is a test CSV";
$mail->WordWrap = 50;

$filename = $number.".csv";
   echo "Message was not sent";
   echo "Mailer Error: " . $mail->ErrorInfo;
   echo "Message has been sent";

#2 ken311

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 30 April 2008 - 02:54 AM

I suspect this may have to do with PHPMailer, try using this example:

//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";

Taken from: http://www.webcheats..._attachment.php
http://www.dzineit.net - Web Development

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users