PHP code example of ezstoritve / m365-mail

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

    

ezstoritve / m365-mail example snippets


'mailers' => [
    'm365-mail' => [
        'transport' => 'm365-mail',
        'tenant_id' => env('MICROSOFT_GRAPH_TENANT_ID'),
        'client_id' => env('MICROSOFT_GRAPH_CLIENT_ID'),
        'client_secret' => env('MICROSOFT_GRAPH_CLIENT_SECRET'),
        'from_address' => env('MAIL_FROM_ADDRESS'),
        'from_name' => env('MAIL_FROM_NAME')
    ],
    ...
]

Mail::send('blade.file', [], function ($m) {
    $m->to('[email protected]', 'Recipient Name')
        ->subject('Mail subject')
        ->getHeaders()->addTextHeader('X-Save', 'true'); // save an email to the sent items folder - optional
});

$folderPath = 'Inbox\Folder1';
$mailbox = '[email protected]';

$result = Mail::read([
    MailReadParams::FolderPath => $folderPath,
    MailReadParams::Mailbox => $mailbox,
    MailReadParams::GetFiles => false
], function ($emails) {
    $output = '';
    foreach ($emails as $email) {
        $output .= '<h3>' . htmlspecialchars($email['subject']) . '</h3>';
        $output .= '<p>From: ' . htmlspecialchars($email['fromName']) . ' (' . htmlspecialchars($email['from']) . ')</p>';
        $output .= '<p>To: ' . htmlspecialchars(implode(', ', array_column($email['to'], 'address'))) . '</p>';
        $output .= '<p>CC: ' . htmlspecialchars(implode(', ', array_column($email['cc'], 'address'))) . '</p>';
        $output .= '<p>BCC: ' . htmlspecialchars(implode(', ', array_column($email['bcc'], 'address'))) . '</p>';
        $output .= '<p>Date: ' . htmlspecialchars($email['receivedDateTime']) . '</p>';
        $output .= '<p>' . htmlspecialchars($email['bodyPreview']) . '</p>';
        if (!empty($email['attachments'])) {
            $output .= '<p>Attachments:</p><ul>';
            foreach ($email['attachments'] as $attachment) {
                $output .= '<li>' . htmlspecialchars($attachment['name']) . '</li>';
                //$output .= '<li>' . htmlspecialchars($attachment['contentBytes']) . '</li>';
            }
            $output .= '</ul>';
        }
        $output .= '<hr>';
    }
    return $output;
});

print_r($result);

$emailDetails = [
    'id' => $email['id'],
    'subject' => $email['subject'],
    'from' => $email['from']['emailAddress']['address'],
    'fromName' => $email['from']['emailAddress']['name'],
    'bodyPreview' => $email['bodyPreview'],
    'receivedDateTime' => $email['receivedDateTime'],
    'hasAttachments' => $email['hasAttachments'],
    'to' => array_map(fn($recipient) => [
        'address' => $recipient['emailAddress']['address'],
        'name' => $recipient['emailAddress']['name'],
    ], $email['toRecipients'] ?? []),
    'cc' => array_map(fn($recipient) => [
        'address' => $recipient['emailAddress']['address'],
        'name' => $recipient['emailAddress']['name'],
    ], $email['ccRecipients'] ?? []),
    'bcc' => array_map(fn($recipient) => [
        'address' => $recipient['emailAddress']['address'],
        'name' => $recipient['emailAddress']['name'],
    ], $email['bccRecipients'] ?? []),
    'attachments' => [
        0 => [
            'name' => 'file name', 
            'contentBytes' => '' // if GetFiles is set to true - you can manualy download files in Mail:read function
        ],
        ...
    ]
];