forked from nblagoev/Gmail.ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gmail.ps.psm1
1324 lines (1135 loc) · 37.6 KB
/
Gmail.ps.psm1
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$GmailSessions = @();
function New-GmailSession {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[System.Management.Automation.PSCredential]$Credential = $($cr = (Get-StoredCredential Gmail.ps:default); if ($cr -eq $null) {Get-Credential} else {$cr})
)
$ghost = "imap.gmail.com"
$gport = 993
$guser = $Credential.UserName
$gpass = $Credential.GetNetworkCredential().Password
$session = New-Object -TypeName AE.Net.Mail.ImapClient -ArgumentList $ghost,$guser,$gpass,Login,$gport,$true,$false
$GmailSessions += $session
$session
<#
.Synopsis
Creates a Gmail session.
.Description
Opens a connection to a Gmail account using the specified credentials and creates a new session. If a generic credential is
created using the Windows Credential Manager (address: 'Gmail.ps:default'), a session is automatically created using the
stored credentials each time the cmdlet is executed without a -Credential parameter.
.Parameter Credential
The credentials that will be used to connect to Gmail.
.Example
PS> $gmail = New-GmailSession
PS> # play with your gmail...
Description
-----------
Authenticating a Gmail session using the stored credential in the Gmail.ps:default entry.
If there is no credential stored a prompt for username and password will be displayed.
.Link
Remove-GmailSession
.Link
Invoke-GmailSession
.Link
Get-GmailSession
.Link
Clear-GmailSession
#>
}
function Remove-GmailSession {
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session
)
$Session.Disconnect()
<#
.Synopsis
Removes a Gmail session.
.Description
Closes the connection to Gmail and destroys the session.
.Parameter Credential
The credentials that will be used to connect to Gmail.
.Example
PS> $gmail | Remove-GmailSession
Description
-----------
Closing an already opened connection to a Gmail account.
.Link
New-GmailSession
.Link
Invoke-GmailSession
.Link
Get-GmailSession
.Link
Clear-GmailSession
#>
}
function Invoke-GmailSession {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[System.Management.Automation.PSCredential]$Credential = $($cr = (Get-StoredCredential Gmail.ps:default); if ($cr -eq $null) {Get-Credential} else {$cr}),
[Parameter(Position = 1, Mandatory = $true)]
[ScriptBlock]$ScriptBlock
)
$gmail = New-GmailSession -Credential $Credential
& $ScriptBlock $gmail
$gmail | Remove-GmailSession
<#
.Synopsis
Invokes a block of code on a Gmail session.
.Description
Creates a new Gmail session and passes it to a script block. Once the block is executed, the session is automatically closed.
.Parameter ScriptBlock
A script that is executed once the session is opened.
.Parameter Credential
The credentials that will be used to connect to Gmail.
.Link
New-GmailSession
.Link
Remove-GmailSession
.Example
PS> Invoke-GmailSession -ScriptBlock {
PS> $args | Count-Message
PS> }
Description
-----------
Creates a Gmail session, returns the number of messages in the Inbox and then closes the session.
The automatically created session can be accessed inside the script block via the $args variable.
.Example
PS> Invoke-GmailSession -ScriptBlock {
PS> param($gmail)
PS> $gmail | Get-Label
PS> }
Description
-----------
Creates a Gmail session, returns all the labels used in that account and then closes the session.
The automatically created session can be accessed inside the script block via the $gmail variable.
#>
}
function Get-GmailSession {
$GmailSessions
<#
.Synopsis
Returns a list of all opened Gmail sessions.
.Description
Returns a list of all opened Gmail sessions.
.Link
New-GmailSession
.Link
Clear-GmailSession
#>
}
function Clear-GmailSession {
$GmailSessions | ForEach-Object -Process { $_ | Remove-GmailSession }
$GmailSessions = @();
<#
.Synopsis
Closes all opened Gmail sessions.
.Description
Closes all opened Gmail sessions.
.Link
New-GmailSession
.Link
Get-GmailSession
#>
}
function Get-Mailbox {
[CmdletBinding(DefaultParameterSetName = "DefaultFolder")]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Position = 0, ValueFromPipelineByPropertyName = $true, ParameterSetName = "DefaultFolder")]
[ValidateSet("All Mail", "Starred", "Drafts", "Important", "Sent Mail", "Spam", "Inbox")]
[string]$Name = "Inbox",
[Parameter(ValueFromPipelineByPropertyName = $true, ParameterSetName = "LabelFolder")]
[string]$Label
)
if ($Label) {
$mailbox = $Session.SelectMailbox($Label)
} elseif ($Name -and ($Name -ne "Inbox")) {
$mailbox = $Session.SelectMailbox("[Gmail]/" + $Name)
} elseif ($Name -and ($Name -eq "Inbox")) {
$mailbox = $Session.SelectMailbox("Inbox")
}
AddSessionTo $mailbox $Session
<#
.Synopsis
Returns a mailbox.
.Description
Returns the Inbox if no parameters are specified, an existing Label or one of the default
Gmail folders (All Mail, Starred, Drafts, Important, Sent Mail, Spam).
.Parameter Session
The opened session that will be manipulated.
.Parameter Name
The name of the default Gmail folder to be accessed.
.Parameter Label
The name of an existing label to be accessed.
.Example
PS> $inbox = $gmail | Get-Mailbox
PS> $inbox | Get-Message -Unread
Description
-----------
Get the unread messages in the inbox.
.Example
PS> $gmail | Get-Mailbox "Important" | Get-Message
Description
-----------
Get the messages marked as Important by Gmail.
.Link
Get-Message
.Link
Measure-Message
#>
}
function Get-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[string]$From,
[string]$To,
[DateTime]$On,
[DateTime]$After,
[DateTime]$Before,
[string]$Cc,
[string]$Bcc,
[string]$Subject,
[string]$Text,
[string]$Body,
[string[]]$Label,
[string]$FileName,
[ValidateSet("Primary", "Personal", "Social", "Promotions", "Updates", "Forums")]
[string]$Category,
[switch]$Unread,
[switch]$Read,
[switch]$Starred,
[switch]$Unstarred,
[switch]$HasAttachment,
[switch]$Answered,
[switch]$Draft,
[switch]$Undraft,
[switch]$Prefetch
)
$imap = @()
$xgm = @()
if ($Unread) {
$imap += "UNSEEN"
} elseif ($Read) {
$imap += "SEEN"
}
if ($Answered) {
$imap += "ANSWERED"
}
if ($Draft) {
$imap += "DRAFT"
} elseif ($Undraft) {
$imap += "UNDRAFT"
}
if ($Starred) {
$imap += "FLAGGED"
} elseif ($Unstarred) {
$imap += "UNFLAGGED"
}
if ($On) {
$imap += 'ON "' + $(GetRFC2060Date $After) + '"'
}
if ($From) {
$imap += 'FROM "' + $From + '"'
}
if ($To) {
$imap += 'TO "' + $To + '"'
}
if ($After) {
$imap += 'AFTER "' + $(GetRFC2060Date $After) + '"'
}
if ($Before) {
$imap += 'BEFORE "' + $(GetRFC2060Date $Before) + '"'
}
if ($Cc) {
$imap += 'CC "' + $Cc + '"'
}
if ($Bcc) {
$imap += 'BCC "' + $Bcc + '"'
}
if ($Text) {
$imap += 'TEXT "' + $Text + '"'
}
if ($Body) {
$imap += 'BODY "' + $Body + '"'
}
if ($Subject) {
$imap += 'SUBJECT "' + $Subject + '"'
}
if ($Label) {
$Label | ForEach-Object { $xgm += 'label:' + $_ }
}
if ($HasAttachment) {
$xgm += 'has:attachment'
}
if ($FileName) {
$xgm += 'filename:' + $FileName
}
if ($Category) {
$xgm += 'category:' + $Category
}
if ($imap.Length -gt 0) {
$criteria = ($imap -join ') (')
}
if ($xgm.Length -gt 0) {
$gmcr = 'X-GM-RAW "' + ($xgm -join ' ') + '"'
if ($imap.Length -gt 0) {
$criteria = $criteria + ' (' + $gmcr + ')'
} else {
$criteria = $gmcr
}
}
$result = $Session.Search('(' + $criteria + ')');
$i = 1
foreach ($item in $result) {
$msg = $Session.GetMessage($item, !$Prefetch, $false)
AddSessionTo $msg $Session
Write-Progress -Activity "Gathering messages" -Status "Progress: $($i)/$($result.Count)" -PercentComplete ($i / $result.Count * 100) -Id 90017
$i += 1
}
<#
.Synopsis
Returns a list of messages.
.Description
Returns a (filtered) list of the messages inside a selected mailbox (see Get-Mailbox).
The returned messages will have their body and attachments downloaded only if the -Prefetch parameter is specified.
Every listed message has a set of flags indicating the message's status and properties.
Flags' meaning:
ufisa The message:
^____________ is unread
^___________ is fetched
^__________ is important
^_________ is starred
^________ has attachment
Any flag may be unset. An unset flag is the equivalent of "is not" and is represented as a "-" character.
'--i-a' means the message is not Unread, is not Fetched, is Important, is not Starred and has atleast one attachment.
Supports automatic name completion for the existing labels.
.Parameter Session
The opened session that will be manipulated.
.Parameter Prefetch
Fetches the message's body and attachments. By default only the headers are downloaded from the server.
.Parameter Unread
Forces only unread messages to be returned.
.Parameter Read
Forces only read messages to be returned.
.Parameter Answered
Forces only messages that has been answered to, to be returned.
.Parameter Draft
If set, only drafts will be returned.
.Parameter Undraft
If set, only non-draft messages will be returned.
.Parameter Starred
Indicates only starred mesages to be returned.
.Parameter Unstarred
Indicates only messages that are not marked with Star to be returned.
.Parameter On
Filters the messages based on an exact date of receiving.
.Parameter After
Returns only messages received after a given date.
.Parameter Before
Returns only messages received before a given date.
.Parameter From
Filters the messages based on the sender's name and email address.
.Parameter To
Filters the messages based on the recipient's name and email address.
.Parameter Cc
Filters the messages based on the Cc recipient's name and email address.
.Parameter Bcc
Filters the messages based on the Bcc recipient's name and email address.
.Parameter Text
A text to search the entire message for.
.Parameter Body
A substring to search the message's body for.
.Parameter Subject
A substring to search the message's subject for.
.Parameter Label
Returns only messages having a particular set of labels applied.
.Parameter HasAttachment
Returns only messages with attachments.
.Parameter FileName
Returns only messages having attachments with a given name.
.Parameter Category
Returns only messages within a particular category.
.Example
PS> $inbox = $gmail | Get-Mailbox
PS> $inbox | Get-Message -Unread
Description
-----------
Get the unread messages in the inbox.
.Example
PS> $gmail | Get-Mailbox "Important" | Get-Message
Description
-----------
Get the messages marked as Important by Gmail.
.Example
PS> $inbox | Get-Message -After "2011-06-01" -Before "2012-01-01"
PS> $inbox | Get-Message -On "2011-06-01"
PS> $inbox | Get-Message -From "x@gmail.com"
PS> $inbox | Get-Message -To "y@gmail.com"
Description
-----------
Filter with some criteria.
.Example
PS> $inbox | Get-Message -Unread -From "myboss@gmail.com"
Description
-----------
Combine flags and options.
.Link
Get-Message
.Link
Update-Message
.Link
Remove-Message
.Link
Measure-Message
.Link
Reveive-Message
#>
}
function Remove-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message
)
process {
$Session.DeleteMessage($Message)
}
<#
.Synopsis
Deletes a message.
.Description
Sends a message to the Gmail's Trash folder.
.Parameter Session
The opened session that will be manipulated.
.Parameter Message
The message that will be deleted.
.Example
PS> $inbox | Get-Message -From "x@gmail.com" | Remove-Message
Description
-----------
Delete all emails from X.
.Link
Get-Message
.Link
Update-Message
#>
}
function Update-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[switch]$Read,
[switch]$Unread,
[switch]$Star,
[switch]$Unstar,
[switch]$Archive,
[switch]$Spam
)
process {
if ($Read -and $Unread) {
Write-Error "The -Read and -Unread parameters cannot be used together."
}
if ($Star -and $Unstar) {
Write-Error "The -Star and -Unstar parameters cannot be used together."
}
if ($Archive) {
$Session.MoveMessage($Message.Uid, "[Gmail]/All Mail")
}
if ($Spam) {
$Session.MoveMessage($Message.Uid, "[Gmail]/Spam")
}
$replace = $false
$changed = $false
if ($Read) {
$flags = $flags -bor [AE.Net.Mail.Flags]::Seen
$changed = $true
} elseif ($Unread) {
$flags = $Message.Flags
$flags = $flags -bxor [AE.Net.Mail.Flags]::Seen
$changed = $true
$replace = $true
}
if ($Star) {
$flags = $flags -bor [AE.Net.Mail.Flags]::Flagged
$changed = $true
} elseif ($Unstar) {
$flags = $Message.Flags
$flags = $flags -bxor [AE.Net.Mail.Flags]::Flagged
$changed = $true
$replace = $true
}
if ($changed) {
if (-not $replace) {
$Session.AddFlags([AE.Net.Mail.Flags]$flags, @($Message))
} else {
$Session.SetFlags([AE.Net.Mail.Flags]$flags, @($Message))
}
}
}
<#
.Synopsis
Flags a message.
.Description
Archives, marks as spam, as read/undead or adds/removes a star from a given message.
.Parameter Session
The opened session that will be manipulated.
.Parameter Message
The message that will be updated.
.Parameter Read
Marks a message as read.
.Parameter Unread
Marks a message as undead.
.Parameter Star
Flags a message with a Star.
.Parameter Unstar
Removes the star from a message.
.Parameter Archive
Archives a message.
.Parameter Spam
Forces a message to be marked as spam.
.Example
PS> $messages = $inbox | Get-Message -Unread | Select-Object -Last 10
PS> foreach ($msg in $messages) {
PS> $msg | Update-Message -Read # you can use -Unread, -Spam, -Star, -Unstar, -Archive too
PS> }
Description
-----------
Each message can be manipulated using block style. Remember that
every message in a conversation/thread will come as a separate message.
.Link
Get-Message
.Link
Remove-Message
#>
}
function Receive-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message
)
process {
$Session.GetMessage($Message.Uid, $false)
}
<#
.Synopsis
Fetches a message.
.Description
Fetches the whole message from the server (including the body and the attachments).
.Parameter Session
The opened session that will be manipulated.
.Parameter Message
The message that will be fetched.
.Example
PS> $msg = $inbox | Get-Message -From "x@gmail.com" | Receive-Message
PS> $msg.Body # returns the body of the message
Description
-----------
To read the actual body of a message you have to first fetch it from the Gmail servers.
.Link
Get-Message
#>
}
function Move-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true, ParameterSetName = "DefaultFolder")]
[ValidateSet("Inbox", "All Mail", "Starred", "Drafts", "Important", "Sent Mail", "Spam")]
[string]$Mailbox,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "LabelFolder")]
[string]$Label
)
if ($Label) {
$Session.MoveMessage($Message.Uid, $Label)
} elseif ($Mailbox ) {
$Session.MoveMessage($Message.Uid, $Mailbox)
}
<#
.Synopsis
Moves a message.
.Description
Moves a message to a different mailbox or label.
Supports automatic name completion for the existing labels.
.Parameter Session
The opened session that will be manipulated.
.Parameter Message
The message that will be fetched.
.Parameter Mailbox
The name of a mailbox the message will be moved to.
.Parameter Label
The name of a label the message will be moved to.
.Example
PS> $msg | Move-Message "All Mail"
Description
-----------
Move the message to the All Mail mailbox.
.Example
PS> $msg | Move-Message -Label "Test"
Description
-----------
Move the message to the Test label.
.Link
Get-Message
#>
}
function Measure-Message {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session
)
$Session.GetMessageCount()
<#
.Synopsis
Counts messages.
.Description
Returns the number of messages in a mailbox (supports labels too).
.Parameter Session
The opened session that will be manipulated.
.Example
PS> $inbox | Measure-Message
Description
-----------
Count the messages in the inbox.
.Example
PS> $gmail | Get-Mailbox "Important" | Measure-Message
Description
-----------
Count the important messages.
.Example
PS> # returns 100, the number of messages in `Important`
PS> $gmail | Get-Mailbox "Important" | Get-Message -Unread | Measure-Message
PS> # returns 2, the number of unread messages in `Important`
PS> $gmail | Get-Mailbox "Important" | Get-Message -Unread | Measure-Object
Description
-----------
Note that Measure-Message will return the number of all messages in the selected mailbox,
not the number of the returned messages (if any). To count the returned messages, use Measure-Object.
In this example we have 2 unread and 98 read messages in the Important mailbox.
.Link
Get-Message
#>
}
function Get-Conversation {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[switch]$Prefetch
)
process {
$thrid = $Message.Headers["X-GM-THRID"].Value
$result = $Session.Search('(X-GM-THRID ' + $thrid + ')')
$i = 1
foreach ($item in $result) {
$msg = $Session.GetMessage($item, !$Prefetch, $false)
AddSessionTo $msg $Session
Write-Progress -Activity "Gathering messages within a conversation" -Status "Progress: $($i)/$($result.Count)" -PercentComplete ($i / $result.Count * 100) -Id 90018
$i += 1
}
}
<#
.Synopsis
Returns a list of messages that are part of a conversation.
.Description
Returns a list of messages that are part of a conversation.
.Parameter Session
The opened session that will be manipulated.
.Parameter Message
A message that is part of the wanted conversation.
.Parameter Prefetch
Fetches the conversation's bodies and attachments. By default only the headers are downloaded from the server.
.Example
PS> $gmail | Get-Mailbox Inbox | Get-Message -From z@gmail.com | Get-Conversaion
Description
-----------
Searches the Inbox based on the message returned by Get-Message,
and returns all messages that are part of that conversaton and are in the Inbox.
.Example
PS> $gmail | Get-Mailbox "All Mail" | Get-Message -From z@gmail.com | Get-Conversaion
Description
-----------
Searches "All Mail" based on the message returned by Get-Message,
and returns all messages that are part of that conversaton.
.Link
Get-Message
.Link
Get-Mailbox
#>
}
function Save-Attachment {
[CmdletBinding(DefaultParameterSetName = "Path")]
param (
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(Position = 0, ParameterSetName = "Path", Mandatory = $true)]
[string[]] $Path,
[Parameter(ParameterSetName = "LiteralPath", Mandatory = $true)]
[string[]] $LiteralPath,
[Parameter(Mandatory = $false)]
[switch] $PassThru
)
process {
$paths = ($LiteralPath + $Path | Where { $_ })
for ($i = 0; $i -lt $paths.Count; $i++) {
$destPath = $paths[$i]
if (!(Test-Path -Path $destPath -PathType Container)) {
New-Item -Path $destPath -ItemType Container | Out-Null
}
$destPath = $destPath | Resolve-Path
foreach ($a in $Message.Attachments) {
$filename = ($Message.Uid + "_" + $a.Filename)
$fileDest = Join-Path $destPath $filename
if ($i -eq 0) {
$a.Save($fileDest)
} else {
$fileLoc = Join-Path ($paths[0] | Resolve-Path) $filename
Copy-Item $fileLoc $fileDest
}
if ($PassThru) {
Get-ItemProperty $fileDest
}
}
}
}
<#
.Synopsis
Downloads the attachments of a message.
.Description
Downloads the attachments of a message to a local folder.
.Parameter Message
The message whose attachments will be downloaded.
.Parameter Path
Specifies a path to the directory where the attachments will be saved.
.Parameter LiteralPath
Specifies a path to the directory where the attachments will be saved. The value of the LiteralPath parameter is used
exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in
single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
.Parameter PassThru
Returns a file object representing each downloaded attachment. By default, this cmdlet does not generate any output.
.Example
PS> $gmail | Get-Mailbox -Label "Important" | Get-Message -Prefetch | Save-Attachment $folder
Description
-----------
Save all attachments in the "Important" label to a local folder.
Note that without the -Prefetch parameter, no attachments will be downloaded.
.Example
PS> $msg = $inbox | Get-Message -Unread -HasAttachment | Select-Object -Last 1
PS> $fetchedMsg = $msg | Receive-Message # or use -Prefetch on Get-Message above
PS> $fetchedMsg.Attachments[0].Save($location)
Description
-----------
Save just the first attachment from the newest unread email.
.Link
Get-Message
.Link
Receive-Message
#>
}
function Get-Label {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message,
[Parameter(Position = 0, Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[Alias("Name")]
[string]$Like = "",
[Parameter()]
[switch]$All
)
process {
if ($Message) {
$Message.Labels
} else {
if ($All) {
$Session.ListMailboxes($Like, "*")
} else {
$Session.ListMailboxes($Like, "*") | Where-Object { $_.Name -notmatch "\[Gmail\]" -and $_.Name -ne "INBOX" }
}
}
}
<#
.Synopsis
Returns the labels applied to a message or all labels that exist.
.Description
Returns the labels applied to a message or all labels that exist.
.Parameter Session
The opened session that will be used to fetch all existing labels.
.Parameter Message
The message whose labels will be returned.
.Example
PS> $msg | Get-Label
Description
-----------
Get all labels applied to a message.
.Example
PS> $gmail | Get-Label
Description
-----------
Get a list of the defined labels.
.Example
PS> $gmail | Get-Label -Name "SomeLabel" # returns null if the label doesn't exist
Description
-----------
Check if a label exists.
.Link
New-Label
.Link
Set-Label
.Link
Remove-Label
#>
}
function New-Label {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[string[]]$Name,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session
)
foreach ($item in $Name) {
$Session.CreateMailbox($item)
}
<#
.Synopsis
Creates a label.
.Description
Creates a new label.
.Parameter Session
The opened session that will be manipulated.
.Parameter Name
The name of the label that will be created.
.Link
Get-Label
.Link
Set-Label
.Link
Remove-Label
#>
}
function Remove-Label {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[string[]]$Name,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AE.Net.Mail.ImapClient]$Session,
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[AE.Net.Mail.MailMessage]$Message
)
foreach ($item in $Name) {
if ($Message) {
$Session.RemoveLabels($Name, @($Message))
} else {
$Session.DeleteMailbox($item)
}
}
<#
.Synopsis