Migrate emails from one gmail account to another gmail account by imap in php-ThrowExceptions

Exception or error:

Environment
– PHP Version: 7.2

Question
I am creating web app for a client and I want to copy all emails from one gmail account to another gmail account by PHP imap extension. I have searched and find out these functions imap_mail_copy and imap_mail_move but these are for single account to Move/Copy specified messages to a mailbox.
Here is the code that i have written to fetch emails:

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'mail.migrator.ovh@gmail.com';
$password = 'xxxxxxx';`

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

$emails = imap_search($inbox,'ALL');

if($emails) {
    $mailList = [];
    rsort($emails);

    foreach($emails as $email_number) 
    {
        $mailList[] = $email_number;
    }
    $mailListStr = implode(',', $mailList);
}
imap_close($inbox);
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *