I'm attempting to have a script that when complete, will access my email account, add all the email addresses of senders to a database (just a text file for now), and mail the list out once a week. At the moment, however, I am stuck.
I'm using put_file_contents to try and add these addresses to a text file, but it only writes one email address. My code is as follows.
<?php
$imap = imap_open("{imap.gmail.com:993/imap/ssl}INBOX","xxx@gmail.com", "xxx" );
$message_count = imap_num_msg($imap);
for ($i = 1; $i <= $message_count; ++$i) {
$header = imap_header($imap, $i);
if (isset($header->from[0]->personal)) {
$personal = $header->from[0]->personal;
} else {
$personal = $header->from[0]->mailbox;
}
$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;
echo "$fromaddr <br>";
}
$file = 'database.txt';
$current = file_get_contents($file);
$current .= ($header->from[0]->mailbox . "@" . $header->from[0]->host);
file_put_contents($file, $current);
imap_close($imap);
?>
The echo shows all the email addresses when I load the page, but when I try to write it, it only writes the last one displayed. How do I resolve this issue?
Thank you in advance.