From d00091440d855a76caaad0b6d94122d40b140750 Mon Sep 17 00:00:00 2001 From: Ricardo Signes Date: Sat, 4 Jul 2015 14:20:21 -0400 Subject: [PATCH] fold headers from create method this code has grown very long in the tooth :-/ --- Changes | 2 ++ lib/Email/Simple/Creator.pm | 2 +- lib/Email/Simple/Header.pm | 10 ++++++++-- t/folding.t | 19 ++++++++++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Changes b/Changes index bddfbe5..87757f4 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for Perl extension Email::Simple {{$NEXT}} + - fold headers passed to header arg in create method + https://github.com/rjbs/Email-Simple/issues/7 2.206 2015-03-26 23:30:51-04:00 America/New_York - the changes from 2.204 are back (and re-listed here), but now with diff --git a/lib/Email/Simple/Creator.pm b/lib/Email/Simple/Creator.pm index 93e4932..cef372f 100644 --- a/lib/Email/Simple/Creator.pm +++ b/lib/Email/Simple/Creator.pm @@ -22,7 +22,7 @@ sub _add_to_header { Carp::carp("replaced vertical whitespace in $key header with space; this will become fatal in a future version"); } - $$header .= "$key: $value" . $class->_crlf; + $$header .= Email::Simple::Header->__fold_objless("$key: $value", 78, q{ }, $class->_crlf); } sub _finalize_header { diff --git a/lib/Email/Simple/Header.pm b/lib/Email/Simple/Header.pm index 09b827b..c05908b 100644 --- a/lib/Email/Simple/Header.pm +++ b/lib/Email/Simple/Header.pm @@ -316,15 +316,21 @@ sub _fold { my $indent = $arg->{indent} || $self->_default_fold_indent; + return $self->__fold_objless($line, $limit, $indent, $self->crlf); +} + +sub __fold_objless { + my ($self, $line, $limit, $indent, $crlf) = @_; + # We know it will not contain any new lines at present my $folded = ""; while ($line) { if ($line =~ s/^(.{0,$limit})(\s|\z)//) { - $folded .= $1 . $self->crlf; + $folded .= $1 . $crlf; $folded .= $indent if $line; } else { # Basically nothing we can do. :( - $folded .= $line . $self->crlf; + $folded .= $line . $crlf; last; } } diff --git a/t/folding.t b/t/folding.t index 75299e6..9b83b25 100644 --- a/t/folding.t +++ b/t/folding.t @@ -1,6 +1,6 @@ #!perl -w use strict; -use Test::More tests => 6; +use Test::More tests => 7; # This time, with folding! @@ -36,3 +36,20 @@ END my $email = Email::Simple->new($text); is($email->header('Fold-2'), '0 1 2', "we unfold with a false start string"); } + +{ + my $to = 'to@example.com'; + my $from = 'from@example.com'; + + my $subject = 'A ' x 50; # Long enough to need to be folded + + my $email_1 = Email::Simple->create( + header => [ + To => $to, + From => $from, + Subject => $subject, # string specified in constructor does *not* get folded + ] + ); + + unlike($email_1->as_string, qr/\Q$subject/, "we fold the 50-A line"); +}