Giter Site home page Giter Site logo

laravel-msgraph-mail's Introduction

Laravel Microsoft Graph Mail Driver Package

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

This package provides a Microsoft Graph mail driver for Laravel. It is an alternative when you don't want to use the deprecated and unsecure Basic Auth SMTP driver with Microsoft Office 365.

Installation

You can install the package via composer:

composer require innoge/laravel-msgraph-mail

Configuration

Register the Azure App

Microsoft Azure AD Configuration

I have written a detailed Blog Post how you can configure your Microsoft Azure AD Tenant. Sending Mails with Laravel and Microsoft Office 365 the secure way

I want to figure it out on my own

You need to register an Azure App in your Azure AD tenant. You can do this by following the steps in the Microsoft Graph documentation.

After creating the App you have to add the following permissions to the App: Mail.Send (Application permission) you will find it under the "Microsoft Graph" section.

Now you have to Grant Admin Consent for the App. You can do this by following the steps in the Microsoft Graph documentation.

Configuring your Laravel app

First you need to add a new entry to the mail drivers array in your config/mail.php configuration file:

'microsoft-graph' => [
    'transport' => 'microsoft-graph',
    'client_id' => env('MICROSOFT_GRAPH_CLIENT_ID'),
    'client_secret' => env('MICROSOFT_GRAPH_CLIENT_SECRET'),
    'tenant_id' => env('MICROSOFT_GRAPH_TENANT_ID'),
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS'),
        'name' => env('MAIL_FROM_NAME'),
    ],
    'save_to_sent_items' =>  env('MAIL_SAVE_TO_SENT_ITEMS', false),
],

For the client_id, client_secret and tenant_id you need to use the values from the Azure App you created in the previous step.

The save_to_sent_items option in Microsoft Graph refers to a parameter that determines whether a sent email should be saved to the sender's "Sent Items" folder within their mailbox. When this option is set to true, the email will be automatically saved to the "Sent Items" folder, providing a record of the communication. Conversely, when it's set to false, the email will not be saved to the "Sent Items" folder.

By default, the save_to_sent_items option is set to false, which means that emails sent through Microsoft Graph won't be saved in the sender's "Sent Items" folder unless explicitly specified otherwise. This behavior can be useful in scenarios where you might want more control over which emails are saved as sent items, perhaps to reduce clutter or ensure confidentiality.

Now you can switch your default mail driver to the new microsoft-graph driver by setting the env variable:

MAIL_MAILER=microsoft-graph

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-msgraph-mail's People

Contributors

geisi avatar dependabot[bot] avatar spawnia avatar joskolenberg avatar

Stargazers

Daniel, Petrica Andrei-Daniel avatar Cary avatar Damon Bakker avatar Derek Buntin avatar  avatar Daniele Sesoldi avatar Yu Feng avatar Matt Smallman avatar  avatar Noah Gillard avatar David Palomares avatar Haneef Ansari avatar Fire Department avatar Filip Petrovic avatar ju5t avatar  avatar Glenn Hermans avatar Azad Furkan ŞAKAR avatar  avatar Palaniappan P avatar Nils Domin avatar Manik Mia avatar  avatar  avatar Jeremy Robertson avatar moneya avatar

Watchers

 avatar Nils Domin avatar  avatar

laravel-msgraph-mail's Issues

Getting Error In Laravel 10 [Bug]:

What happened?

I'm getting this error when trying to send an email out.

2023-10-25 03:30:13] local.ERROR: HTTP request returned status code 500:
{"error":{"code":"ErrorInternalServerError","message":"An internal server error occurred. The operation failed."}}
{"exception":"[object] (Illuminate\Http\Client\RequestException(code: 500): HTTP request returned status code 500:
{"error":{"code":"ErrorInternalServerError","message":"An internal server error occurred. The operation failed."}}

How to reproduce the bug

try to send email

Package Version

latest

PHP Version

8.2

Laravel Version

10.0

Which operating systems does with happen with?

macOS

Notes

2023-10-25 03:30:13] local.ERROR: HTTP request returned status code 500:
{"error":{"code":"ErrorInternalServerError","message":"An internal server error occurred. The operation failed."}}
{"exception":"[object] (Illuminate\Http\Client\RequestException(code: 500): HTTP request returned status code 500:
{"error":{"code":"ErrorInternalServerError","message":"An internal server error occurred. The operation failed."}}

[Bug]: Name not handled in "reply to" and recipients

What happened?

When couples (email, name) are provided in message, only email addresses are kept in the final message sent, except for e-mail addresses known by the tenant, for which the name known in tenant is used.

Seems that problem can be solved by adding one line 'name' => $address->getName(), in function transformEmailAddress() of MicrosoftGraphTransport.php

protected function transformEmailAddress(Address $address): array
    {
        return [
            'emailAddress' => [
                'address' => $address->getAddress(),
                'name' => $address->getName(), 
            ],
        ];
    }

How to reproduce the bug

Send e-mail with recipient or "reply to" not known by the tenant

Package Version

1.3.0

PHP Version

8.2.11

Laravel Version

11.6.0

Which operating systems does with happen with?

macOS, Linux

Notes

No response

[Bug]: How do we use this?

What happened?

THis is not a bug but I cannot ask anywhere.. I am looking to implement this on laravel and I did not see any example of how to implement this, esp on the laravel side (controller). If you can help in any way, please let me know. Thanks a lot. I just need a sample of Sending a basic email.

How to reproduce the bug

Not a bug..

Package Version

2.0

PHP Version

8.2

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

[Bug]: From settings of email or notification are ignored

What happened?

I have set the email from address in my .env file to [email protected] . When I send an email where I have specified another from address with ->from('[email protected]') the email is still sent from the email address that was specified in the config.

How to reproduce the bug

Set the EMAIL_FROM_ADDRESS in .env to one address and send an email with ->from('[email protected]').

Package Version

1.0.5

PHP Version

8.2

Laravel Version

10

Which operating systems does with happen with?

Linux

Notes

Perhaps https://github.com/InnoGE/laravel-msgraph-mail/blob/main/src/MicrosoftGraphTransport.php#L58 should be changed (untested):

old
$this->microsoftGraphApiService->sendMail($this->from, $payload);

new
$this->microsoftGraphApiService->sendMail($this->transformEmailAddresses(collect($email->getFrom())), $payload);

[Question]: ErrorInvalidUser

What happened?

code: ErrorInvalidUser
message: The requested user '[email protected]' is invalid.

.env
MAIL_FROM_ADDRESS="[email protected]"

Any idea why i receive this error, the customer say the user exists in Azure.

How to reproduce the bug

Mail::send()

Package Version

1.1

PHP Version

8.1

Laravel Version

10.43

Which operating systems does with happen with?

macOS

Notes

No response

[Bug]: Issue after updating to 1.3.0

What happened?

Error: Configuration key tenant_id for microso ft-graph mailer is missing. at /httpdocs/vendor/innoge/laravel-msgraph-mail/src/LaravelMsGraphMailServiceProvider.php:53)
PHP: 8.2
Laravel: 9

I believe the issue is in this commit in LaravelMsGraphMailServiceProvider.php:

The requireConfigString method reports missing configuration because of $value = $config[$key]; so for example it looks for $config['tenant_id']; but this is how $config looks at least in my app

[
  'driver' => 'microsoft-graph',
  'host' => '',
  'port' => 587,
  'from' => [
    'address' => '[email protected]',
    'name' => 'Mail',
  ],
  'encryption' => '',
  'username' => '',
  'password' => '',
  'sendmail' => '/usr/sbin/sendmail -bs',
  'markdown' => [
    'theme' => 'default',
    'paths' => [
      0 => '/var/www/vhosts/example.com/httpdocs/resources/views/vendor/mail',
    ],
  ],
  'log_channel' => NULL,
  'mailers' => [
    'microsoft-graph' => [
      'transport' => 'microsoft-graph',
      'client_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx',
      'client_secret' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx',
      'tenant_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx',
      'from' => [
        'address' => '[email protected]',
        'name' => 'Mail',
      ],
    ],
  ],
]

It used to pull the config this way config('mail.mailers.microsoft-graph.tenant_id', '')

How to reproduce the bug

Update to 1.3.0

Package Version

1.3.0

PHP Version

8.2

Laravel Version

9.0

Which operating systems does with happen with?

Linux

Notes

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.