How to send raw mail message on Linux?

5,285

Solution 1

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.

Solution 2

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.

Solution 3

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.

e.g.

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.

Share:
5,285
Mattia
Author by

Mattia

Updated on September 18, 2022

Comments

  • Mattia
    Mattia over 1 year

    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?

  • Mattia
    Mattia almost 7 years
    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.
  • AnFi
    AnFi almost 7 years
    I have expanded the answer