Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to send an email with attachment from a perl script.

First, I create the attachment (an xml file):

open(XMLFILE, ">:utf8", $xmlfile);
print XMLFILE "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
print XMLFILE "<data>\n";
#print XMLFILE "...\n";
print XMLFILE "</data>\n";
close (XMLFILE);

I also tried open(XMLFILE, ">", $xmlfile); with binmode XMLFILE, ":utf8";.

Then I send the email like this:

open(MUTT, "|/usr/bin/mutt -s \"TestSubject\" -a $xmlfile \"test\@example.com\"");
binmode MUTT, ":utf8";
print MUTT ("TestBody");
close (MUTT);

Nevertheless, both the text part and the attachment have Content-Type: text/plain; charset=iso-8859-1.

I also tried open(MUTT, "|/usr/bin/mutt -e \"set file_charset=utf-8\" -a $xmlfile ..., but this gave me an Error in command line: file_charset: unknown variable.

What am I doing wrong?

share|improve this question
@slm Thanks for retagging! – Martin Jun 13 '13 at 10:24

How about something like this:

$ mutt -e "set content_type=text/html" Email address -s "subject" < test.html

Change it around for what ever content_type you want. In Perl something like this:

open(MUTT, "|/usr/bin/mutt -e \"set content_type=text/xml\" -s \"TestSubject\" -a $xmlfile \"test\@example.com\"");

If you don't want to use mutt you can use mail instead:

### method #1
$ mail -a 'MIME-Version: 1.0' -a 'Content-Type: text/xml; charset=iso-8859-1' -a 'X-AUTOR: Some Guy' -s 'MTA STATUS: mail queue' <to user>  -- -f <from user>  < /tmp/eximrep.xml

### method #2
 $ mail -a 'Content-type: text/xml; charset="us-ascii"' <to user> < /tmp/file.xml

You could also do this using sendmail directly:

echo "From: myuser@example.com"
echo "To: user@example.net"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed;"
echo ' boundary="BOUNDARY"'
echo "Subject: Test Message"
echo ""
echo "This is a MIME-encapsulated message"
echo "--BOUNDARY"
echo "Content-Type: text/plain"
echo ""
echo "This is what someone would see without an HTML capable mail client."
echo ""
echo "--BOUNDARY"
echo "Content-Type: text/html"
echo ""
echo "<html>
<body bgcolor='black'>
<blockquote><font color='green'>GREEN</font> <font color='white'>WHITE</font> <font color='red'>RED</font></blockquote>
echo "--BOUNDARY--"
) | sendmail -t


share|improve this answer
Same problem: Error in command line: content_type: unknown variable - Also, I'm not sure if that would give me utf-8-encoded attachments. – Martin Jun 13 '13 at 10:23
Both give the same error. Which mutt version do you have? I didn't find out how to get the version number, but my docs (/usr/share/doc) have version mutt- – Martin Jun 13 '13 at 12:59
mutt -v. I have this: Mutt 1.5.21 (2010-09-15) – slm Jun 13 '13 at 13:20
Then it's probably a version problem. I'll try to update mutt (problematic, because it's not my own system...). – Martin Jun 13 '13 at 13:24
let us continue this discussion in chat – Martin Jun 13 '13 at 13:27
up vote 0 down vote accepted

I was unable to update the mutt version, but I found a workaround - others may find this helpful, too.

Including a comment with special characters lets perl and mutt choose the correct (utf-8) encoding (probably the 'ł' would suffice, but the intent gets clearer with the umlaut characters):

In the xml this is what it looks like:

<?xml ... ?>
<?comment <!-- ł€èÄöÜß --> ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.