Sending UTF-8 encoded email using Perl

The following worked for me. Assumes a working sendmail interface on a UNIX host.

use Encode;
use utf8;

sub SendEmail
{
    my ($from, $to, $subject, $msg) = @_;
    
    $from = Encode::encode('MIME-Q', $from);
    $to = Encode::encode('MIME-Q', $to);
    $subject = Encode::encode('MIME-Q', $subject);

    open (SENDMAIL, "| /usr/sbin/sendmail -t") or die("Failed to open pipe to sendmail: $!");
    binmode(SENDMAIL, ":utf8");
    print SENDMAIL <<"EOF";
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Subject: $subject
From: $from
To: $to
$msg
EOF
    close (SENDMAIL);
}