Different ways to send mails using Microsoft 365 CLI

Learn how to send emails in different ways and which technology is behind the Microsoft 365 CLI commands.

Microsoft 365 CLI provides a rich set of cross-platform commands to access Microsoft 365 cloud services. When I am investigating sending mails with this CLI, I have found two different ways to send emails to the users.

  1. Sends an e-mail from SharePoint

    The below command used to send the mail to the specified user (user@ktskumar.com) from SharePoint.
    m365 spo mail send
    --webUrl https://contoso.sharepoint.com/sites/project-x
    --to "user@ktskumar.com"
    --subject "Email sent from SharePoint via Microsoft 365 CLI"
    --body "<h1>CLI for Microsoft 365</h1>Email sent via <b>command</b>."
    --additionalHeaders "'{\"X-MC-Tags\":\"CLI for Microsoft 365\"}'"

    This command uses SharePoint’s REST API “/_api/SP.Utilities.Utility.SendEmail” for sending the mails. And we must provide the SharePoint URL to run this command.

  2. Sends e-mail on behalf of the current user via Microsoft Graph API

    The below command used to send the mail to the user (user@ktskumar.com) using Microsoft Graph API.

    m365 outlook mail send --to "user@ktskumar.com" --subject "Email sent from Graph API via Microsoft 365 CLI" --bodyContents "The mail outlook send command uses Graph API. Check out the link <a href='https://pnp.github.io/cli-microsoft365/cmd/outlook/mail/mail-send/'>there</a>" --bodyContentType HTML

    This command uses the POST call of https://graph.microsoft.com/v1.0/me/sendMail to send the emails. To run this command, we no need to provide any URL, but we must have permission to run the Graph API

Either of the commands has its own advantages. To send an email based on the SharePoint site, we can use the 1st option and if we need to send a generic mail on behalf of the current user, we can use the 2nd option.

Shantha Kumar
Shantha Kumar
Articles: 295