barbushin / php-imap

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
MIT License
1.65k stars 459 forks source link

Need to get spam messages list #648

Closed adrianbecker013 closed 2 years ago

adrianbecker013 commented 2 years ago

How can I get spam emails in mail.com and zohomail providers ? Using this demo I get properly inbox mails of all providers. But I can not able to find spam folder's mails.

Can you please let me know is it possible in IMAP or not. Please let me know how can I get spam emails.

Sebbo94BY commented 2 years ago

First, you need to figure out, how the folders are named at these mail servers. This differs sometimes from mail server to mail server.

You can get a list of all your mailbox folders using this code:

// get the list of folders/mailboxes
$folders = $mailbox->getMailboxes('*');

// loop through mailboxs
foreach($folders as $folder) {
    // Print full path of mailbox folder
    echo $folder['fullpath'] . "\n";
}

Then it should be as simple as this:

$mailbox = new Mailbox(
    '{imap.gmail.com:993/imap/ssl}Spam', // IMAP server and mailbox folder
    'some@gmail.com', // Username for the before configured mailbox
    '*********', // Password for the before configured username
    __DIR__, // Directory, where attachments will be saved (optional)
    'US-ASCII' // Server encoding (optional)
);

Simply set the the mailbox folder to the respective folder name. In the above sample for example to Spam.

Alternatively, you can change after connecting to your e.g. Inbox folder to the respective folder:

$mailbox->switchMailbox("Spam");

Please note, that the folder, which contains your spam / junk mails can be named differently. Common names are Spam and Junk.

I hope, this helps you. :)


Full examples:

// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
    '{imap.gmail.com:993/imap/ssl}', // IMAP server and mailbox folder
    'some@gmail.com', // Username for the before configured mailbox
    '*********', // Password for the before configured username
    __DIR__, // Directory, where attachments will be saved (optional)
    'UTF-8', // Server encoding (optional)
    true, // Trim leading/ending whitespaces of IMAP path (optional)
    false // Attachment filename mode (optional; false = random filename; true = original filename)
);

// get the list of folders/mailboxes
$folders = $mailbox->getMailboxes('*');

// loop through mailboxs
foreach($folders as $folder) {
    // Print full path of mailbox folder
    echo $folder['fullpath'] . "\n";
}
// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
    '{imap.gmail.com:993/imap/ssl}Spam', // IMAP server and mailbox folder
    'some@gmail.com', // Username for the before configured mailbox
    '*********', // Password for the before configured username
    __DIR__, // Directory, where attachments will be saved (optional)
    'UTF-8', // Server encoding (optional)
    true, // Trim leading/ending whitespaces of IMAP path (optional)
    false // Attachment filename mode (optional; false = random filename; true = original filename)
);

try {
    $mail_ids = $mailbox->searchMailbox('ALL');
} catch (ConnectionException $ex) {
    exit('IMAP connection failed: '.$ex->getMessage());
} catch (Exception $ex) {
    exit('An error occured: '.$ex->getMessage());
}

foreach ($mail_ids as $mail_id) {
    echo "+------ P A R S I N G ------+\n";

    $email = $mailbox->getMail(
        $mail_id, // ID of the email, you want to get
        false // Do NOT mark emails as seen (optional)
    );

    echo 'from-name: '.(string) ($email->fromName ?? $email->fromAddress)."\n";
    echo 'from-email: '.(string) $email->fromAddress."\n";
    echo 'to: '.(string) $email->toString."\n";
    echo 'subject: '.(string) $email->subject."\n";
    echo 'message_id: '.(string) $email->messageId."\n";
}
// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
    '{imap.gmail.com:993/imap/ssl}', // IMAP server and mailbox folder
    'some@gmail.com', // Username for the before configured mailbox
    '*********', // Password for the before configured username
    __DIR__, // Directory, where attachments will be saved (optional)
    'UTF-8', // Server encoding (optional)
    true, // Trim leading/ending whitespaces of IMAP path (optional)
    false // Attachment filename mode (optional; false = random filename; true = original filename)
);

$mailbox->switchMailbox("Spam");

try {
    $mail_ids = $mailbox->searchMailbox('ALL');
} catch (ConnectionException $ex) {
    exit('IMAP connection failed: '.$ex->getMessage());
} catch (Exception $ex) {
    exit('An error occured: '.$ex->getMessage());
}

foreach ($mail_ids as $mail_id) {
    echo "+------ P A R S I N G ------+\n";

    $email = $mailbox->getMail(
        $mail_id, // ID of the email, you want to get
        false // Do NOT mark emails as seen (optional)
    );

    echo 'from-name: '.(string) ($email->fromName ?? $email->fromAddress)."\n";
    echo 'from-email: '.(string) $email->fromAddress."\n";
    echo 'to: '.(string) $email->toString."\n";
    echo 'subject: '.(string) $email->subject."\n";
    echo 'message_id: '.(string) $email->messageId."\n";
}
adrianbecker013 commented 2 years ago

