sub send_email { # updated 03/19/2020 to correct encoding of attachments use Email::MIME; use Email::Address::XS; use Encode; use Email::Sender::Simple qw(sendmail); use IO::All; %rec = &get_record($in{$db_key}); my $to = $rec{'Email'}; my $from = $admin_email; my $subject = "webform $html_title"; my $html = $in{'emailmessage'}; my $text = $in{'emailmessage'}; $html = decode( 'utf-8', $html ); $text = decode( 'utf-8', $text ); my ($status,$attach,$newfile); if ($in{'Filename'}) { ($status,$attach) = &get_email_attachment; } if ($status ne 'ok') { $message .= $status; &html_send_email_form($message); return; } # multipart message my @message_parts = ( Email::MIME->create( body_str => $text, attributes => { encoding => 'quoted-printable', content_type => "text/plain", disposition => "inline", charset => "UTF-8", } ), Email::MIME->create( body_str => $html, attributes => { encoding => 'quoted-printable', charset => "UTF-8", content_type => "text/html", disposition => "inline", } ) ); my @all_parts; push @all_parts, Email::MIME->create( parts => \@message_parts, attributes => { content_type => "multipart/alternative" } ); if ($attach) { my $filename = (reverse split /\//, $attach)[0]; # also changed in body => below my $ext = substr($filename,rindex($filename,".")); my $content; if ($ext eq '.pdf') { $content = qq|application/pdf|; } elsif ($ext eq '.docx') { $content = qq|application/vnd.openxmlformats-officedocument.wordprocessingml.document|; } elsif ($ext eq '.doc') { $content = qq|application/msword|; } elsif ($ext eq '.xlsx') { $content = qq|application/vnd.openxmlformats-officedocument.spreadsheetml.sheet|; } elsif ($ext eq '.xls') { $content = qq|application/vnd.ms-excel|; } elsif (($ext eq '.htm') || ($ext eq '.html')) { $content = qq|text/html|; } else { $content = qq|application/octet-stream|; } #$content = qq|application/octet-stream|; # this wipes out all the if statements!!!! push @all_parts, Email::MIME->create( attributes => { filename => $filename, content_type => $mime, encoding => "base64", name => $filename }, body => io( $attach )->binary->all, ) } my $email = Email::MIME->create( header_str => [ From => $from, To => [ $to ], Subject => $subject ], parts => \@all_parts, attributes => { encoding => 'base64', content_type => "multipart/mixed" } ); my $bcc = ''; if ($bcc) { sendmail($email,{ to => [$to, $bcc] }); } else { sendmail($email->as_string); } # print qq|Structure: | . $email->debug_structure. "\n\n" # print for andy &html_send_email_success; }