blib/lib/Mail/DMARC/Report/Send/SMTP.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 193 CC 10 SCAR 21.0
CC
10
Cov
0%
get_domain_mx
30.0
get_smtp_hosts
6.0
get_subject
6.0
CRAP 8.2
1 package Mail::DMARC::Report::Send::SMTP;
2 use strict;
3 use warnings;
4 use feature 'signatures';
5 use feature 'try';
6 no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
7  
8 our $VERSION = '2.20260827';
9  
10 use Carp;
11 use Email::MIME;
12  
13 #use Mail::Sender; # something to consider
14 use Sys::Hostname;
15 use POSIX;
16  
17 use parent 'Mail::DMARC::Base';
18  
19 sub get_domain_mx( $self, $domain ) {
20 print "getting MX for $domain\n";
21 my $query;
22 try {
23 $query = $self->get_resolver->send( $domain, 'MX' );
24 }
25 catch ($error) {
26 print "\terror: $error";
27 }
28 return [] if !$query;
29  
30 my @mx;
31 for my $rr ( $query->answer ) {
32 next if $rr->type ne 'MX';
33 push @mx, { pref => $rr->preference, addr => $rr->exchange };
34 print $rr->exchange if $self->verbose;
35 }
36 if ( $self->verbose ) {
37 print "found " . scalar @mx . "MX exchanges\n";
38 }
39 return \@mx;
40 }
41  
42 sub get_smtp_hosts( $self, $email = undef ) {
43 $email or croak "missing email!";
44  
45 my ($domain) = ( split /@/, $email )[-1];
46 my @mx = map { $_->{addr} }
47 sort { $a->{pref} <=> $b->{pref} } @{ $self->get_domain_mx($domain) };
48  
49 push @mx, $domain;
50 print "\tfound " . scalar @mx . " MX for $email\n" if $self->verbose;
51 return @mx;
52 }
53  
54 sub get_subject( $self, $agg_ref ) {
55  
56 my $rid = $$agg_ref->metadata->report_id || $self->time;
57 my $id = POSIX::strftime( "%Y.%m.%d.", localtime $self->time ) . $rid;
58 my $us = $self->config->{organization}{domain};
59 if ( $us eq 'example.com' ) {
60 die "Please update mail-dmarc.ini";
61 }
62 my $pol_dom = $$agg_ref->policy_published->domain;
63 return "Report Domain: $pol_dom Submitter: $us Report-ID:$id";
64 }
65  
66 sub human_summary( $self, $agg_ref ) {
67  
68 my $records = scalar @{ $$agg_ref->{record} };
69 my $OrgName = $self->config->{organization}{org_name};
70 my $pass = grep {
71 'pass' eq $_->{row}{policy_evaluated}{dkim}
72 || 'pass' eq $_->{row}{policy_evaluated}{spf}
73 } @{ $$agg_ref->{record} };
74 my $fail = grep {
75 'pass' ne $_->{row}{policy_evaluated}{dkim}
76 && 'pass' ne $_->{row}{policy_evaluated}{spf}
77 } @{ $$agg_ref->{record} };
78 my $ver = $Mail::DMARC::Base::VERSION || ''; # undef in author environ
79 my $from = $$agg_ref->{policy_published}{domain} or croak;
80  
81 return <<"EO_REPORT"
82  
83 This is a DMARC aggregate report for $from
84  
85 $records records.
86 $pass passed.
87 $fail failed.
88  
89 Submitted by $OrgName
90 Generated with Mail::DMARC $ver
91  
92 EO_REPORT
93 ;
94 }
95  
96 sub get_filename( $self, $agg_ref ) {
97  
98 # 2013 DMARC Draft, 12.2.1 Email
99 #
100 # filename = receiver "!" policy-domain "!" begin-timestamp "!"
101 # end-timestamp [ "!" unique-id ] "." extension
102 # filename="mail.receiver.example!example.com!1013662812!1013749130.gz"
103 return join( '!',
104 $self->config->{organization}{domain},
105 $$agg_ref->policy_published->domain,
106 $$agg_ref->metadata->begin,
107 $$agg_ref->metadata->end,
108 $$agg_ref->metadata->report_id || $self->time,
109 ) . '.xml';
110 }
111  
112 sub assemble_too_big_message_object( $self, $to, $body ) {
113  
114 my @parts = Email::MIME->create(
115 attributes => {
116 content_type => "text/plain",
117 disposition => "inline",
118 charset => "US-ASCII",
119 },
120 body => $body,
121 ) or croak "unable to add body!";
122  
123 my $email = Email::MIME->create(
124 header_str => [
125 From => $self->config->{organization}{email},
126 To => $to,
127 Date => $self->get_timestamp_rfc2822,
128 Subject => 'DMARC too big report',
129 ],
130 parts => [@parts],
131 ) or croak "unable to assemble message\n";
132  
133 return $email;
134 }
135  
136 sub assemble_message_object( $self, $agg_ref, $to, $shrunk ) {
137  
138 my $filename = $self->get_filename($agg_ref);
139  
140 # WARNING: changes made here MAY affect Send::compress. Check it!
141 # my $cf = ( time > 1372662000 ) ? 'gzip' : 'zip'; # gz after 7/1/13
142 my $cf = 'gzip';
143 $filename .= $cf eq 'gzip' ? '.gz' : '.zip';
144  
145 my @parts = Email::MIME->create(
146 attributes => {
147 content_type => "text/plain",
148 disposition => "inline",
149 charset => "US-ASCII",
150 },
151 body => $self->human_summary($agg_ref),
152 ) or croak "unable to add body!";
153  
154 push @parts,
155 Email::MIME->create(
156 attributes => {
157 filename => $filename,
158 content_type => "application/$cf",
159 encoding => "base64",
160 name => $filename,
161 },
162 body => $shrunk,
163 ) or croak "unable to add report!";
164  
165 my $email = Email::MIME->create(
166 header_str => [
167 From => $self->config->{organization}{email},
168 To => $to,
169 Date => $self->get_timestamp_rfc2822,
170 Subject => $self->get_subject($agg_ref),
171 ],
172 parts => [@parts],
173 ) or croak "unable to assemble message\n";
174 $email->header_str_set( 'Message-ID' => $self->get_message_id );
175  
176 return $email;
177 }
178  
179 sub get_timestamp_rfc2822( $self, @args ) {
180 my @ts = @args ? @args : localtime $self->time;
181 my $locale = setlocale(LC_CTYPE);
182 setlocale( LC_ALL, 'C' );
183 my $timestamp = POSIX::strftime( '%a, %d %b %Y %H:%M:%S %z', @ts );
184 setlocale( LC_ALL, $locale );
185 return $timestamp;
186 }
187  
188 sub get_helo_hostname($self) {
189 my $host = $self->config->{smtp}{hostname};
190 return $host if $host && $host ne 'mail.example.com';
191 return Sys::Hostname::hostname;
192 }
193  
194 sub get_message_id($self) {
195 my $host = $self->get_helo_hostname;
196  
197 my ( $ss, $mm, $hh, $mday, $mon, $year, $wday, $yday, $isdst )
198 = localtime(time);
199  
200 # Generate a "random" Message-ID
201 return sprintf(
202 "<%04d%02d%02d%02d%02d.%s\@%s>",
203 $year + 1900,
204 $mon + 1, $mday, $hh, $mm, rand(), $host
205 );
206 }
207  
208 1;
209  
210 __END__
211  
212 =pod
213  
214 =head1 NAME
215  
216 Mail::DMARC::Report::Send::SMTP - utility methods for sending reports via SMTP
217  
218 =head1 VERSION
219  
220 version 2.20260827
221  
222 =head2 SUBJECT FIELD
223  
224 The RFC5322.Subject field for individual report submissions SHOULD conform to the following ABNF:
225  
226 dmarc-subject = %x52.65.70.6f.72.74 1*FWS ; "Report"
227 %x44.6f.6d.61.69.6e.3a 1*FWS ; "Domain:"
228 domain-name 1*FWS ; from RFC6376
229 %x53.75.62.6d.69.74.74.65.72.3a ; "Submitter:"
230 1*FWS domain-name 1*FWS
231 %x52.65.70.6f.72.74.2d.49.44.3a ; "Report-ID:"
232 msg-id ; from RFC5322
233  
234 The first domain-name indicates the DNS domain name about which the
235 report was generated. The second domain-name indicates the DNS
236 domain name representing the Mail Receiver generating the report.
237 The purpose of the Report-ID: portion of the field is to enable the
238 Domain Owner to identify and ignore duplicate reports that might be
239 sent by a Mail Receiver.
240  
241 =head1 12.2.1 Email
242  
243 In the case of a "mailto" URI, the Mail Receiver SHOULD communicate
244 reports using the method described in [STARTTLS].
245  
246 The message generated by the Mail Receiver must be a [MIME] formatted
247 [MAIL] message. The aggregate report itself MUST be included in one
248 of the parts of the message. A human-readable portion MAY be
249 included as a MIME part (such as a text/plain part).
250  
251 The aggregate data MUST be an XML file subjected to GZIP compression.
252 The aggregate data MUST be present using the media type "application/
253 gzip", and the filenames SHOULD be constructed using the following
254 ABNF:
255  
256 filename = receiver "!" policy-domain "!" begin-timestamp "!"
257 end-timestamp [ "!" unique-id ] "." extension
258  
259 unique-id = token
260 ; "token" is imported from [MIME]
261  
262 receiver = domain
263 ; imported from [MAIL]
264  
265 policy-domain = domain
266  
267 begin-timestamp = 1*DIGIT
268 ; seconds since 00:00:00 UTC January 1, 1970
269 ; indicating start of the time range contained
270 ; in the report
271  
272 end-timestamp = 1*DIGIT
273 ; seconds since 00:00:00 UTC January 1, 1970
274 ; indicating end of the time range contained
275 ; in the report
276  
277 extension = "xml" / "gzip"
278  
279 For the GZIP file itself, the extension MUST be "gz"; for the XML
280 report, the extension MUST be "xml".
281  
282 =head1 AUTHORS
283  
284 =over 4
285  
286 =item *
287  
288 Matt Simerson <msimerson@cpan.org>
289  
290 =item *
291  
292 Davide Migliavacca <shari@cpan.org>
293  
294 =item *
295  
296 Marc Bradshaw <marc@marcbradshaw.net>
297  
298 =back
299  
300 =head1 COPYRIGHT AND LICENSE
301  
302 This software is copyright (c) 2026 by Matt Simerson.
303  
304 This is free software; you can redistribute it and/or modify it under
305 the same terms as the Perl 5 programming language system itself.
306  
307 =cut