Linux – How to send raw mail message on Linux

emaillinuxsendmailsmtp

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: root@test.server.com
To: test@test.com
Subject: Test Email
Message-ID: <593fe7a3.IgSR+/BLy+NYXlVZ%root@test.server.com>
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?

Best Answer

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.

https://packages.debian.org/stretch/msmtp

Package: msmtp (1.6.6-1)
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.

Related Question