Ask Your Question
0

How can I send emails from Laravel using Outlook?

asked 2023-05-10 17:38:54 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-10 17:47:02 +0000

pufferfish gravatar image

To send emails from Laravel using Outlook, you need to follow these steps:

  1. Install the Microsoft/Graph package using composer. Run the below command in your terminal:

    require microsoft/microsoft-graph

  2. 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="">".

  3. 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.

  4. 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.

  5. 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');

  6. 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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-10 17:38:54 +0000

Seen: 14 times

Last updated: May 10 '23