How to insert variable into a body of email sent via Send-MgUserMail

T Crha 396 Reputation points
2023-05-16T06:47:53+00:00

Hello everyone,

I am wondering if anyone knows how to construct a body parameter for Send-MgUserMail cmdlet so it takes a variable with stored data, and send them in a body of an email (my goal is to send this from Azure automation runbook).

Contents of the variable look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/></colgroup>
<tr><th>DisplayName</th><th>UserPrincipalName</th></tr>
<tr><td>Surname Name 1</td><td>******@etc.com</td></tr>
<tr><td>Surname Name 2</td><td>******@etc.com</td></tr>
<tr><td>Surname Name 3</td><td>******@etc.com</td></tr>
<tr><td>Surname Name 4</td><td>******@etc.com</td></tr>
<tr><td>Surname Name 5</td><td>******@etc.com</td></tr>
<tr><td>Surname Name 6</td><td>******@etc.com</td></tr>
</table>
</body></html>

And when I want to define email parameters according to following scheme...

Import-Module Microsoft.Graph.Users.Actions
$params = @{
	Message = @{
		Subject = "Meet for lunch?"
		Body = @{
			ContentType = "Text"
			Content = "The new cafeteria is open."
		}
		ToRecipients = @(
			@{
				EmailAddress = @{
					Address = "******@contoso.onmicrosoft.com"
				}
			}
		)
		CcRecipients = @(
			@{
				EmailAddress = @{
					Address = "******@contoso.onmicrosoft.com"
				}
			}
		)
	}
	SaveToSentItems = "false"
}
# A UPN can also be used as -UserId.
Send-MgUserMail -UserId $userId -BodyParameter $params

...I am not sure how to fit the variable into this - is it possible to just reference a variable or do I need to construct it in a different way?
Is it possible to somehow define ContentType as variable and then just put it there?

Many thanks for your help,

Tomas

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2023-05-16T07:26:47.23+00:00

    Sure, you can just plugin variables as needed. I.e. if you have a variable $contentType defining the type ("html") and another one holding the content ($body), you can do something like this:

    		Body = @{
    			ContentType = "$($contentType)"
    			Content = "$($Body)"
    		}
    

    There are ready-to-use examples, including a full script, in this article by Tony: https://practical365.com/send-email-powershell-graph/


  2. Sedat SALMAN 14,180 Reputation points MVP
    2023-05-16T12:50:24.5866667+00:00

    First, assign the HTML content to a variable:

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>HTML TABLE</title>
    </head><body>
    <table>
    <colgroup><col/><col/></colgroup>
    <tr><th>DisplayName</th><th>UserPrincipalName</th></tr>
    <tr><td>Surname Name 1</td><td>******@etc.com</td></tr>
    <tr><td>Surname Name 2</td><td>******@etc.com</td></tr>
    <tr><td>Surname Name 3</td><td>******@etc.com</td></tr>
    <tr><td>Surname Name 4</td><td>******@etc.com</td></tr>
    <tr><td>Surname Name 5</td><td>******@etc.com</td></tr>
    <tr><td>Surname Name 6</td><td>******@etc.com</td></tr>
    </table>
    </body></html>
    "@
    
    

    Then, use that variable in the $params hashtable:

    
    $params = @{
    	Message = @{
    		Subject = "Meet for lunch?"
    		Body = @{
    			ContentType = "HTML"
    			Content = $htmlContent
    		}
    		ToRecipients = @(
    			@{
    				EmailAddress = @{
    					Address = "******@contoso.onmicrosoft.com"
    				}
    			}
    		)
    		CcRecipients = @(
    			@{
    				EmailAddress = @{
    					Address = "******@contoso.onmicrosoft.com"
    				}
    			}
    		)
    	}
    	SaveToSentItems = "false"
    }
    
    Send-MgUserMail -UserId $userId -BodyParameter $params
    
    
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.