unyii2/yii2-imap

yii2 extension to read and process mails from IMAP and PHP

Installs: 36 087

Dependents: 1

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 33

Open Issues: 0

Type:yii2-extension

0.5.4 2019-05-08 16:07 UTC

This package is auto-updated.

Last update: 2024-04-07 19:22:52 UTC


README

Total Downloads

This library is a fork of https://github.com/yiioverflow/yii2-imap

Installation by composer

{
    "require": {
       "unyii2/yii2-imap": "dev-master"
    }
}

Or

$ composer require unyii2/yii2-imap "dev-master"

Use as compnent

Connection details define in component

'components' => [
      ...
      'imap' => [
         'class' => 'unyii2\imap\Imap',
         'connection' => [
              'imapPath' => '{imap.gmail.com:993/imap/ssl}INBOX',
              'imapLogin' => 'username',
              'imapPassword' => 'password',
              'serverEncoding'=>'encoding', // utf-8 default.
              'attachmentsDir'=>'/'
        ],
    ],
    ...
 ],


//4th Param _DIR_ is the location to save attached files 
//Eg: /path/to/application/mail/uploads.
$mailbox = new unyii2\Mailbox(yii::$app->imap->connection);

Usage as library

Connection details set on fly

$imapConnection = new unyii2\imap\ImapConnection

$imapConnection->imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
$imapConnection->imapLogin = 'username';
$imapConnection->imapPassword = 'password';
$imapConnection->serverEncoding = 'encoding'; // utf-8 default.
$imapConnection->attachmentsDir = '/';


//4th Param _DIR_ is the location to save attached files 
//Eg: /path/to/application/mail/uploads.
$mailbox = new unyii2\Mailbox($imapConnection);

#To get all mails and its index

$mailbox->searchMailBox(ALL)// Prints all Mail ids.
print_r($mailIds);

#Do not read attachments $mailbox->readMailParts = false;

#To read Inbox contents

foreach($mailIds as $mailId)
{
    // Returns Mail contents
    $mail = $mailbox->getMail($mailId); 

    if(alreadyProcesedMessage($mail->messageId)){
        continue;
    }

    // Use, if $mailbox->readMailParts = false; 
    // Read mail parts (plain body, html body and attachments
    $mail = $mailbox->getMailParts($mail);

    // Returns mail attachements if any or else empty array
    $attachments = $mail->getAttachments(); 
    foreach($attachments as $attachment){
        echo ' Attachment:' . $attachment->name . PHP_EOL;
        
        // Delete attachment file
        unlink($attachment->filePath);

    }
}

#To Mark and delete mail from IMAP server.

//Mark a mail to delete</span></span> 
$mailbox->deleteMail($mailId); // Deletes all marked mails
$mailbox->expungeDeletedMails();

Contribute

Feel free to contribute. If you have ideas for examples, add them to the repo and send in a pull request.

Apreciate

Dont forgett o Leave me a "star" if you like it. Enjoy coding!