-1

Ok I now have the following code after adding PHPMailer:

<?php
$name = $_POST['id'];
$email = $_POST['username'];
$password = md5($_POST['password']);
$usergroup = $_POST['usergroup'];
$lastsid = $_POST['lastsid'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$lastaccess = $_POST['lastaccess'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$addone = $_POST['addone'];
$addtwo = $_POST['addtwo'];
$city = $_POST['city'];
$stateprov = $_POST['stateprov'];
$country = $_POST['country'];
$postalcode = $_POST['postalcode'];
$rescom = $_POST['rescom'];
$addbook = $_POST['addbook'];
$subscriptions = $_POST['subscriptions'];
$affilid = $_POST['affilid'];
$affilcommpct = $_POST['affilcommpct'];
$affilsalestodate = $_POST['affilsalestodate'];
$affilcommearned = $POST['affilcommearned'];
$affilcommowed = $_POST['$affilcommowed'];
$affilcommpaid = $_POST['affilcommpaid'];
$whlsalepct = $_POST['whlsalepct'];
$show_currency = $_POST['show_currency'];
$loyaltypoints = $_POST['loyaltypoints'];

$fp = fopen("feed.csv", "w+");
$savestring = $name . "," . $email . "," . $password . "," . $usergroup . "," . $lastsid . "," . $fname . "," . $lname . "," . $lastaccess . "," . $company . "," . $phone . "," . $addone . "," . $addtwo . "," . $city . "," . $stateprov . "," . $country . "," . $postalcode . "," . $rescom . "," . $addbook . "," . $subscriptions . "," . $affilid . "," . $affilcommpct . "," . $affilsalestodate . "," . $affilcommearned . "," . $affilcommowed . "," . $affilcommpaid . "," . $whlsalepct . "," . $show_currency . "," . $loyaltypoints . "\n";
fwrite($fp, $savestring);
fclose($fp);
echo "<h1>Thank you.</h1><p>We will confirm your details and send your new account details via email within the next working day.</p><p><a href='test2.php'>Back to main page</a></p>";

/* email finished file */

require_once('/public_html/xx/class.phpmailer.php');

$email = new PHPMailer();
$email->From      = 'my@emailaddress;
$email->FromName  = 'test name';
$email->Subject   = 'Form Test';
$email->Body      = $bodytext;
$email->AddAddress( 'my@emailaddress' );

$file_to_attach = '/public_html/xx/';

$email->AddAttachment( $file_to_attach, 'formdata.csv' );

return $email->Send();

?>

Nothing happens! Please could anyone advise? I am now getting no error logs

I have also tried to write $searchstring as fputcsv() with no success. I need to workaround this as I do have one field for checkboxes where the options are separated by comma.

Please help, I really do appreciate your feedback. :)

debonator
  • 85
  • 2
  • 13
  • you are going to end up with corrupted csv files, since you're dumping raw user-provided text into the file. e.g. what happens if someone's last name is entered as `Smith, Esquire`? you should use [fputcsv()](http:///php.net/fputcsv) instead. – Marc B Oct 13 '14 at 11:46
  • As said by @Marc you have to process user data before dumping it directly it may contain any thing. Below answer also does not solve the problem. Please do as suggested by Marc – Vickrant Oct 13 '14 at 11:51
  • Thanks guys. So what you are saying is instead of using $fwrite, use fputcsv. I have got an issue where checkboxes are separated with a comma, but I haven't got to that issue yet. – debonator Oct 13 '14 at 12:26
  • I have tried with this, where would I add it? – debonator Oct 13 '14 at 13:13
  • Are you only adding 1 record in your CSV file? If only one record, it would be better if you'll send it as an html content not CSV. But if you really need it to be in CSV file, please read this post, http://stackoverflow.com/questions/15501463/creating-csv-file-with-php – user1149244 Oct 13 '14 at 14:55
  • It needs to be sent in CSV format as it is being imported elsewhere.. Thanks – debonator Oct 13 '14 at 15:22

1 Answers1

0

Since you have posted a very little details - I can suggest then following:

Instead of this - which always write on same file:

$fp = fopen("formdata.csv", "w+");

Try this:

$date_time = date('Y-m-d H:i:s');
$fp        = fopen("formdata" . $date_time . ".csv", "w+");

And after fclose($fp);:

Trigger email code and send the currently generated CSV file as attachment.

Hope that helps. Let me know, if you want to know further.

  • @debonator I assume that you are properly generating CSV file and you know how to send the CSV file in email - as you did not mention about these 2 points. –  Oct 13 '14 at 11:49
  • Sorry if I wasn't clear: I would like to know how to send the CSV file as an attachment to my colleague's email address. Currently the file does overwrite the new entry, but I would like for the file to be specific. Does that help? – debonator Oct 13 '14 at 12:30
  • @debonator From above answer you got clear that how to clear unique CSV file each time? –  Oct 13 '14 at 12:31
  • Let me show you how to send CSV file in email attachment now. –  Oct 13 '14 at 12:32
  • Please visit [this](http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail/12302354#12302354). Let me know if it still does not clear. –  Oct 13 '14 at 12:33
  • Not working. Following from: fclose($fp); echo "

    Thank you.

    "; /* email finished file */ require_once('/public_html/xxfile/class.phpmailer.php'); $email = new PHPMailer(); $email->From = 'my@emailaddress'; $email->FromName = 'John smith'; $email->Subject = 'Form Test'; $email->Body = $bodytext; $email->AddAddress( 'my@emailaddress.com' ); $file_to_attach = '/public_html/xx/'; $email->AddAttachment( $file_to_attach, 'formdata.csv' ); return $email->Send();
    – debonator Oct 13 '14 at 13:08
  • @debonator Cool. Please mark my answer as correct if I could help you a bit. –  Oct 13 '14 at 13:09
  • Sorry webline I haven't got it. I'm don't use this often so still getting to grips. (comment is above). – debonator Oct 13 '14 at 13:11
  • What error you are getting now? Have downloaded the PHPMailer class form GIT? –  Oct 13 '14 at 13:15
  • Shall i post everything I have at the moment? The email function isn't working – debonator Oct 13 '14 at 13:30
  • Edit your answer and put the new things which you did. Also please format the code too. –  Oct 13 '14 at 13:32
  • I marked your answer correct weblineIndia but the naming new file didn't work. Although anyone who falls into the same situation I did theres a great set of tutorials on PHPMailer on YouTube https://www.youtube.com/watch?v=CizDkNNcLRg&list=PLfdtiltiRHWEM8SBc7fahXQJrUy9xmFYW&index=1 Thanks for oyur help! – debonator Oct 13 '14 at 21:20