PHP code example of laikait / laika-mailman

1. Go to this page and download the library: Download laikait/laika-mailman library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laikait / laika-mailman example snippets


use Laika\Mailman\Mailer;

$mailer = new Mailer([
    'driver'     => 'smtp',
    'host'       => 'smtp.gmail.com',
    'port'       => 587,
    'username'   => '[email protected]',
    'password'   => 'app-password',
    'encryption' => 'tls',
    'from'       => '[email protected]',
    'from_name'  => 'Your App',
]);

$mailer->to('[email protected]', 'Someone')
       ->subject('Monthly report')
       ->body('<h1>Report</h1><p>Attached.</p>', 'Report — attached.')
       ->attach('/path/to/report.pdf')
       ->send();

$results = $mailer->sendMany([
    fn (Mailer $m) => $m->to('[email protected]')->subject('Hi')->text('One.'),
    fn (Mailer $m) => $m->to('[email protected]')->subject('Hi')->text('Two.'),
]);
// [0 => true, 1 => true] — one failure doesn't abort the rest of the batch

$mailer = Mailer::fromDsn('smtp://user:[email protected]:587?encryption=tls');

$mailer->xmailer('My App 2.0');                // X-Mailer: My App 2.0
$mailer->xmailer(Mailer::XMAILER_NONE);        // no X-Mailer header at all
$mailer->xmailer(Mailer::XMAILER_PHPMAILER);   // PHPMailer's default, version and all

use Laika\Mailman\Reader\ImapReader;

$reader = new ImapReader([
    'host'       => 'imap.gmail.com',
    'port'       => 993,
    'encryption' => 'ssl',
    'username'   => '[email protected]',
    'password'   => 'app-password',
    'folder'     => 'INBOX',
]);

$reader->connect();

foreach ($reader->mailboxes() as $mailbox) {
    echo $mailbox->name, "\n";
}

$uids = $reader->search([
    'unseen'  => true,
    'since'   => new DateTimeImmutable('-7 days'),
    'from'    => '[email protected]',
]);

foreach ($uids as $uid) {
    $message = $reader->fetch($uid);

    echo $message->subject, ' — ', $message->fromAddress(), "\n";

    foreach ($message->files() as $attachment) {
        $attachment->saveTo('/var/mail-attachments');
    }
}

$reader->disconnect();

use Laika\Mailman\Reader\Pop3Reader;

$reader = new Pop3Reader([
    'host'       => 'pop.gmail.com',
    'port'       => 995,
    'encryption' => 'ssl',
    'username'   => '[email protected]',
    'password'   => 'app-password',
]);

$reader->connect();

echo $reader->count(), " messages\n";

// TOP n 0 — headers only, far cheaper than RETR for building a listing
foreach (array_keys($reader->listing()) as $number) {
    echo $reader->headers($number)->subject, "\n";
}

$message = $reader->fetch(1);   // full RETR
$reader->disconnect();

$message->subject;          // string, decoded
$message->from;             // ['email' => ..., 'name' => ...]
$message->to;               // list of the same
$message->date;             // ?DateTimeImmutable
$message->textBody;         // text/plain part
$message->htmlBody;         // text/html part
$message->body();           // htmlBody if present, else textBody
$message->attachments;      // Attachment[], including inline cid: parts
$message->files();          // Attachment[], excluding inline parts
$message->flags;            // IMAP only
$message->header('x-spam-score');

use Laika\Mailman\Pipeline\Pipeline;
use Laika\Mailman\Pipeline\ScanResult;
use Laika\Mailman\Pipeline\Source\ImapSource;

$pipeline = (new Pipeline())
    ->onIdentifier('ticket', fn (ScanResult $r) => $tickets->appendReply($r->first('ticket'), $r->message))
    ->onIdentifier('invoice', fn (ScanResult $r) => $billing->attach($r->first('invoice'), $r->message))
    ->otherwise(fn (ScanResult $r) => $tickets->open($r->message))
    ->afterProcessing(Pipeline::MOVE, 'Processed');

$stats = $pipeline->run(new ImapSource($reader, ['unseen' => true]));
// ['scanned' => 12, 'handled' => 10, 'unmatched' => 2, 'failed' => 0]

$pipeline->extract('customer', '/\bCUST-(\d{4,})/i')
         ->extract('ticket', '/\bSUP-(\d+)/i', [Extractor::SUBJECT]);  // replaces the preset

$r->first('ticket');            // '5150' — best available
$r->all('ticket');              // ['5150', '991', '1234'] — every hit, best first
$r->confidenceOf('ticket');     // 'header'
$r->isTrusted('ticket');        // true for header/recipient only

->otherwise(function (ScanResult $r) use ($tickets) {
    if ($r->isReply() && $t = $tickets->findByMessageIds($r->threadIds)) {
        return $t->appendReply($r->message);
    }
    $tickets->open($r->message);
})

$result = $pipeline->scan(MimeParser::parseMessage($rawEmail));
assert($result->first('ticket') === '1234');

$mailer->oauth($yourTokenProvider);