If I have the full contents of a MIME message, what is the best utility on Linux to send the message? The MIME message would include the full headers and mail body, for example:

Received: (qmail 32389 invoked by uid 0); 13 Jun 2017 09:24:51 -0400
Date: Tue, 13 Jun 2017 09:24:51 -0400
From: [email protected]
To: [email protected]
Subject: Test Email
Message-ID: <593fe7a3.IgSR+/BLy+NYXlVZ%[email protected]>
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

The mail body goes here

I want to be able to feed the above to a command line utility which will then re-process the message exactly 'as is' without having to parse fields such as sender, subject, etc. It should send the message through a specified external SMTP server (not the local server's mail queue).

What command line utility can I use for this purpose?


3 Answers 3


You may use sendmail or "sendmail look alike" provided by postfix/exim/... .

/usr/sbin/sendmail -i -- $recipients < message_file

-i - do not treat lines with leading dot specially

You may use more exotic "sendmail look alike" (e.g. provided by msmtp) to send directly via another smtp host without "system wide" configuration.
msmtp is distributed in debian so it is likely to be included in other linux distributions.



Package: msmtp (…)
light SMTP client with support for server profiles

msmtp is an SMTP client that can be used to send mails from Mutt and probably other MUAs (mail user agents). It forwards mails to an SMTP server (for example at a free mail provider), which takes care of the final delivery. Using profiles, it can be easily configured to use different SMTP servers with different configurations, which makes it ideal for mobile clients.

  • Thanks. I forgot to mention in the original question that we would like it to send via another SMTP server (not the local mail queue). Is that possible? I am editing the question to include this.
    – Chris
    Commented Jul 31, 2017 at 20:44
  • I have expanded the answer Commented Aug 1, 2017 at 2:53

Unfortunately sendmail and mailx do not allow you to specify a different SMTP server. But you could combine the answer from Andrzej with ssh. Although I will use the -t option to read recipients from the file/data being read.

cat <messagefile> | ssh user@mailhost /usr/sbin/sendmail -i -t

<messagefile> is a file containing the MIME message and mailhost is the SMTP server. user is a login id on the SMTP server.


s-nail is a greatly enhanced alternative to mailx that, amongst many other improvements, allows you to specify an SMTP server on the command line.


s-nail -S "smtp=smtp://remote-server:port" < message.txt

It even supports encrypted SMTP (using STARTTLS, SMTPS, or SUBMISSION) and SMTP AUTH. See man s-nail for details.

s-nail is available pre-packaged for several linux distributions (e.g. on debian apt-get install s-nail.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .