PHP code example of dcblogdev / imap

1. Go to this page and download the library: Download dcblogdev/imap 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/ */

    

dcblogdev / imap example snippets


use Dcblogdev\Imap\Imap;

//set search criteria
$date = date('d-M-y', strtotime('1 week ago'));
$term = 'ALL UNDELETED SINCE "'.$date.'"';

//ignore array of emails
$exclude = [];

$email    = '[email protected]';
$password = 'emailpassword';
$host     = 'outlook.office365.com';//your email host
$port     = '993';//port number
$savePath = "emails";//folder to save attachments
$markAsSeen = true;//when true mark email as been read
$delete   = false;//set to true to delete email

//initialise email
$imap = new Imap($email, $password, $host, $port, 'Inbox', $savePath, $markAsSeen, $delete);

//get emails pass in the search term and exclude array
$emails = $imap->emails($term, $exclude);

//loop over emails and display
foreach($emails as $email) {
	
	echo "Account {$email['account']}<br>";
	echo "Subject {$email['subject']}<br>";
	echo "From {$email['fromName']} ({$email['fromAddress']})<br>";
	echo "To {$email['toAddress']}<br>";
	echo "CC {$email['ccAddress']}<br>";
	echo "Date {$email['emailDate']}<br>";
	echo count($email['attachments'])." Attachments<br>";

	foreach($email['attachments'] as $attachment) {
		echo "<a href='{$attachment['file']}'>{$attachment['fileName']}</a>";
	}

	echo "<br><br>";
	if ($email['htmlBody'] !='') {
		echo $email['htmlBody'];
	} else {
		echo nl2br($email['plainBody']);
	}
	echo "<hr>";
}