Thursday, December 5, 2024
HomePowershellPowerShell Script to Simplify Ship-MgUserMail

PowerShell Script to Simplify Ship-MgUserMail


Okay, Microsoft Graph API is cool, however generally it’s boring to take care of all these hashtables and arrays.

The Ship-MgUserMail is a good graph cmdlet to ship Emails utilizing the Graph API endpoint, this submit is totally defined within the Ship-MgUserMail BodyParameter Defined.

However going by these hashtables is a problem, and its extra difficult once you need to ship emails to a number of customers.

Certainly Ship-MailMessage was easy and straightforward to make use of, simply kind the parameter and set the worth, and you might be good to go. That is what I need to do however use Graph API.

This cmdlet wont make a brand new connection to the Graph API, so be certain to have a legitimate reference to the required scope.

Obtain

You may obtain the PowerShell script Ship-GraphMail from my GitHub Repo or from PSGallery utilizing the next line

Set up-Script -Title Ship-GraphMail

Ship-GraphMail

The Ship-GraphMail sends Microsoft Graph e-mail messaging utilizing a simplified strategy that appears just like Ship-MailMessage, however by using the Ship-MgUserMail within the background.

This script will construct all of the required parameters for you and ship the e-mail. It’s doable additionally to not ship the e-mail however as an alternative take the JSON request solely so you need to use it in another app to ship the e-mail.

This lower the time an admin must undergo these nested arrays and hashtable.

I really like the group work, be happy to ship a pull request and replace what you would like, add you title and merge it.
In a while I’ll add further parameters to the Ship-GraphMail

Supported Parameters

Ship-GraphMail Help the next parameters:

  • [Array] To: The recipient’s tackle/s could be an array for a number of accounts. Simply be certain to incorporate the array kind @(“User1@area.com”,”User2@Area.com”)
  • [Array] Bcc: Just like To, however that is the BCC and likewise helps an array of recipients
  • [Array] CC: Just like To and Bcc, it additionally helps an array of recipients.
  • [String] Topic: The Message Topic
  • [String] MessageFormat: it may be HTML or Textual content
  • [String] Physique: The Message Physique you need to ship.
  • [Switch] BodyFromFile: Utilizing it means the physique is saved in a file in your exhausting drive. When utilizing the BodyFromFile, the Physique parameter must be the total path of the file.
  • [Switch] DeliveryReport: Set to obtain a supply report e-mail or not
  • [Switch] ReadReport: set to obtain a learn report or not.
  • [Switch] Flag: allow the follow-up flag for the message
  • [ValidationSet] Significance: Set the message precedence. It may be one of many following, Low or Excessive
  • [String] Attachments: The Attachment file path. For now, it solely helps 1 attachment. If you would like extra, let me know
  • [String] DocumentType: The attachment MIME kind, for instance for textual content file, the DocumentType is textual content/plain
  • [Switch] ReturnJSON: This gained’t ship the e-mail, however as an alternative, it returns the JSON file totally structured and prepared, so you may invoke it with another software.

Beta endpoints will not be included, akin to Mentions.

EXAMPLE

Listed here are some examples of the right way to use the script.

Ship a Graph e-mail message to a number of customers with attachments and a number of To, CC and single Bcc, additionally request the Supply and Learn stories.

Ship-GraphMail -To @('user1@area.com','user2@area.com') -CC @('cc@area.com','cc1@area.com') -Bcc "bcc@area.com" -Topic "Check Message" -MessageFormat HTML -Physique 'That is the Message Physique' -DeliveryReport -ReadReport -Attachments C:MyFile.txt -DocumentType 'textual content/plain'

Ship Graph e-mail, load the Physique from a file saved domestically. Ensure that to make use of the BodyFromFile swap.

Ship-GraphMail -To 'User1@area.com' -Topic "Check Message" -MessageFormat HTML -Physique C:11111.csv -BodyFromFile -DeliveryReport -ReadReport -Flag -Significance Excessive -Attachments 'C:MyFile.txt' -DocumentType 'textual content/plain'

Return and get how the JSON is structured with out sending the Electronic mail. That is completed through the use of the –ReturnJSON Parameter

$JSONFile=send-GraphMail -To 'vdi1@area.com' -Topic "Check Message" -MessageFormat HTML -Physique "Hello That is New Message" -Flag -ReturnJSON

The output of the above line seems to be like this

{
  "Message": {
    "ToRecipients": [
      {
        "EmailAddress": {
          "Address": "vdi1@domain.com"
        }
      }
    ],
    "Physique": {
      "Content material": "That is the Message Physique",
      "ContentType": "HTML"
    },
    "Topic": "MySubject"
  }
}

I hope this script helps a bit in simplifying a standard job, let me know within the feedback.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments