/
basics.html
1074 lines (1049 loc) · 83.9 KB
/
basics.html
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
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>mRpostman basics • mRpostman</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><meta property="og:title" content="mRpostman basics">
<meta property="og:description" content="mRpostman">
<meta property="og:image" content="/logo.png">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container template-article">
<header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">mRpostman</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">1.1.1</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="../index.html">
<span class="fas fa-home fa-lg"></span>
</a>
</li>
<li>
<a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Articles
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="../articles/basics.html">mRpostman basics</a>
</li>
<li>
<a href="../articles/code_migration.html">Migrating old code to the new mRpostman's syntax</a>
</li>
<li>
<a href="../articles/xoauth2.0.html">IMAP OAuth2.0 authentication in mRpostman</a>
</li>
</ul>
</li>
<li>
<a href="../news/index.html">Changelog</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/allanvc/mRpostman/">
<span class="fab fa-github fa-lg"></span>
</a>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container -->
</div>
<!--/.navbar -->
</header><script src="basics_files/header-attrs-2.23/header-attrs.js"></script><div class="row">
<div class="col-md-9 contents">
<div class="page-header toc-ignore">
<h1 data-toc-skip>mRpostman basics</h1>
<small class="dont-index">Source: <a href="https://github.com/allanvc/mRpostman/blob/HEAD/vignettes/basics.Rmd"><code>vignettes/basics.Rmd</code></a></small>
<div class="hidden name"><code>basics.Rmd</code></div>
</div>
<div id="introduction" class="section level2">
<h2 class="hasAnchor">
<a href="#introduction" class="anchor"></a>Introduction</h2>
<p><code>mRpostman</code> is an easy-to-use IMAP client that provides
tools for message searching, selective fetching of message attributes,
mailbox management, attachment extraction, and several other IMAP
features, paving the way for email data analysis in R. To do so, this
package makes extensive use of the {curl} package and the libcurl C
library.</p>
<p>In this vignette, we present all available methods and functions of
this package, but not all the possibilities one can explore.</p>
<p><strong>IMPORTANT</strong>:</p>
<ol style="list-style-type: decimal">
<li><p>In version <code>0.9.0.0</code>, <code>mRpostman</code> went
trough substantial changes, including ones that have no backward
compatibility with versions <code><= 0.3.1</code>. A detailed
vignette on how to migrate your mRpostman’s deprecated code to the new
syntax is available at <a href="https://allanvc.github.io/mRpostman/articles/code_migration.html"><em>“Migrating
old code to the new mRpostman’s syntax”</em></a>.</p></li>
<li><p>Old versions of the libcurl C library ({curl}’s main engine) will
cause malfunctioning of this package. If your libcurl’s version is above
7.58.0, you should be fine. If you intend to use OAuth 2.0
authentication, then you will need libcurl >= 7.65.0. To know more
about the OAuth 2.0 authentication in this package, refer to the <a href="https://allanvc.github.io/mRpostman/articles/xoauth2.0.html"><em>“Using
IMAP OAuth2.0 authentication in mRpostman”</em></a> vignette.</p></li>
</ol>
</div>
<div id="authentication" class="section level2">
<h2 class="hasAnchor">
<a href="#authentication" class="anchor"></a>Authentication</h2>
<p>There are two ways of connecting to your IMAP server: using plain or
OAuth2.0 authentication. In this vignette, we only describe the plain
authentication process. If you want to use OAuth2.0 authentication,
please read the aforementioned vignette.</p>
<div id="allowing-less-secure-apps-access" class="section level3">
<h3 class="hasAnchor">
<a href="#allowing-less-secure-apps-access" class="anchor"></a>Allowing less secure apps access</h3>
<p>When using plain authentication, most of the mail providers will
require the user to enable <strong>less secure apps</strong> access.
Once it is done, you will be able to access your mailbox using a “third
party app” as <code>mRpostman</code>.</p>
<p>See the <strong>README.md</strong> for more details on how to allow
less secure apps access in some of the most known mail providers.</p>
<p>If you are using Outlook - Office 365, there’s no need to make any
external configuration.</p>
</div>
</div>
<div id="package-structure" class="section level2">
<h2 class="hasAnchor">
<a href="#package-structure" class="anchor"></a>Package Structure</h2>
<p>The package is now implemented under a OO framework, using an R6
class. The main functionalities of <code>mRpostman</code> are
implemented as methods of the R6 class called <code>ImapCon</code>.
There are also a few independent functions. All methods and functions
are described below:</p>
<ul>
<li>
<strong>configuration methods</strong>:
<code><a href="../reference/configure_imap.html">configure_imap()</a></code>, <code>reset_url()</code>,
<code>reset_username()</code>, <code>reset_password()</code>,
<code>reset_verbose()</code>, <code>reset_buffersize()</code>,
<code>reset_timeout_ms()</code>,
<code>reset_xoauth2_bearer()</code>;</li>
<li>
<strong>server-capabilities method</strong>:
<code>list_server_capabilities()</code>;</li>
<li>
<strong>mailbox-operations methods</strong>:
<code>list_mail_folders()</code>, <code>select_folder()</code>,
<code>examine_folder()</code>, <code>rename_folder()</code>,
<code>create_folder()</code>, <code>list_flags()</code>;</li>
<li>
<strong>single-search methods</strong>:
<code>search_before()</code>, <code>search_since()</code>,
<code>search_period()</code>, <code>search_on()</code>,
<code>search_sent_before()</code>,<code>search_sent_since()</code>,
<code>search_sent_period()</code>, <code>search_sent_on()</code>,
<code>search_string()</code>, <code>search_flag()</code>,
<code>search_smaller_than()</code>, <code>search_larger_than()</code>,
<code>search_younger_than()</code>,
<code>search_older_than()</code>;</li>
<li>
<strong>the custom-search method and its helper functions</strong>:
<code><a href="https://rdrr.io/r/base/search.html">search()</a></code>;
<ul>
<li>relational operators functions: <code><a href="../reference/AND.html">AND()</a></code>,
<code><a href="../reference/OR.html">OR()</a></code>;</li>
<li>criteria definition functions: <code><a href="../reference/before.html">before()</a></code>,
<code><a href="../reference/since.html">since()</a></code>, <code><a href="../reference/on.html">on()</a></code>, <code><a href="../reference/sent_before.html">sent_before()</a></code>,
<code><a href="../reference/sent_since.html">sent_since()</a></code>, <code><a href="../reference/sent_on.html">sent_on()</a></code>,
<code><a href="../reference/string.html">string()</a></code>, <code><a href="../reference/flag.html">flag()</a></code>, <code><a href="../reference/smaller_than.html">smaller_than()</a></code>,
<code><a href="../reference/larger_than.html">larger_than()</a></code>, <code><a href="../reference/younger_than.html">younger_than()</a></code>,
<code><a href="../reference/older_than.html">older_than()</a></code>;</li>
</ul>
</li>
<li>
<strong>fetch methods</strong>: <code>fetch_body()</code>,
<code>fetch_header()</code>, <code>fetch_text()</code>,
<code>fetch_metadata()</code>, <code>fetch_attachments_list()</code>,
<code>fetch_attachments()</code>;</li>
<li>
<strong>attachments methods</strong>:
<code><a href="../reference/list_attachments.html">list_attachments()</a></code>, <code>get_attachments()</code>,
<code>fetch_attachments_list()</code>,
<code>fetch_attachments()</code>;</li>
<li>
<strong>complementary methods</strong>: <code>copy_msg()</code>,
<code>move_msg()</code>, <code>esearch_min_id()</code>,
<code>esearch_max_id()</code>, <code>esearch_count_msg()</code>,
<code>delete_msg()</code>, <code>expunge()</code>,
<code>add_flags()</code>, <code>remove_flags()</code>,
<code>replace_flags()</code>.</li>
</ul>
</div>
<div id="how-do-i-start-connection-configuration" class="section level2">
<h2 class="hasAnchor">
<a href="#how-do-i-start-connection-configuration" class="anchor"></a>1) How do I start? (Connection configuration)</h2>
<p>After enabling (if needed) “less secure apps access” in your mail
provider or obtaining an OAuth2.0 token, you have to configure an IMAP
connection:</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://allanvc.github.io/mRpostman/">mRpostman</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># IMAP settings</span></span>
<span></span>
<span><span class="co"># Outlook - Office 365</span></span>
<span><span class="va">con</span> <span class="op"><-</span> <span class="fu"><a href="../reference/configure_imap.html">configure_imap</a></span><span class="op">(</span></span>
<span> url<span class="op">=</span><span class="st">"imaps://outlook.office365.com"</span>,</span>
<span> username<span class="op">=</span><span class="st">"user@your_company.com"</span>,</span>
<span> password<span class="op">=</span><span class="fu">rstudioapi</span><span class="fu">::</span><span class="fu"><a href="https://rstudio.github.io/rstudioapi/reference/askForPassword.html">askForPassword</a></span><span class="op">(</span><span class="op">)</span></span>
<span> <span class="op">)</span></span>
<span></span>
<span><span class="co"># Gmail</span></span>
<span><span class="va">con</span> <span class="op"><-</span> <span class="fu"><a href="../reference/configure_imap.html">configure_imap</a></span><span class="op">(</span></span>
<span> url <span class="op">=</span> <span class="st">"imaps://imap.gmail.com"</span>,</span>
<span> username <span class="op">=</span> <span class="st">"user"</span>,</span>
<span> password <span class="op">=</span> <span class="fu">rstudioapi</span><span class="fu">::</span><span class="fu"><a href="https://rstudio.github.io/rstudioapi/reference/askForPassword.html">askForPassword</a></span><span class="op">(</span><span class="op">)</span></span>
<span> <span class="op">)</span></span>
<span></span>
<span><span class="co"># Hotmail</span></span>
<span><span class="va">con</span> <span class="op"><-</span> <span class="fu"><a href="../reference/configure_imap.html">configure_imap</a></span><span class="op">(</span></span>
<span> url <span class="op">=</span> <span class="st">"imaps://imap-mail.outlook.com"</span>,</span>
<span> username <span class="op">=</span> <span class="st">"user@hotmail.com"</span>,</span>
<span> password <span class="op">=</span> <span class="fu">rstudioapi</span><span class="fu">::</span><span class="fu"><a href="https://rstudio.github.io/rstudioapi/reference/askForPassword.html">askForPassword</a></span><span class="op">(</span><span class="op">)</span></span>
<span> <span class="op">)</span></span>
<span></span>
<span><span class="co"># Yahoo Mail</span></span>
<span><span class="va">con</span> <span class="op"><-</span> <span class="fu"><a href="../reference/configure_imap.html">configure_imap</a></span><span class="op">(</span></span>
<span> url<span class="op">=</span><span class="st">"imaps://imap.mail.yahoo.com/"</span>,</span>
<span> username<span class="op">=</span><span class="st">"your_user"</span>,</span>
<span> password<span class="op">=</span><span class="fu">rstudioapi</span><span class="fu">::</span><span class="fu"><a href="https://rstudio.github.io/rstudioapi/reference/askForPassword.html">askForPassword</a></span><span class="op">(</span><span class="op">)</span></span>
<span> <span class="op">)</span></span>
<span></span>
<span><span class="co"># AOL Mail</span></span>
<span><span class="va">con</span> <span class="op"><-</span> <span class="fu"><a href="../reference/configure_imap.html">configure_imap</a></span><span class="op">(</span></span>
<span> url<span class="op">=</span><span class="st">"imaps://export.imap.aol.com/"</span>,</span>
<span> username<span class="op">=</span><span class="st">"your_user"</span>,</span>
<span> password<span class="op">=</span><span class="fu">rstudioapi</span><span class="fu">::</span><span class="fu"><a href="https://rstudio.github.io/rstudioapi/reference/askForPassword.html">askForPassword</a></span><span class="op">(</span><span class="op">)</span></span>
<span> <span class="op">)</span></span>
<span></span>
<span><span class="co"># Yandex Mail</span></span>
<span><span class="va">con</span> <span class="op"><-</span> <span class="fu"><a href="../reference/configure_imap.html">configure_imap</a></span><span class="op">(</span></span>
<span> url<span class="op">=</span><span class="st">"imaps://imap.yandex.com"</span>,</span>
<span> username<span class="op">=</span><span class="st">"your_user"</span>,</span>
<span> password<span class="op">=</span><span class="fu">rstudioapi</span><span class="fu">::</span><span class="fu"><a href="https://rstudio.github.io/rstudioapi/reference/askForPassword.html">askForPassword</a></span><span class="op">(</span><span class="op">)</span></span>
<span> <span class="op">)</span></span>
<span></span>
<span><span class="co"># ... and any other mail provider with IMAP support</span></span></code></pre></div>
<p>Other useful options are: <code>timeout_ms</code>,
<code>verbose = TRUE</code>, <code>buffersize</code>. Further {curl}
options related to IMAP functionalities can be passed to
<code><a href="../reference/configure_imap.html">configure_imap()</a></code>, but you probably won’t need it. See
<code><a href="https://rdrr.io/pkg/curl/man/curl_options.html">curl::curl_options()</a></code>.</p>
<p>Since version <code>0.9.0.0</code>, this package provides more
flexibility to the user in the sense that you can modify the connection
parameters for specific commands or parts of a script, using the
<code>reset_*()</code> methods. This prevents users from having to call
<code><a href="../reference/configure_imap.html">configure_imap()</a></code> multiple times during a session or in a
script. It is particularly useful when the user is going to perform some
fetch operation, for example. In this case, it is recommended to
increase the <code>timeout_ms</code> and set
<code>verbose = FALSE</code>.</p>
<p>The <code>con</code> object that we created in the example above has
the <code>ImapCon</code> R6 class. Now, almost 99% of the other IMAP
commands to be performed on the server will be called following the
structure: <code>con$method()</code>. The exceptions are the
<code><a href="../reference/list_attachments.html">list_attachments()</a></code> function and the helper functions of the
custom-search group.</p>
<p>As you will see, the R6 framework combined with {curl} will make this
package works such a session-based IMAP client. Besides this, for some
commands, users will be able to use the tidy approach with pipes. All
this together provides an elegant way of accessing your IMAP provider,
searching and fetching emails, and managing your mailbox as well.</p>
</div>
<div id="server-capabilities" class="section level2">
<h2 class="hasAnchor">
<a href="#server-capabilities" class="anchor"></a>2) Server capabilities</h2>
<p>Once the connection is configured, it is important to know which
capabilities your IMAP provider offers to users. This impacts on which
type of operations you are allowed to perform. For example, if your
server has the <code>WITHIN</code> extension you can use the WITHIN
search methods <code>search_younger_than()</code> and
<code>search_older_than()</code>; if the server has the
<code>ESEARCH</code> capability, besides being allowed to use the
<code>esearch_*()</code> methods, you can optimize all your search
functions with the <code>esearch = TRUE</code> parameter; if you see the
<code>MOVE</code> capability, then you can use the
<code>move_msg()</code> method. Therefore, to know all your server
capabilities, you can use <code>list_server_capabliltites</code>.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">list_server_capabilities</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="mailbox-commands" class="section level2">
<h2 class="hasAnchor">
<a href="#mailbox-commands" class="anchor"></a>3) Mailbox commands</h2>
<div id="listing-folders" class="section level3">
<h3 class="hasAnchor">
<a href="#listing-folders" class="anchor"></a>3.1) Listing folders</h3>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">list_mail_folders</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="creating-a-new-folder" class="section level3">
<h3 class="hasAnchor">
<a href="#creating-a-new-folder" class="anchor"></a>3.2) Creating a new folder</h3>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">create_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"New Folder"</span><span class="op">)</span></span></code></pre></div>
<p>Except for <code>examine_folder()</code> and
<code>rename_folder()</code>, from now on, you will have to select a
folder to issue further commands.</p>
</div>
<div id="selecting-a-folder" class="section level3">
<h3 class="hasAnchor">
<a href="#selecting-a-folder" class="anchor"></a>3.3) Selecting a folder</h3>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"INBOX"</span><span class="op">)</span></span></code></pre></div>
<p>Probably, the main folder in your mailbox will be the
<code>"INBOX"</code>. You can select it without having to worry about
the case of the letters. However, all the other folders in a mailbox are
case sensitive.</p>
</div>
<div id="examining-a-folder" class="section level3">
<h3 class="hasAnchor">
<a href="#examining-a-folder" class="anchor"></a>3.4) Examining a folder</h3>
<p>Count the number of existent and recent messages in the previously
selected folder.</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Inbox"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">examine_folder</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p>If you want to examine a folder that is not the currently selected
one (<code>INBOX</code>), you can achieve this by specifying the
name.</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">examine_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Sent"</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="renaming-a-folder" class="section level3">
<h3 class="hasAnchor">
<a href="#renaming-a-folder" class="anchor"></a>3.5) Renaming a folder</h3>
<p>The following will rename the selected folder.</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"CRAN Messages"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">rename_folder</span><span class="op">(</span>new_name <span class="op">=</span> <span class="st">"CRAN"</span><span class="op">)</span></span></code></pre></div>
<p>There is a <code>reselect</code> argument, which by default is set to
<code>TRUE</code>. This will cause the automatic re-selection of the new
folder name.</p>
<p>If you want to rename a folder that is different from the currently
selected one (<code>CRAN</code>), you can achieve this by specifying the
name.</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">rename_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Sent"</span>, new_name <span class="op">=</span> <span class="st">"Sent2"</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="flags-listing" class="section level3">
<h3 class="hasAnchor">
<a href="#flags-listing" class="anchor"></a>3.6) Flags listing</h3>
<p>Flags work like tags or labels attached to messages. After a mail
folder is selected, you can check which flags are available, and if you
are allowed to set custom flags of your own in this folder with
<code>list_flags()</code>.</p>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">list_flags</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
<div id="single-search" class="section level2">
<h2 class="hasAnchor">
<a href="#single-search" class="anchor"></a>4) Single-search</h2>
<p>All search methods will return a numeric vector containing the
results from the search. This allows users to chain fetch operations
together with search one. You can also <strong>NEGATE</strong> all
search expressions by setting <code>negate = TRUE</code>.</p>
<p>If your server supports <strong>ESEARCH</strong>, we recommend you to
use it. It will prevent your results from being truncated when there are
too many messages ids and you didn’t set a high <code>buffersize</code>
in <code>confiure_imap()</code><a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>. With “ESEARCH”, the results will be
condensed to groups of sequences similar to what does. For instance, if
your search returns 10000 results, it is better to have condensed groups
such as <code>1:10, 12, 23:27, ...</code> instead of a sequence of
<code>1, 2, 3, 4, 5, 6, ..., 10, 12, 23, 24, 25, ...</code>. If you
can’t use ESEARCH, or if your results are being truncated even with
ESEARCH, you can try to increase your buffersize in
<code><a href="../reference/configure_imap.html">configure_imap()</a></code> to avoid this.</p>
<div id="search-by-date" class="section level3">
<h3 class="hasAnchor">
<a href="#search-by-date" class="anchor"></a>4.1) Search by date</h3>
<p><code>search_before()</code>, <code>search_since()</code>,
<code>search_on()</code>, and <code>search_period()</code> use internal
date, which reflects the moment when the message was received.
<code>search_sent_before()</code>, <code>search_sent_since()</code>,
<code>search_sent_on()</code>, and <code>search_sent_period()</code> use
the RFC-2822 date header (origination date), which “specifies the date
and time at which the creator of the message indicated that the message
was complete and ready to enter the mail delivery system” (Resnick,
2008). Dates in both methods must be the same most of the time. The
difference may occur when you copied or moved some messages between
folders. In this case, the RFC-2822 date header of the copied/moved
messages in the destination folder will point out to the date of the
copy. Another difference is that searching by the internal date will
probably be faster because this information is kept in a database
outside the message.</p>
<div id="by-internal-date" class="section level4">
<h4 class="hasAnchor">
<a href="#by-internal-date" class="anchor"></a>4.1.1) By internal date</h4>
</div>
<div id="before-a-date" class="section level4">
<h4 class="hasAnchor">
<a href="#before-a-date" class="anchor"></a>4.1.1.1) Before a date</h4>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"INBOX"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">res</span></span></code></pre></div>
<p>You can use the “UID” (unique identifier) instead of the message
sequence number <a href="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a>, and one or more flags as an additional
filter to your search. In fact, you can use this in almost every search
method of this package.</p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span>,</span>
<span> use_uid <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> flag <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ANSWERED"</span>, <span class="st">"SEEN"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">res</span></span></code></pre></div>
<p>Remember that, if your IMAP server has the ESEARCH capability, you
can use it. Gmail is one of the mail providers that allow it.</p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span>,</span>
<span> use_uid <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> flag <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ANSWERED"</span>, <span class="st">"SEEN"</span><span class="op">)</span>,</span>
<span> esearch <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span></span>
<span><span class="va">res</span></span></code></pre></div>
<p>You can also <strong>NEGATE</strong> the statement to search for
messages <strong>NOT BEFORE a date</strong>, for example:</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span>,</span>
<span> negate <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span> </span>
<span><span class="va">res</span></span></code></pre></div>
</div>
<div id="since-a-date" class="section level4">
<h4 class="hasAnchor">
<a href="#since-a-date" class="anchor"></a>4.1.1.2) Since a date</h4>
<p>The previous operation, in which we have used
<code>negate = TRUE</code>, is equivalent to search for messages
received <strong>SINCE a DATE</strong>:</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span>,</span>
<span> use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span> </span>
<span><span class="va">res</span></span></code></pre></div>
</div>
<div id="by-period" class="section level4">
<h4 class="hasAnchor">
<a href="#by-period" class="anchor"></a>4.1.1.3) By period</h4>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_period</span><span class="op">(</span>since_date_char <span class="op">=</span> <span class="st">"02-Jan-2020"</span>,</span>
<span> before_date_char <span class="op">=</span> <span class="st">"30-Jun-2020"</span><span class="op">)</span></span>
<span> </span>
<span><span class="va">res</span></span></code></pre></div>
<p>You can <strong>NEGATE</strong> a period search as well. In this
case, the search will exclude messages from the specified period.</p>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_period</span><span class="op">(</span>since_date_char <span class="op">=</span> <span class="st">"02-Jan-2020"</span>,</span>
<span> before_date_char <span class="op">=</span> <span class="st">"30-Jun-2020"</span>,</span>
<span> negate <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span> </span>
<span><span class="va">res</span></span></code></pre></div>
</div>
<div id="on-a-specific-date" class="section level4">
<h4 class="hasAnchor">
<a href="#on-a-specific-date" class="anchor"></a>4.1.1.4) On a specific date</h4>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_on</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"02-Jan-2020"</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="by-origination-date" class="section level4">
<h4 class="hasAnchor">
<a href="#by-origination-date" class="anchor"></a>4.1.2) By origination date</h4>
</div>
<div id="sent-before-a-date" class="section level4">
<h4 class="hasAnchor">
<a href="#sent-before-a-date" class="anchor"></a>4.1.2.1) Sent before a date</h4>
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_sent_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span><span class="op">)</span></span></code></pre></div>
<p>You can modify some of the search parameters as well:</p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_sent_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span>,</span>
<span> negate <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> use_uid <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span> flag <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ANSWERED"</span>, <span class="st">"SEEN"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">res</span></span></code></pre></div>
</div>
<div id="sent-since-a-date" class="section level4">
<h4 class="hasAnchor">
<a href="#sent-since-a-date" class="anchor"></a>4.1.2.2) Sent since a date</h4>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_sent_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="sent-by-period" class="section level4">
<h4 class="hasAnchor">
<a href="#sent-by-period" class="anchor"></a>4.1.2.3) Sent by period</h4>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_sent_period</span><span class="op">(</span>since_date_char <span class="op">=</span> <span class="st">"02-Jan-2020"</span>,</span>
<span> before_date_char <span class="op">=</span> <span class="st">"30-Jun-2020"</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="sent-on-a-specific-date" class="section level4">
<h4 class="hasAnchor">
<a href="#sent-on-a-specific-date" class="anchor"></a>4.1.2.4) Sent On a specific date</h4>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_sent_on</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"30-Jun-2020"</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
<div id="search-by-string" class="section level3">
<h3 class="hasAnchor">
<a href="#search-by-string" class="anchor"></a>4.2) Search by string</h3>
<p>You can search for a simple string or compound expression either in
the whole message, in a section, or in a specific header field. One
important thing to know is that the SEARCH command in the IMAP server is
not case sensitive.</p>
<p>You can also <strong>NEGATE</strong> the statement and search for
messages (or a specific part of a message) not containing that string,
and add additional flag filters as well.</p>
<p>For the next examples, we are going to select a different mail
folder.</p>
<p>Searching in the “TO” header field:</p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"K-State"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_string</span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"xpto@k-state.com"</span>, where <span class="op">=</span> <span class="st">"TO"</span><span class="op">)</span></span></code></pre></div>
<p>Searching in the “FROM” header field:</p>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_string</span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"xpto@k-state.edu"</span>, where <span class="op">=</span> <span class="st">"FROM"</span><span class="op">)</span></span></code></pre></div>
<p>Searching in the “SUBJECT” header field:</p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_string</span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"PhD offer"</span>, where <span class="op">=</span> <span class="st">"SUBJECT"</span><span class="op">)</span></span></code></pre></div>
<p>Searching in the “TEXT” section.</p>
<p><strong>IMPORTANT</strong>: Since the text may contain raw data, it
may not be a super-effective search. In this case, searching for a
expression in the whole <code>"BODY"</code> may be preferred.</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>con<span class="sc">$</span><span class="fu">search_string</span>(<span class="at">expr =</span> <span class="st">"Dear Allan"</span> <span class="at">where =</span> <span class="st">"TEXT"</span>)</span></code></pre></div>
<p>Searching in the “BODY” section.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>con<span class="sc">$</span><span class="fu">search_string</span>(<span class="at">expr =</span> <span class="st">"Dear Allan"</span> <span class="at">where =</span> <span class="st">"BODY"</span>)</span></code></pre></div>
</div>
<div id="search-by-flag" class="section level3">
<h3 class="hasAnchor">
<a href="#search-by-flag" class="anchor"></a>4.3) Search by flag</h3>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_flag</span><span class="op">(</span>name <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"ANSWERED"</span>, <span class="st">"Seen"</span><span class="op">)</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p>Remember that you can check the available flags in a mail folder with
<code>list_flags()</code>.</p>
</div>
<div id="search-by-size" class="section level3">
<h3 class="hasAnchor">
<a href="#search-by-size" class="anchor"></a>4.4) Search by size</h3>
<p>The size is specified in bytes.</p>
<div id="smaller-than" class="section level4">
<h4 class="hasAnchor">
<a href="#smaller-than" class="anchor"></a>4.4.1) Smaller than</h4>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_smaller_than</span><span class="op">(</span>size <span class="op">=</span> <span class="fl">512000</span><span class="op">)</span> <span class="co"># smaller than 512KB</span></span></code></pre></div>
</div>
<div id="larger-than" class="section level4">
<h4 class="hasAnchor">
<a href="#larger-than" class="anchor"></a>4.4.1) Larger than</h4>
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_larger_than</span><span class="op">(</span>size <span class="op">=</span> <span class="fl">512000</span><span class="op">)</span> <span class="co"># larger than 512KB</span></span></code></pre></div>
</div>
</div>
<div id="search-by-within-extension" class="section level3">
<h3 class="hasAnchor">
<a href="#search-by-within-extension" class="anchor"></a>4.5) Search by within extension</h3>
<p>Severs with support to the “WITHIN” EXTENSION enable searching for
messages within a span, i.e. younger than “x” seconds, or older than “x”
seconds. This capability is really rare to find in IMAP servers, but
<code>mRpostman</code> has two methods implemented for coping with this
capability if it is available.</p>
<div id="younger-than" class="section level4">
<h4 class="hasAnchor">
<a href="#younger-than" class="anchor"></a>4.5.1) Younger than</h4>
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_younger_than</span><span class="op">(</span>seconds <span class="op">=</span> <span class="fl">3600</span><span class="op">)</span> <span class="co"># msgs received less than one hour (3600 sec)</span></span></code></pre></div>
</div>
<div id="older-than" class="section level4">
<h4 class="hasAnchor">
<a href="#older-than" class="anchor"></a>4.5.2) Older than</h4>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_older_than</span><span class="op">(</span>seconds <span class="op">=</span> <span class="fl">3600</span><span class="op">)</span> <span class="co"># msgs received more than one hour ago (3600 sec)</span></span></code></pre></div>
</div>
</div>
</div>
<div id="custom-search" class="section level2">
<h2 class="hasAnchor">
<a href="#custom-search" class="anchor"></a>5) Custom-search</h2>
<p>The <code><a href="https://rdrr.io/r/base/search.html">search()</a></code> method and its <strong>helper
functions</strong> enable users to create a vast number of complex and
customized search requests by combining different criteria, using all
the types of searches previously presented in this document.</p>
<p>These are the helper functions you can use inside
<code><a href="https://rdrr.io/r/base/search.html">search()</a></code>:</p>
<ol style="list-style-type: decimal">
<li>Relational operators: <code><a href="../reference/AND.html">AND()</a></code>, and
<code><a href="../reference/OR.html">OR()</a></code>;</li>
<li>Criteria definition: <code><a href="../reference/before.html">before()</a></code>, <code><a href="../reference/since.html">since()</a></code>,
<code><a href="../reference/on.html">on()</a></code>, <code><a href="../reference/sent_before.html">sent_before()</a></code>,
<code><a href="../reference/sent_since.html">sent_since()</a></code>, <code><a href="../reference/sent_on.html">sent_on()</a></code>,
<code><a href="../reference/string.html">string()</a></code>, <code><a href="../reference/flag.html">flag()</a></code>, <code><a href="../reference/smaller_than.html">smaller_than()</a></code>,
<code><a href="../reference/larger_than.html">larger_than()</a></code>, <code><a href="../reference/younger_than.html">younger_than()</a></code>, and
<code><a href="../reference/older_than.html">older_than()</a></code>.</li>
</ol>
<p><strong>NOTE</strong>: IMAP queries follows Polish notation,
i.e. operators such as <code>OR</code> come before arguments, e.g. “OR
argument1 argument2”. Therefore, the relational operators functions in
this package should be used like the following examples:
<code>OR(before(date_char = "17-Apr-2015"), string(expr = "Jimmy", where = "FROM"))</code>.
Even though there is no “AND” operator in the IMAP protocol, this
package adds a helper function <code><a href="../reference/AND.html">AND()</a></code> to indicate multiple
arguments that must be searched together,
e.g. <code>AND(since(date_char = "01-Jul-2018"), larger_than(size = 16000))</code>.</p>
<p><strong>Example 1</strong>: Searching for messages (in “INBOX”)
containing the string “Kansas State University” in the “SUBJECT” header
field <strong>AND</strong> that were received before “02-Jan-2020”.</p>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"INBOX"</span><span class="op">)</span></span>
<span></span>
<span></span>
<span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search</span><span class="op">(</span>request <span class="op">=</span> <span class="fu"><a href="../reference/AND.html">AND</a></span><span class="op">(</span><span class="fu"><a href="../reference/string.html">string</a></span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"Kansas State University"</span>, where <span class="op">=</span> <span class="st">"SUBJECT"</span><span class="op">)</span>,</span>
<span> <span class="fu"><a href="../reference/before.html">before</a></span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"02-Jan-2020"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">res</span></span></code></pre></div>
<p><strong>Example 2</strong>: Searching (using UID) for messages
received from “<span class="citation">@k-state.edu</span>”
<strong>OR</strong> “<span class="citation">@ksu.edu</span>”.</p>
<div class="sourceCode" id="cb35"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search</span><span class="op">(</span>request <span class="op">=</span> <span class="fu"><a href="../reference/OR.html">OR</a></span><span class="op">(</span><span class="fu"><a href="../reference/string.html">string</a></span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"@k-state.edu"</span>, where <span class="op">=</span> <span class="st">"FROM"</span><span class="op">)</span>,</span>
<span> <span class="fu"><a href="../reference/string.html">string</a></span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"@ksu.edu"</span>, where <span class="op">=</span> <span class="st">"FROM"</span><span class="op">)</span><span class="op">)</span>,</span>
<span> use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="fetch" class="section level2">
<h2 class="hasAnchor">
<a href="#fetch" class="anchor"></a>6) Fetch</h2>
<p>You can fetch the full content of messages, or their parts, such as
the header, text, or specific metadata fields. Besides this, you can
also fetch a message attachments list or the attachment files
themselves, downloading them to the disk.</p>
<p>We usually fetch messages after a search operation. Given the output
of the search functions in <strong>mRpostman</strong>, you can use the
pipe <code>%>%</code> to chain the search and the fetch operations
together. Using the base R approach is perfectly possible as well.</p>
<p>In the main fetch methods (those that are not related to attachment
fetching), you can choose to write the fetch results to disk (working
directory) using <code>write_to_disk = TRUE</code>. If you opt to do so,
<strong>mRpostman</strong> saves the fetched content to a
<code>.txt</code> file in the following folder structure:
<code>working directory > imap.server.url > mail folder name</code>.
The text files will be named after the id of the fetched message. If the
operation was executed using the UID, the “UID” prefix is added to the
file names.</p>
<p><strong>IMPORTANT</strong>:</p>
<ol style="list-style-type: decimal">
<li><p>If the fetch operation is to be chained after a search, the
<code>use_uid</code> arguments in the two operations have to be the
same. Otherwise, an error will occur or the fetch will be performed on
wrong messages’ ids.</p></li>
<li><p>It is always recommended to increase the timeout_ms before
<code>fetch_body()</code>, <code>fetch_text()</code>, and
<code>fetch_attachents()</code> operations as sometimes the operation
may hang for a few seconds while fetching the message parts.</p></li>
<li><p>if you have configured a connection with
<code>verbose = TRUE</code>, it is extremely recommended that you reset
it to <code>FALSE</code> before a fetching operation. The
<code>verbose = TRUE</code> option fill the console with the whole flux
of information between the server and the client, drastically slowing
the speed of the process and your R session.</p></li>
</ol>
<div id="fetch-body" class="section level3">
<h3 class="hasAnchor">
<a href="#fetch-body" class="anchor"></a>6.1) Fetch body</h3>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># increasing timeout_ms</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">reset_timeout_ms</span><span class="op">(</span>x <span class="op">=</span> <span class="fl">30000</span><span class="op">)</span> <span class="co"># ... to 30 secs</span></span>
<span></span>
<span><span class="co"># and supposing that you had verbose = TRUE before</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">reset_verbose</span><span class="op">(</span>x <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># tidy approach</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_string</span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"@k-state.edu"</span>, where <span class="op">=</span> <span class="st">"FROM"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_body</span><span class="op">(</span>write_to_disk <span class="op">=</span> <span class="cn">TRUE</span>, keep_in_mem <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># ---------------</span></span>
<span></span>
<span><span class="co"># base R approach</span></span>
<span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_string</span><span class="op">(</span>expr <span class="op">=</span> <span class="st">"@k-state.edu"</span>, where <span class="op">=</span> <span class="st">"FROM"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">fetch_body</span><span class="op">(</span>msg_id <span class="op">=</span> <span class="va">res</span>, write_to_disk <span class="op">=</span> <span class="cn">TRUE</span>, keep_in_mem <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></code></pre></div>
<p>Since the goal here is to write the fetch results to disk, it is
recommended that we set <code>keep_in_mem = FALSE</code>. This will
optimize the whole operation because <code>mRpostman</code> will clean
the memory after fetching each message as we are not going to use the
results in our R session.</p>
<p>Our local folder will be populated with the <code>.txt</code> files
of the fetched messages:</p>
<p><img src="figures/basics/fetch-body.png" style="width:80.0%"></p>
</div>
<div id="fetch-header" class="section level3">
<h3 class="hasAnchor">
<a href="#fetch-header" class="anchor"></a>6.2) Fetch header</h3>
<div class="sourceCode" id="cb37"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># tidy approach</span></span>
<span><span class="va">out</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"15-Aug-2019"</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_header</span><span class="op">(</span>use_uid <span class="op">=</span> <span class="cn">TRUE</span>, fields <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"DATE"</span>, <span class="st">"SUBJECT"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">out</span></span>
<span></span>
<span><span class="co"># ---------------</span></span>
<span></span>
<span><span class="co"># base R approach</span></span>
<span><span class="va">res</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"15-Aug-2019"</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span></span>
<span><span class="va">out</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_header</span><span class="op">(</span>use_uid <span class="op">=</span> <span class="cn">TRUE</span>, fields <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"DATE"</span>, <span class="st">"SUBJECT"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="va">out</span></span></code></pre></div>
<p>Please, note that, in the example above, we are saving the results to
the <code>out</code> object in our R session. Also note that we are
setting <code>use_uid = TRUE</code> in both search and fetch
requests.</p>
</div>
<div id="fetch-text" class="section level3">
<h3 class="hasAnchor">
<a href="#fetch-text" class="anchor"></a>6.3) Fetch text</h3>
<p><code>fetch_text()</code> is almost as costly as
<code>fetch_body()</code>. So, it is a good idea to keep a “high”
<code>timeout_ms</code>.</p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"15-Aug-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_text</span><span class="op">(</span>write_to_disk <span class="op">=</span> <span class="cn">TRUE</span>, keep_in_mem <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="fetch-metadata" class="section level3">
<h3 class="hasAnchor">
<a href="#fetch-metadata" class="anchor"></a>6.4) Fetch metadata</h3>
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">out</span> <span class="op"><-</span> <span class="va">con</span><span class="op">$</span><span class="fu">search_on</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"15-Aug-2019"</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_metadata</span><span class="op">(</span>use_uid <span class="op">=</span> <span class="cn">TRUE</span>, attribute <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"INTERNALDATE"</span>, <span class="st">"UID"</span>, <span class="st">"ENVELOPE"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p>If nothing is specified to the <code>metadata</code> argument, all
the metadata fields are fetched by default. To know which are the
metadata options of a message, refer to
<code><a href="../reference/metadata_options.html">metadata_options()</a></code>.</p>
<p>There are two more fetch methods, but they are going to be presented
in the next session since they are related to a very special fetch
operation.</p>
</div>
</div>
<div id="attachments" class="section level2">
<h2 class="hasAnchor">
<a href="#attachments" class="anchor"></a>7) Attachments</h2>
<p>Previously, <code>mRpostman</code> had two attachment-related
functions: <code><a href="../reference/list_attachments.html">list_attachments()</a></code> and
<code>get_attachments()</code>. Both were to be used after a fetching
operation because they would use the content fetched in a previous step
to parse the attachments list and/or decode the content to files. These
methods are still available in the current version of the package.
However, two more methods were implemented since version
<code>0.9.0.0</code>: <code>fetch_attachments_list()</code> and
<code>fetch_attachments()</code>. These methods are not contingent on a
previous fetch operation, since they are a properly fetch operation.</p>
<p>Let’s take a look at the first two attachments functionalities
available in <code>mRpostman</code>.</p>
<div id="dependent-attachment-operations" class="section level3">
<h3 class="hasAnchor">
<a href="#dependent-attachment-operations" class="anchor"></a>7.1) Dependent attachment operations</h3>
<p>The dependent operations are comprised of a function
<code>list_atachments()</code> and a R6 method
<code>get_attachments()</code>. We call them dependent operations
because they dependent on a previous fetch operation with
<code>fetch_body()</code> or <code>fetch_text()</code>.</p>
<p><strong>IMPORTANT:</strong> Since these are fetch-dependent
operations, you <strong>cannot</strong> set
<code>keep_in_mem = FALSE</code> in the
<code>fetch_body()</code>/<code>fetch_text()</code> step if you intend
to list or get attachments after fetching your messages.</p>
<div id="listing-attachments" class="section level4">
<h4 class="hasAnchor">
<a href="#listing-attachments" class="anchor"></a>7.1.1) Listing attachments</h4>
<p>This will list your attachments file names for each fetched
message.</p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># using fetch_body()</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"23-Sep-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_body</span><span class="op">(</span>write_to_disk <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="fu"><a href="../reference/list_attachments.html">list_attachments</a></span><span class="op">(</span><span class="op">)</span> <span class="co"># don't need to specify the connection object here</span></span>
<span></span>
<span><span class="co"># ------------------</span></span>
<span></span>
<span><span class="co"># using fetch_text()</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"23-Sep-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_text</span><span class="op">(</span>write_to_disk <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="fu"><a href="../reference/list_attachments.html">list_attachments</a></span><span class="op">(</span><span class="op">)</span> <span class="co"># don't need to specify the connection object here</span></span></code></pre></div>
<p>In both examples, the whole message body(ies) or text will be saved
to the disk, but a copy will be kept in the R session. This copy is
passed forward to <code><a href="../reference/list_attachments.html">list_attachments()</a></code>, which, by its turn,
finds and extract the filenames and the type of the
“Content-Disposition” for the part where the attachment were found.</p>
</div>
<div id="extracting-attachments" class="section level4">
<h4 class="hasAnchor">
<a href="#extracting-attachments" class="anchor"></a>7.1.2) Extracting Attachments</h4>
<p><code>get_attachments()</code> will try to decode base64 text inside
your message to the appropriate file extension. This will create a
folder with the inside the structure
<code>working directory > username > mail folder > msg_id</code>.
The attachment(s) will be inside their respective message folder.</p>
<div class="sourceCode" id="cb41"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># using fetch_body()</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"23-Sep-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_body</span><span class="op">(</span>write_to_disk <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">get_attachments</span><span class="op">(</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># ------------------</span></span>
<span></span>
<span><span class="co"># using fetch_text()</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"23-Sep-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_text</span><span class="op">(</span>write_to_disk <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">get_attachments</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p>The results in your local folder will be like this:</p>
<p><img src="figures/basics/attach1.png" style="width:80.0%"><img src="figures/basics/attach2.png" style="width:80.0%"></p>
</div>
</div>
<div id="direct-attachment-fetch-operations" class="section level3">
<h3 class="hasAnchor">
<a href="#direct-attachment-fetch-operations" class="anchor"></a>7.2) Direct attachment fetch operations</h3>
<p>As we stated before, the following two methods are not contingent on
a previous fetch step because they execute they own fetching operation.
Hence, they consist of an alternative for attachments listing and
downloading.</p>
<p><strong>IMPORTANT</strong>: The same recommendations for the other
fetch methods hold in this case: set <code>verbose = FALSE</code> and a
high <code>timeout_ms</code>.</p>
<div id="fetch-attachments-list" class="section level4">
<h4 class="hasAnchor">
<a href="#fetch-attachments-list" class="anchor"></a>7.2.1) Fetch attachments list</h4>
<div class="sourceCode" id="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"23-Sep-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_attachments_list</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="fetch-attachments" class="section level4">
<h4 class="hasAnchor">
<a href="#fetch-attachments" class="anchor"></a>7.2.2) Fetch attachments</h4>
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"23-Sep-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">fetch_attachments</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
</div>
</div>
</div>
<div id="complementary-operations" class="section level2">
<h2 class="hasAnchor">
<a href="#complementary-operations" class="anchor"></a>8) Complementary operations</h2>
<p>Here we present other functions to perform very useful complementary
IMAP operations.</p>
<div id="copy-messages" class="section level3">
<h3 class="hasAnchor">
<a href="#copy-messages" class="anchor"></a>8.1) Copy message(s)</h3>
<p>Copying search results from “INBOX” to “K-State” folder:</p>
<div class="sourceCode" id="cb44"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"INBOX"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"10-may-2019"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">copy_msg</span><span class="op">(</span>to_folder <span class="op">=</span> <span class="st">"K-State"</span><span class="op">)</span></span></code></pre></div>
<p>It will automatically re-select the destination folder unless the
user sets <code>reselect = FALSE</code>.</p>
</div>
<div id="get-minimum-message-id" class="section level3">
<h3 class="hasAnchor">
<a href="#get-minimum-message-id" class="anchor"></a>8.2) Get minimum message id</h3>
<p>This operation depends on the ESEARCH capability. It will retrieve
the <strong>minimum</strong> message id containing a specific flag(s) in
the selected mail folder.</p>
<div class="sourceCode" id="cb45"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">esearch_min_id</span><span class="op">(</span>flag <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Answered"</span>, <span class="st">"Seen"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="get-maximum-message-id" class="section level3">
<h3 class="hasAnchor">
<a href="#get-maximum-message-id" class="anchor"></a>8.3) Get maximum message id</h3>
<p>This operation also depends on the ESEARCH capability. It will
retrieve the <strong>maximum</strong> message id containing a specific
flag(s) in the selected mail folder.</p>
<div class="sourceCode" id="cb46"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">esearch_min_id</span><span class="op">(</span>flag <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Answered"</span>, <span class="st">"Seen"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="count-messages" class="section level3">
<h3 class="hasAnchor">
<a href="#count-messages" class="anchor"></a>8.4) Count messages</h3>
<p>This operation also depends on the ESEARCH capability. It will
retrieve the <strong>number</strong> of messages with a specific flag(s)
in the selected mail folder.</p>
<div class="sourceCode" id="cb47"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">esearch_count</span><span class="op">(</span>flag <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Answered"</span>, <span class="st">"Seen"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="delete-messages" class="section level3">
<h3 class="hasAnchor">
<a href="#delete-messages" class="anchor"></a>8.5) Delete message(s)</h3>
<p>This method marks one or more messages with the “\Deleted” system
flag. Some servers automatically delete messages marked with this flag,
and others require the <code>EXPUNGE</code> command to permanently
delete the e-mail.</p>
<div class="sourceCode" id="cb48"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"Trash"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_before</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"10-may-2012"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">delete_msg</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p>Deleting an specific “msg_id” without a previous search:</p>
<div class="sourceCode" id="cb49"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">delete_msg</span><span class="op">(</span>msg_id <span class="op">=</span> <span class="fl">66128</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="expunge" class="section level3">
<h3 class="hasAnchor">
<a href="#expunge" class="anchor"></a>8.6) Expunge</h3>
<p>Expunges message(s) marked with the “DELETED” flag in a mailbox or a
specific message using the <code>msg_uid</code> argument. Please, note
that the this requires the unique id, not sequence numbers. Therefore,
we set <code>use_uid = TRUE</code></p>
<div class="sourceCode" id="cb50"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># expunge the entire mail folder</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">expunge</span><span class="op">(</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># expunge selected msg UID</span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">delete_msg</span><span class="op">(</span>msg_id <span class="op">=</span> <span class="fl">71171</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="fu">expunge</span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="addremovereplace-flags" class="section level3">
<h3 class="hasAnchor">
<a href="#addremovereplace-flags" class="anchor"></a>8.7) Add/Remove/Replace flags</h3>
<p>Adding, removing and replacing one or more flags to messages.</p>
<p><strong>IMPORTANT</strong>: Differently from the search functions
where the (system) flags passed as additional parameters to search
methods did not contain “\”, the <code>add/replace/remove_flags()</code>
methods require the double backslash when referring to system flags. You
can know which are the flags of a mail folder, and if custom flags are
allowed, using <code>list_flags()</code>.</p>
<div id="add-flags" class="section level4">
<h4 class="hasAnchor">
<a href="#add-flags" class="anchor"></a>8.7.1) Add flags</h4>
<div class="sourceCode" id="cb51"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">select_folder</span><span class="op">(</span>name <span class="op">=</span> <span class="st">"INBOX"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">con</span><span class="op">$</span><span class="fu">search_since</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"01-Sep-2020"</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">add_flags</span><span class="op">(</span>flags_to_set <span class="op">=</span> <span class="st">"\\Answered"</span>, use_uid <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
</div>
<div id="replace-flags" class="section level4">
<h4 class="hasAnchor">
<a href="#replace-flags" class="anchor"></a>8.7.2) Replace flags</h4>
<p>Replaces the existent flags by the one(s) specified in the
method.</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>con<span class="sc">$</span><span class="fu">search_since</span>(<span class="at">date_char =</span> <span class="st">"01-Sep-2020"</span>, <span class="at">use_uid =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a> con<span class="sc">$</span><span class="fu">replace_flags</span>(<span class="at">flags_to_set =</span> <span class="fu">c</span>(<span class="st">"</span><span class="sc">\\</span><span class="st">Seen"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">Flagged"</span>, <span class="at">use_uid =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
</div>
<div id="remove-flags" class="section level4">
<h4 class="hasAnchor">
<a href="#remove-flags" class="anchor"></a>8.7.3) Remove flags</h4>
<p>Now we have the <code>flags_to_UNset</code> argument.</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>con<span class="sc">$</span><span class="fu">search_since</span>(<span class="at">date_char =</span> <span class="st">"01-Sep-2020"</span>, <span class="at">use_uid =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a> con<span class="sc">$</span><span class="fu">remobe_flags</span>(<span class="at">flags_to_unset =</span> <span class="fu">c</span>(<span class="st">"</span><span class="sc">\\</span><span class="st">Seen"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">Flagged"</span>, <span class="at">use_uid =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
</div>
</div>
<div id="move-messages" class="section level3">
<h3 class="hasAnchor">
<a href="#move-messages" class="anchor"></a>8.8) Move message(s)</h3>
<p><code>move_msg()</code> uses IMAP “MOVE” EXTENSION. Check if your
server supports the “MOVE” capability with
<code>list_server_capabilities()</code>.</p>
<div class="sourceCode" id="cb54"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">con</span><span class="op">$</span><span class="fu">search_on</span><span class="op">(</span>date_char <span class="op">=</span> <span class="st">"07-Sep-2020"</span><span class="op">)</span> <span class="op"><a href="../reference/%>%.html">%>%</a></span></span>
<span> <span class="va">con</span><span class="op">$</span><span class="fu">move_msg</span><span class="op">(</span>to_folder <span class="op">=</span> <span class="st">"K-State"</span><span class="op">)</span></span></code></pre></div>
<p>If your server does not provide “MOVE” capability, the same result