-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSend Mail with mutliple Attachment.ps1
82 lines (65 loc) · 2.6 KB
/
Send Mail with mutliple Attachment.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$clientID = "yourClientID"
$Clientsecret = "yourSecret"
$tenantID = "yourTenantID"
#Configure Mail Properties
$MailSender = "[email protected]"
$Recipient = "[email protected]"
#Get File Name and Base64 string
$AttachmentArray = Get-ChildItem -Path "C:\Users\seimi\OneDrive - Seidl Michael\2-au2mator\1 - TECHGUY\GitHub\Microsoft-Graph-API-Examples" -Filter "*.docx"
$AttachmentJson = ""
foreach ($A in $AttachmentArray) {
$FileName = (Get-Item -Path $A.FullName).name
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($A.FullName))
$AttachmentJson += @"
{
"@odata.type": "#microsoft.graph.fileAttachment",
"name": "$FileName",
"contentType": "text/plain",
"contentBytes": "$base64string"
},
"@
}
$watchdir="C:\Users\seimi\OneDrive - Seidl Michael\2-au2mator\1 - TECHGUY\GitHub\Microsoft-Graph-API-Examples"
$attach102119 = @(gci $watchdir | Where-Object {$_.Name -match "_102" -or $_.Name -match "_119"} | Select-Object -expand FullName)
$attach102119.count
#Connect to GRAPH API
$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientId
Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
"Authorization" = "Bearer $($tokenResponse.access_token)"
"Content-type" = "application/json"
}
#Send Mail
$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
{
"message": {
"subject": "Hello World from Microsoft Graph API",
"body": {
"contentType": "HTML",
"content": "This Mail is sent via Microsoft <br>
GRAPH <br>
API<br>
and an Attachment <br>
"
},
"toRecipients": [
{
"emailAddress": {
"address": "$Recipient"
}
}
]
,"attachments": [
$AttachmentJson
]
},
"saveToSentItems": "false"
}
"@
Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend