mixed + alternative not supported
Opened this issue ยท 8 comments
docs should indicate that you can't use both text_body
, html_body
, with attach
. Maybe in TODO or attach
/attach_file
sections?
E.g.:
Email::Stuffer->from ('cpan@ali.as' )
->to ('santa@northpole.org' )
->text_body('plain text' )
->html_body('html text' )
->attach_file('dead_bunbun_faked.gif' )
->as_string;
Produces multipart/mixed
header with 3 parts; but no multipart/alternative
Should be two parts, multipart/alternative
and the attachment, both inside a multipart/mixed
Gotta be a bug. I need to make a run through the bugs on E-S and will check it out when I get there.
Any progress on patching this?
Workaround:
my $mail=Email::Stuffer->new({ transport=>$transport})
->to($to)
->from($from)
->subject($subject)
->text_body($plain_text)
->html_body($html_text);
if($attachment){
$mail->{parts}->[0]=Email::MIME->create(
attributes=>{content_type=>q(multipart/alternative)},
parts=>[delete $mail->{parts}->[0,1]]
);
$mail->attach_file($attachment, filename=>$filename, disposition=>'attachment');
}
Thx biggle1856, I will test that out at work ๐
I've written a patch to fix this issue. I will test it over the next couple of days, and then submit a PR. Issue #33 should probably be closed, as it is a duplicate of this one.
PR 40 submitted :)
Here's a modified workaround, based on the one above by biggles1856 (thanks!). When I tried that one, the text body got lost. Not sure why, but it's possibly related to the following note in perldoc -f delete
:
WARNING: Calling "delete" on array values is strongly discouraged.
The notion of deleting or checking the existence of Perl array
elements is not conceptually coherent, and can lead to surprising
behavior.
This version seems to work:
my $mail = Email::Stuffer->new({ transport => $transport })
->to($to)
->from($from)
->subject($subject)
->text_body($plain_text)
->html_body($html_text);
if ($attachment) {
$mail->{parts} = [ Email::MIME->create(
attributes => {content_type => q(multipart/alternative)},
parts => $mail->{parts}
) ];
$mail->attach_file($attachment, filename => $filename, disposition => 'attachment');
}
For those curious, biggle1856's solution should work ... if you change ->[0,1]
to ->@[0,1]
.
May be what was intended originally.
That ->@
syntax is called postfix dereferencing, I think.
Note that it "became stable in Perl v5.24", so may not be available to you, if running on older systems.
Kind of funny (and unfortunate), with use warnings
Perl will tend to complain about "Useless use of a constant (n) in void context" ... except for indices 0 or 1, as was the case here. ๐
Anyway, would be nice to have this functionality provided by default, whether merging PR #41 as-is or updated, or offering any other interface.