blib/lib/Outlook/Mail/Count.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Outlook::Mail::Count;
2
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Date::Calc qw(:all);
8 our @ISA = qw( Exporter );
9 our @EXPORT = qw( run );
10
11 =head1 NAME
12
13 Outlook::Mail::Count - The module is work for windows platform for collection email number by folder and date.
14 output sample:
15 Folder Total Today Yesterday ThisWeek LastWeek ThisMonth LastMonth ThisYear LastsYear
16 Inbox 480 19 15 42 90 42 391 451 29
17 Cloud 243 0 0 0 2 0 9 85 158
18 None 35 0 0 0 0 0 0 12 23
19 IT 242 2 1 5 8 5 81 180 62
20 Systems 732 2 6 15 49 15 177 378 354
21 Devops 293 0 0 0 0 0 1 122 171
22 DB 67 0 0 0 0 0 0 8 59
23 Security 411 2 2 5 3 5 31 149 262
24 Manager 104 0 0 0 2 0 11 34 70
25 RM 146 0 0 0 0 0 3 38 108
26 Application 348 0 0 0 0 0 8 139 209
27 HR 0 0 0 0 0 10 61 130
28 QA 628 0 0 0 0 0 5 164 464
29 O-IT 1286 5 3 11 28 11 87 417 869
30 ME 1 0 1 1 0 1 0 1 0
31
32 =head1 VERSION
33
34 Version 1.01
35
36 =cut
37
38 our $VERSION = '1.01';
39 use Win32::OLE qw(in with);
40 use Win32::OLE::Const 'Microsoft Outlook';
41 $Win32::OLE::Warn = 2;
42 my $dt = DateTime->now;
43 my $today = $dt->ymd('-');
44 my $week = $dt->week_number(); #day_of_week();
45 my $month = $dt->month();
46 my $year = $dt->year();
47 my $yesterday = $dt->add( days => -1 )->ymd('-');
48
49 my @FIELDS = (
50 [ qw( Email1Address Email1DisplayName) ],
51 [ qw( Email2Address Email2DisplayName) ],
52 [ qw( Email3Address Email3DisplayName) ],
53 );
54
55 my $outlook = get_outlook();
56
57
58 my $mapi = $outlook->GetNamespace('MAPI');
59 my $inbox = $mapi->GetDefaultFolder(olFolderInbox);
60
61 =head1 SYNOPSIS
62
63 Quick summary of what the module does.
64
65 use Outlook::Mail::Count qw (run);
66 run();
67
68
69 =head1 EXPORT
70
71 run - Invoke count subroutines passthrough folder parameter.
72
73 =head1 SUBROUTINES/METHODS
74
75 run - Invoke count subroutines passthrough folder parameter.
76 count - count folder emails.
77
78 =cut
79
80 sub run {
81 printf("%30s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n","Folder","Total","Today","Yesterday","ThisWeek","LastWeek","ThisMonth","LastMonth", "ThisYear","LastsYear");
82 my $count = $inbox->{Items}{Count};
83 count($inbox);
84
85 foreach ( in $inbox->Folders) {
86 count($_);
87 }
88 }
89
90
91 =head2 count
92
93 =cut
94
95 sub count {
96 my $subfolder = shift;
97 my $c = 0;
98 my $y = 0;
99 my $this_week = 0;
100 my $last_week = 0;
101 my $m = 0;
102 my $lm = 0;
103 my $ty = 0;
104 my $ly = 0;
105 foreach my $msg (reverse in($subfolder->{items})) {
106
107 #print $msg->{Received};die;$msg->{EntryID}
108 my $dt2 = DateTime->new(
109 year => $msg->{ReceivedTime}->Date("yyyy"),
110 month => $msg->{ReceivedTime}->Date("MM"),
111 day => $msg->{ReceivedTime}->Date("dd"),
112 hour => $msg->{ReceivedTime}->Time("HH"),
113 minute => $msg->{ReceivedTime}->Time("mm"),
114 time_zone => "Asia/Chongqing",
115 );
116
117 my $dt2_day_of_week = $dt2->week_number();
118 my $dt2_year = $dt2->year;
119 my $dt2_day = $msg->{ReceivedTime}->Date("yyyy-MM-dd");
120 my $d_o_w = $week - $dt2_day_of_week;
121
122 if( $dt2_year == $year ) {
123 $ty = $ty + 1;
124 if( $dt2_day =~ /$today/ ) { $c = $c + 1; }
125 if( $dt2_day =~ /$yesterday/ ) { $y = $y + 1; }
126 if( $d_o_w == 0 ) {
127
128 $this_week = $this_week + 1;
129 }
130 if( $d_o_w == 1 ) { $last_week = $last_week + 1; }
131 if( $dt2->month() == $month ) { $m = $m + 1; }
132 if(( $month - $dt2->month()) == 1 ) { $lm = $lm + 1; }
133 } else {
134 $ly = $ly + 1;
135 }
136 }
137 #printf("%30s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n","Folder","Total","Today","Yesterday","ThisWeek","LastWeek","ThisMonth","LastMonth", "ThisYear","LastsYear");
138 printf("%30s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n", $subfolder->{Name},$subfolder->{Items}{Count}, $c, $y, $this_week, $last_week, $m, $lm, $ty, $ly);
139
140 }
141
142 sub content {
143
144 foreach my $msg (reverse in($inbox->{items})) {
145 printf("%10s,%8s,%20s\n", $msg->{ReceivedTime}->Date("yyyy-MM-dd"), $msg->{ReceivedTime}->Time("HH:mm:ss"), $msg->{SenderEmailAddress});
146 }
147
148 }
149
150 sub contact {
151 my $contacts = $mapi->GetDefaultFolder(olFolderContacts);
152
153 my $count = $contacts->{Items}{Count};
154
155 print qq{"name","Email Address"\n};
156
157 for my $k (1 .. $count) {
158 my $contact = $contacts->{Items}->Item($k);
159
160 for my $field ( @FIELDS ) {
161 if ( (my $addr = $contact->{ $field->[0] })
162 and (my $name = $contact->{ $field->[1]}) ) {
163 $name =~ s{(\s+\(.+\))}{};
164 printf qq{"%s", %s\n}, $name, $addr;
165 }
166 }
167 }
168 }
169
170 sub get_outlook {
171 my $outlook;
172 eval {
173 $outlook = Win32::OLE->GetActiveObject('Outlook.Application');
174 };
175 die "$@\n" if $@;
176 return $outlook if defined $outlook;
177
178 $outlook = Win32::OLE->new('Outlook.Application', sub { $_[0]->Quit })
179 or die "Oops, cannot start Outlook: ",
180 Win32::OLE->LastError, "\n";
181 }
182
183 sub emulate {
184 my $wd = Win32::OLE::Const->Load($inbox->{Items}[1]);
185 foreach my $key (keys %$wd) {
186 printf "$key = %s\n", $wd->{$key};
187 }
188 exit;
189 }
190
191 =head1 AUTHOR
192
193 Linus Yuan, C<< <yuan_shijiang at 163.com> >>
194
195 =head1 BUGS
196
197 Please report any bugs or feature requests to C<bug-outlook-mail-count at rt.cpan.org>, or through
198 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Outlook-Mail-Count>. I will be notified, and then you'll
199 automatically be notified of progress on your bug as I make changes.
200
201
202
203
204 =head1 SUPPORT
205
206 You can find documentation for this module with the perldoc command.
207
208 perldoc Outlook::Mail::Count
209
210
211 You can also look for information at:
212
213 =over 4
214
215 =item * RT: CPAN's request tracker (report bugs here)
216
217 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Outlook-Mail-Count>
218
219 =item * AnnoCPAN: Annotated CPAN documentation
220
221 L<http://annocpan.org/dist/Outlook-Mail-Count>
222
223 =item * CPAN Ratings
224
225 L<http://cpanratings.perl.org/d/Outlook-Mail-Count>
226
227 =item * Search CPAN
228
229 L<http://search.cpan.org/dist/Outlook-Mail-Count/>
230
231 =back
232
233
234 =head1 ACKNOWLEDGEMENTS
235
236
237 =head1 LICENSE AND COPYRIGHT
238
239 Copyright 2018 Linus Yuan.
240
241 This program is free software; you can redistribute it and/or modify it
242 under the terms of the the Artistic License (2.0). You may obtain a
243 copy of the full license at:
244
245 L<http://www.perlfoundation.org/artistic_license_2_0>
246
247 Any use, modification, and distribution of the Standard or Modified
248 Versions is governed by this Artistic License. By using, modifying or
249 distributing the Package, you accept this license. Do not use, modify,
250 or distribute the Package, if you do not accept this license.
251
252 If your Modified Version has been derived from a Modified Version made
253 by someone other than you, you are nevertheless required to ensure that
254 your Modified Version complies with the requirements of this license.
255
256 This license does not grant you the right to use any trademark, service
257 mark, tradename, or logo of the Copyright Holder.
258
259 This license includes the non-exclusive, worldwide, free-of-charge
260 patent license to make, have made, use, offer to sell, sell, import and
261 otherwise transfer the Package with respect to any patent claims
262 licensable by the Copyright Holder that are necessarily infringed by the
263 Package. If you institute patent litigation (including a cross-claim or
264 counterclaim) against any party alleging that the Package constitutes
265 direct or contributory patent infringement, then this Artistic License
266 to you shall terminate on the date that such litigation is filed.
267
268 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
269 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
270 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
271 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
272 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
273 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
274 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
275 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276
277
278 =cut
279
280 1; # End of Outlook::Mail::Count
↑ Summary