Jump to content

PHP not attaching full csv file


davidb2002

Recommended Posts

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:
[code]

    $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);
    fclose($fp);

    


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

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

$filename = $number.".csv";
$mail->AddAttachment("export.csv");
if(!$mail->Send())
{
   echo "Message was not sent";
   echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
   echo "Message has been sent";
}
[/code]
Link to comment
Share on other sites

  • 2 years later...
I suspect this may have to do with PHPMailer, try using this example:

[code]<?php
//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; ?>--

<?php
//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";
?>[/code]

Taken from: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.