I need to know Is there any function that can return all emails (inbox emails, spam emails) with particular folder name.

For example : In My mail box I have 2 email 1st is in inbox and 2nd is in spam folder Then I need function return like below : Content : Mail one content , folder name : Inbox Content : Mail two content , folder name : Spam

Can you please let me know is it possible in IMAP or not.

Sebbo94BY commented 2 years ago

Each email has multiple properties, which can be accessed using this format: $email->PROPERTY_NAME

So using the above example code and your requirements, this should answer your question: $email->mailboxFolder

See https://github.com/barbushin/php-imap/blob/master/src/PhpImap/IncomingMailHeader.php#L21 for further details and all available properties.

adrianbecker013 commented 2 years ago

Hello there,

Actually I need all mails not only inbox or not only spam. Is there any function parameter for get whole messages of given user email.

Not like : For Spam : $mailbox->switchMailbox("Spam"); For Inbox : {imap.gmail.com:993/imap/ssl}INBOX

I need all mail list. Can You please help me out ?

Sebbo94BY commented 2 years ago

PHP's imap_ functions require to be in a specific mailbox folder for searching for specific emails, so you need to connect to the users email account, get a list of available mailbox folders and then you need to search (get) all emails, which you want in every single mailbox folder.

Below, you'll see an (untested) example code:

// Create PhpImap\Mailbox instance for all further actions
$mailbox = new PhpImap\Mailbox(
    '{imap.gmail.com:993/imap/ssl}', // IMAP server and mailbox folder
    'some@gmail.com', // Username for the before configured mailbox
    '*********', // Password for the before configured username
    __DIR__, // Directory, where attachments will be saved (optional)
    'UTF-8', // Server encoding (optional)
    true, // Trim leading/ending whitespaces of IMAP path (optional)
    false // Attachment filename mode (optional; false = random filename; true = original filename)
);

// get the list of folders/mailboxes
$folders = $mailbox->getMailboxes('*');

// loop through mailboxs
foreach($folders as $folder) {
    // Change mailbox folder
    $mailbox->switchMailbox($folder['shortpath']);

    // Get 'ALL' emails from the current mailbox folder
    try {
        $mail_ids = $mailbox->searchMailbox('ALL');
    } catch (ConnectionException $ex) {
        exit('IMAP connection failed: '.$ex->getMessage());
    } catch (Exception $ex) {
        exit('An error occured: '.$ex->getMessage());
    }

    // Get data / information from each email
    foreach ($mail_ids as $mail_id) {
        echo "+------ P A R S I N G ------+\n";

        $email = $mailbox->getMail(
            $mail_id, // ID of the email, you want to get
            false // Do NOT mark emails as seen (optional)
        );

        echo 'Content : '.$email->textPlain.' , folder name : '.$email->mailboxFolder.'\n';
    }
}

$mailbox->disconnect();

This should return you something like your example:

Content : Mail one content , folder name : Inbox
Content : Mail two content , folder name : Spam
adrianbecker013 commented 2 years ago

Hello there, I need to mark email as read once I got this in output.

As I pass here in getMail() second parameter as true, $email = $mailbox->getMail( $mail_id, // ID of the email, you want to get true // Do NOT mark emails as seen (optional) );

Output : Content : Mail one content , folder name : Inbox

Then in second time If I filter email as UNSEEN using $Mailbox->searchMailbox(' UNSEEN'); Here I got that mail. This should not come. Then also get same result.

Output : Content : Mail one content , folder name : Inbox Output : Content : Mail two content , folder name : Inbox

But I need is only second as an output : Output : Content : Mail two content , folder name : Inbox

As this is SEEN email => Output : Content : Mail one content , folder name : Inbox

I need to set SEEN above mail and get another UNSEEN email not this one.

Can you please send me solution ?

Sebbo94BY commented 2 years ago

This should just work like you showed. You also can try to mark it afterwards as read/seen: $mailbox->markMailAsRead($mail_id);

What does the email property isSeen return? print_r($email->isSeen)

Did you also check your emails, if they really got marked as seen or not?

Is the filter except of this single email returning the expected emails?

Here is a full working example: https://github.com/barbushin/php-imap/blob/master/examples/get_and_parse_unseen_emails.php

adrianbecker013 commented 2 years ago

Hello there,

Can I get ToAddress in below function ?

foreach ($mail_ids as $mail_id) { echo "+------ P A R S I N G ------+\n";

$email = $mailbox->getMail(
    $mail_id, // ID of the email, you want to get
    false // Do NOT mark emails as seen (optional)
);
echo 'to: '.(string) $email->toString."\n";
echo 'toAddress: '.(string) $email->toAddress."\n";  

}

Here toAddress not working

Is there any way to find that ?

Sebbo94BY commented 2 years ago

All available properties can be found here: https://github.com/barbushin/php-imap/blob/master/src/PhpImap/IncomingMailHeader.php

I'll close this issue now since your initial question has been answered.

Please keep in mind, that we are only offering support questions specific to this library. We do not provide support for development basics.