PHP code example of tonicforhealth / health-checker-check-email

1. Go to this page and download the library: Download tonicforhealth/health-checker-check-email 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/ */

    

tonicforhealth / health-checker-check-email example snippets




use PhpImap\Mailbox;
use TonicHealthCheck\Check\Email\Persist\PersistCollectionToFile;
use TonicHealthCheck\Check\Email\Receive\EmailReceiveCheck;
use TonicHealthCheck\Check\Email\Send\EmailSendCheck;

$checkNode = 'testnode';

$persistCollectionToFile = new PersistCollectionToFile(sys_get_temp_dir());

$transport = Swift_SmtpTransport::newInstance();

$mailer = Swift_Mailer::newInstance($transport);

$mailbox = new Mailbox('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', 'username', 'password');

$receiveMaxTime = 300;

$sendInterval = 600;

$emailSendCheck = new EmailSendCheck(
    $checkNode,
    $mailer,
    $persistCollectionToFile,
    '[email protected]',
    '[email protected]',
    $sendInterval
);

$emailReceiveCheck = new EmailReceiveCheck(
    $checkNode,
    $mailbox,
    $persistCollectionToFile,
    $receiveMaxTime
);

while (true) {
    $resultSend = $emailSendCheck->performCheck();
    printf(
        "Send result is:%s\n",
        $resultSend->isOk() ? 'true' : sprintf('false error:%s', $resultSend->getError()->getMessage())
    );
    sleep(10);
    $resultReceive = $emailReceiveCheck->performCheck();

    printf(
        "Receive result is:%s\n",
        $resultReceive->isOk() ? 'true' : sprintf('false error:%s', $resultReceive->getError()->getMessage())
    );
    sleep(60);
}