It occurred to me that it could work if you read lines from the end of the file instead of the beginning. I got some help from here figuring out how to do that, and incorporated some of the code from an answer there into a function that seeks backward from the end of the file to the beginning of the last line.
function lineStart($file) {
$position = ftell($file);
while (fgetc($file) != "\n") {
fseek($file, --$position);
if ($position == 0) break;
}
return $position;
}
With that function, you can repeatedly read the last line end then truncate the file to the length indicated by the file position at the beginning of that line.
$file = fopen('emails.csv', 'r+'); // open for read/write
fseek($file, -1, SEEK_END); // move to end of file
while (fstat($file)['size']) {
lineStart($file); // move to beginning of line
$line = fgetcsv($file);
$mail->parseTextandSendMail($line); // do your email thing
ftruncate($file, lineStart($file)); // truncate from beginning of line
}
fclose($file);
I don't know if I think this is generally a good idea (a database seems like it would be much easier to deal with), but I just wanted to figure out if there was a way to accomplish it, and this does seem to work in my limited testing.