diff --git a/lib/Mail/DMARC/Policy.pm b/lib/Mail/DMARC/Policy.pm index cdc40fc7..2e103b4c 100644 --- a/lib/Mail/DMARC/Policy.pm +++ b/lib/Mail/DMARC/Policy.pm @@ -56,6 +56,20 @@ sub parse { return bless \%policy, ref $self; # inherited defaults + overrides } +sub stringify { + my ( $self ) = @_; + + my %dmarc_record = %{$self}; + # "v" tag must be the first one + my $dmarc_txt = 'v=' . $dmarc_record{v} . '; '; + foreach my $key ( keys %dmarc_record ) { + next if ($key =~ /^domain|v$/); + $dmarc_txt .= "$key=$dmarc_record{$key}; "; + } + $dmarc_txt =~ s/;\s$//; + return $dmarc_txt; +} + sub apply_defaults { my $self = shift; @@ -269,6 +283,13 @@ via DNS. $pol->parse( 'v=DMARC1; p=none; rua=mailto:dmarc@example.com' ); $pol->parse( 'v=DMARC1' ); # external reporting record +=head2 stringify + +Returns the textual representation of the DMARC record. + + my $pol = Mail::DMARC::Policy->new('v=DMARC1; p=none;'); + print $pol->stringify; + =head1 Record Tags =head2 Tag Overview diff --git a/t/01.Policy.t b/t/01.Policy.t index c667dca3..d791ddbd 100644 --- a/t/01.Policy.t +++ b/t/01.Policy.t @@ -25,6 +25,7 @@ stderr_is { test_parse() } $expected_parse_warning, 'STDERR yields parse warning test_setter_values(); test_apply_defaults(); test_is_valid(); +test_stringify(); handles_common_record_errors(); done_testing(); @@ -259,6 +260,11 @@ sub test_is_valid { ok( $pol && $pol->is_valid, "is_valid, pos, implicit p=none w/rua" ); } +sub test_stringify { + $pol = Mail::DMARC::Policy->new( 'v=DMARC1; p=reject' ); + ok($pol->stringify, 'v=DMARC1; p=reject'); +} + sub handles_common_record_errors { foreach my $d () {