To send emails from Laravel using Outlook, you need to follow these steps:
Install the Microsoft/Graph package using composer. Run the below command in your terminal:
require microsoft/microsoft-graph
Create an Azure AD application
a. Go to the Azure portal (https://portal.azure.com/) and sign in with your Microsoft account.
b. Go to the App Registrations page.
c. Click "New registration" and enter a name for your application.
d. Click "Register".
e. On the Overview page, copy the Application (client) ID and the Directory (tenant) ID.
f. Under the "Certificates & secrets" section, click "New client secret". Enter a description and select an expiration date.
g. Click "Add".
h. Copy the client secret value that is displayed. This is the only time you'll be able to see this value so make sure to save it somewhere.
i. Under "API permissions", add the "Mail.Send" permission.
j. Click "Grant admin consent for <your tenant="">".
Configure the .env file
a. In the root directory of your Laravel project, open the .env file.
b. Add the following lines to the file:
``` MAILDRIVER=smtp MAILHOST=smtp.office365.com MAILPORT=587 MAILUSERNAME=<your email="" address=""> MAILPASSWORD=<your password=""> MAILENCRYPTION=tls
MSCLIENTID=<your client="" id=""> MSCLIENTSECRET=<your client="" secret=""> MSREDIRECTURI=<your redirect="" uri=""> MSTENANTID=<your tenant="" id=""> ```
c. Replace the placeholders with your actual values.
Create a new controller
a. Create a new controller by running the following command:
artisan make:controller OutlookController
b. Open the controller file and add the following function:
``` public function sendEmail() { $token = $this->getAccessToken();
$graph = new Graph();
$graph->setAccessToken($token);
$message = [
'toRecipients' => [
[
'emailAddress' => [
'address' => 'recipient@domain.com'
]
]
],
'subject' => 'Test Email',
'body' => [
'contentType' => 'Text',
'content' => 'This is a test email'
]
];
$graph->createRequest('POST', '/me/sendMail')
->attachBody($message)
->execute();
return "Email sent successfully";
} ```
c. Replace the recipient email address and the message content with your own values.
d. The getAccessToken() function should be added to the same controller class. This function is used to authenticate your application and obtain an access token for Microsoft Graph. Here's an example implementation:
``` private function getAccessToken() { $oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([ 'clientId' => config('services.msgraph.clientid'), 'clientSecret' => config('services.msgraph.clientsecret'), 'redirectUri' => config('services.msgraph.redirecturi'), 'urlAuthorize' => 'https://login.microsoftonline.com/' . config('services.msgraph.tenantid') . '/oauth2/v2.0/authorize', 'urlAccessToken' => 'https://login.microsoftonline.com/' . config('services.msgraph.tenantid') . '/oauth2/v2.0/token', 'urlResourceOwnerDetails' => '', 'scopes' => 'openid profile offlineaccess Mail.Send', ]);
$token = $oauthClient->getAccessToken('client_credentials', [
'scope' => 'https://graph.microsoft.com/.default',
]);
return $token->getToken();
} ```
e. Save the controller file.
Add a route for the controller function
a. Open the web.php file in the routes directory.
b. Add the following line to define a new route:
Route::get('/send-email', 'OutlookController@sendEmail')->name('send.email');
Test the implementation
a. In your web browser, go to the URL: http://your-domain.com/send-email.
b. The message "Email sent successfully" should be displayed.
c. Check your Outlook account to see if you have received the test email.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-10 17:38:54 +0000
Seen: 15 times
Last updated: May 10 '23
How can a HTTP header be inserted in WordPress?
How can data be stored in a data attribute and utilized through Ajax?
How can an array be passed using typo3 flexform xml and itemsProcConfig?
Does JSON encode fail to retrieve data from the database?
Can the GS1 128 barcode decoder in PHP or Jquery be utilized?
How can Xdebug be used in conjunction with VSCode for Laravel on Sail and WSL2?
In PHP, what is the method for finding encrypted data using the "like" operator?
What is the method to pass a variable from PHP back to JavaScript?
Why do PHP variable variables display unexpected behavior when used with arrays?