Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/227239-issue-with-file_put_contents/
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.