jornatf/msgraph-mailer

A Laravel Package to send emails with Microsoft Graph API.

v2.0.1 2023-04-22 15:59 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A Laravel Package to send emails with Microsoft Graph API.

If you like this package you can Buy me a Coffee ☕️

Installation

Via composer:

composer require jornatf/msgraph-mailer

Prerequisites:

Add your Microsoft Graph credentials in the .env file:

MSGRAPH_CLIENT_ID=your_client_id
MSGRAPH_SECRET_ID=your_secret_id
MSGRAPH_TENANT_ID=your_tenant_id

Usage

Example:

This example shows you how to use the basic methods required to send an email.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class PostController extends Controller
{
    public function sendMail()
    {
        try {
            $msgraph = MsGraphMailer::mail('your@mailbox.com')
                ->to(['John Doe:john.doe@example.com', 'other@example.com', $otherRecipients])
                ->subject('A simple email')
                ->body('<h1>Send a simple email to one recipient.</h1>')
                ->send();
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        return "Email sended!";
    }
}

Available methods:

<?php

// First, instantiate response with your Msgraph mailbox (required):
$msgraph = MsGraphMailer::mail(string $mailbox);

// Add a main recipient(s), a subject and a content (required):
$msgraph->to(array $toRecipient);
$msgraph->subject(string $subject);
$msgraph->content(string $contentHTML);

// You can add Cc and/or bcc with the same way (optionally):
$msgraph->cc(array $ccRecipient);
$msgraph->bcc(array $bccRecipient);

// Optionally, you can add one or many attachments:
$msgraph->addAttachment(array $attachment = [
    'name' => $filename,
    'contentType' => $contentType,
    'content' => $contentBytes
]);
// and repeat if you have many attachments.

// Last, send your mail:
$msgraph->send();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Feel free to contribute to this project to improve with new features or fix bugs 👍

Credits

License

The MIT License (MIT).

Please see License File for more information.