Skip to content

Commit 7bf22fa

Browse files
authored
Merge pull request #3157 from gautamdsheth/feature/3137
Feature #3137: allow sending attachments via Graph API
2 parents 08c3363 + 24c2a23 commit 7bf22fa

File tree

6 files changed

+807
-18
lines changed

6 files changed

+807
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2525
- Added `Move-PnPTerm` and `Move-PnPTermSet` cmdlets to allow moving the terms and termsets. [#2989](https://github.com/pnp/powershell/pull/2989)
2626
- Added `-VerticalZoneEmphasis` parameter to `Add-PnPPageSection` cmdlet to allow setting the emphasis value for vertical columns. [#3129](https://github.com/pnp/powershell/pull/3129)
2727
- Added `-AllowDeletion` parameter to `Set-PnPList` cmdlet to allow or prevent deletion of list from the SharePoint UI. [#3142](https://github.com/pnp/powershell/pull/3142)
28+
- Added `-Attachments` parameter to `Send-PnPMail` cmdlet to allow sending attachments via Microsoft Graph API. [#3157](https://github.com/pnp/powershell/pull/3157)
2829

2930
### Fixed
3031

src/Commands/Model/Mail/Message.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,11 @@ public class Message
6969
/// </summary>
7070
[JsonPropertyName("importance")]
7171
public MessageImportanceType? Importance { get; set; }
72+
73+
/// <summary>
74+
/// The attachments to attach to the message
75+
/// </summary>
76+
[JsonPropertyName("attachments")]
77+
public List<MessageAttachmentOptions> Attachments { get; set; }
7278
}
73-
}
79+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace PnP.PowerShell.Commands.Model.Mail
4+
{
5+
public class MessageAttachmentOptions
6+
{
7+
[JsonPropertyName("@odata.type")]
8+
public string Type { get; set; } = "#microsoft.graph.fileAttachment";
9+
/// <summary>
10+
/// Name of the attachment
11+
/// </summary>
12+
[JsonPropertyName("name")]
13+
public string Name { get; set; }
14+
15+
/// <summary>
16+
/// Content-Type of the attachment
17+
/// </summary>
18+
[JsonPropertyName("contentType")]
19+
public string ContentType { get; set; } = "text/plain";
20+
21+
/// <summary>
22+
/// Bytes of the attachment
23+
/// </summary>
24+
[JsonPropertyName("contentBytes")]
25+
public string ContentBytes { get; set; }
26+
}
27+
}

src/Commands/Utilities/MailUtility.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Net.Mail;
1212
using System.Net;
1313
using PnP.PowerShell.Commands.Enums;
14+
using System.IO;
15+
using System;
1416

1517
namespace PnP.PowerShell.Commands.Utilities
1618
{
@@ -32,9 +34,9 @@ public static async Task SendGraphMail(PnPConnection connection, string accessTo
3234
var jsonSerializer = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, };
3335
jsonSerializer.Converters.Add(new JsonStringEnumConverter());
3436

35-
var stringContent = new StringContent(JsonSerializer.Serialize(new SendMailMessage { Message = message, SaveToSentItems = saveToSentItems}, jsonSerializer));
37+
var stringContent = new StringContent(JsonSerializer.Serialize(new SendMailMessage { Message = message, SaveToSentItems = saveToSentItems }, jsonSerializer));
3638
stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
37-
39+
3840
var response = await GraphHelper.PostAsync(connection, $"v1.0/users/{message.Sender.EmailAddress.Address}/sendMail", accessToken, stringContent);
3941

4042
if (response.StatusCode != System.Net.HttpStatusCode.Accepted)
@@ -67,7 +69,7 @@ public static async Task SendSharePointEmail(ClientContext context, string subje
6769
if (bcc != null)
6870
{
6971
properties.BCC = bcc;
70-
}
72+
}
7173

7274
properties.Subject = subject;
7375
properties.Body = body;
@@ -113,7 +115,7 @@ public static async Task SendSmtpEmail(string subject, string body, string fromA
113115
IsBodyHtml = contentType == MessageBodyContentType.Html
114116
};
115117

116-
if(importance.HasValue)
118+
if (importance.HasValue)
117119
{
118120
switch (importance.Value)
119121
{
@@ -132,7 +134,7 @@ public static async Task SendSmtpEmail(string subject, string body, string fromA
132134
foreach (string user in to)
133135
{
134136
mail.To.Add(user);
135-
}
137+
}
136138

137139
if (cc != null)
138140
{
@@ -151,6 +153,43 @@ public static async Task SendSmtpEmail(string subject, string body, string fromA
151153
}
152154

153155
await client.SendMailAsync(mail);
154-
}
156+
}
157+
158+
/// <summary>
159+
/// Gets the list of attachments to be sent via email
160+
/// </summary>
161+
/// <param name="attachments">The list of attachments to be sent</param>
162+
/// <param name="currentPath">The current path of the directory</param>
163+
/// <returns></returns>
164+
public static List<MessageAttachmentOptions> GetListOfAttachments(string[] attachments, string currentPath)
165+
{
166+
if (attachments == null || attachments?.Length == 0)
167+
{
168+
return null;
169+
}
170+
171+
List<MessageAttachmentOptions> messageAttachmentOptions = new List<MessageAttachmentOptions>();
172+
foreach (var attachment in attachments)
173+
{
174+
MessageAttachmentOptions item = new MessageAttachmentOptions();
175+
var file = attachment;
176+
if (!System.IO.Path.IsPathRooted(attachment))
177+
{
178+
file = System.IO.Path.Combine(currentPath, attachment);
179+
}
180+
181+
FileInfo attachmentFile = new FileInfo(file);
182+
item.Type = "#microsoft.graph.fileAttachment";
183+
item.Name = attachmentFile.Name;
184+
var fileByteArray = System.IO.File.ReadAllBytes(file);
185+
item.ContentBytes = Convert.ToBase64String(fileByteArray);
186+
187+
MimeTypeMap.TryGetMimeType(attachmentFile.Name, out var mimeType);
188+
item.ContentType = mimeType ?? "text/plain";
189+
messageAttachmentOptions.Add(item);
190+
}
191+
192+
return messageAttachmentOptions;
193+
}
155194
}
156195
}

0 commit comments

Comments
 (0)