All posts by Don Matteo

lebt in der Schweiz, ist System Engineer MCP bei A-Enterprise GmbH. Mitglied des UNBLOG Knowledge Network. Author und Blogger zu den Themen, Tutorials für Linux und Open Source.

Sendmail using echo message attachment on Linux

sendmail send message and attachment use echo. sendmail implements Postfix’s compatibility interface and is present in almost all Unix-like operating systems, as mail transfer agent whose history goes back to the early 1980s.

How to sendmail echo message attachment

Sendmail using echo message attachment on Linux

sendmail sends message over the Internet to a specific recipient. This can be done by running the sendmail command on a Linux command line, and can also be used within a shell script.

  sendmail: fatal: open /etc/postfix/main.cf: No such file or directory

Make sure sendmail is present on the Linux host with the command which sendmail or man sendmail. If sendmail is not found and no MTA like Postfix is installed, sendmail can be deployed as follows.

$ sudo apt install sendmail -y

sendmail in the command line

The following example shows a sendmail command to send a message from the command line with using the echo command.

$ echo -e "Subject:Test Mail using sendmail \nThis is a Test Message\n" | sendmail youremail@example.tld

The next command sends an email with sender (From:) address.

$ /usr/sbin/sendmail youremail@example.tld
From: nobody@example.tld
Subject: Test Mail using sendmail
This is a Test Message
.

  Enter period (.) ends the editor and the e-mail will by submit.

Example email is interactively created by each line, a message with the subject delivered to inbox of youremail@example.tld.

Change the recipient accordingly, make sure sendmail is present on the host and the host is allowed to send email.

Further, sendmail robs the pipeline instruction, thereby making it possible to send a message using a text file.

$ /usr/sbin/sendmail < message.txt -t -i youremail@example.tld

Example with the content of the message.txt file.

From: nobody@example.tld
Subject: Test Mail using sendmail
This is a Test Message

  The sendmail -i option handles reading the message from standard input without a period (.) at the end of a line.

The -t option extracts the recipients from the message headers and adds them to all recipients specified on the command line.

How to send message with attachment use sendmail

sending an email directly from a host, in this example sendmail is used to send a file as an attachment.

Here is to filter out the delivery attempts rejected as SPAM “blocked using” on a Postfix MTA, and to send them as archives.txt.gz, for this purpose.

Postfix must be configured on the host, it can also be another local MTA, or an external SMTP-Relay is used.

#!/bin/bash
cat /var/log/maillog* | grep 'blocked using' | grep 'middleton.tld' > /tmp/blocked.txt
gzip -f /tmp/blocked.txt
( echo "to: sysop@example.tld"
  echo "from: no_reply@$HOSTNAME"
  echo "subject: Report of messages blocked by $HOSTNAME"
  echo "mime-version: 1.0"
  echo "content-type: multipart/related; boundary=messageBoundary"
  echo
  echo "--messageBoundary"
  echo "content-type: text/plain"
  echo
  echo "Please find the document attached."
  echo
  echo "--messageBoundary"
  echo "content-type: text/plain; name=blocked.txt.gz"
  echo "content-transfer-encoding: base64"
  echo
  openssl base64 < /tmp/blocked.txt.gz) | sendmail -t -i

  The -t option extracts the recipients from the message header.
-i option don´t treat a . character as the end of input.

Conclusion

This example shows how to use sendmail to send an email with attachment directly from the Linux shell.

By changing the individual lines, many other applications can come into play, whereby the Base64 encoding is done by openssl, the echo statements performs the MIME message body.

  more related in this post and here.

DIG WARNING: recursion requested but not available

You may get a warning when trying to query with dig, WARNING: recursion requested but not available.

; <<>> DiG 9.16.37-Debian <<>> domain.tld
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: FORMERR, id: 34429
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

which does dig recursion requested?

DIG sends DNS COOKIE option, this option is enabled by default starting with BIND 9.11. Unfortunately. The query with this option means that the DNS server, often older Windows DNS servers. Treats the request as incorrect, or does not understand the query, which means that the request is rejected.

A workaround can be to pass the+nocookieor+noedns to disable the EDNS option.

$ dig domain.tld +nocookie

The message recursion requested appear, but the dig request is resolved.

dig commands return one or multiple sections of information about the hostname’s DNS records depending on the syntax of your query. In the example below, dig returned results for the query dig time.google.com +nocookie

$ dig time.google.com +nocookie

; <<>> DiG 9.18.12-0ubuntu0.22.04.2-Ubuntu <<>> time.google.com +nocookie
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31915
;; flags: qr rd ad; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;time.google.com.               IN      A

;; ANSWER SECTION:
time.google.com.        0       IN      A       216.239.35.4
time.google.com.        0       IN      A       216.239.35.8
time.google.com.        0       IN      A       216.239.35.12
time.google.com.        0       IN      A       216.239.35.0

;; Query time: 9 msec
;; SERVER: 172.17.64.1#53(172.17.64.1) (UDP)
;; WHEN: Sun Aug 06 13:02:12 CEST 2023
;; MSG SIZE  rcvd: 112

what is dig?

dig is a robust command-line tool developed by BIND for querying DNS nameservers. It can identify IP address records, record the query route as it obtains answers from an authoritative nameserver. Using Diagnostic and other DNS issues. Use with or without EDNS option for recursion requested.

You can install dig on most operating systems by downloading the latest version of BIND 9 from BIND’s website. And from the command line using a package manager.

Installing dig

To install dig for Windows, go to BIND’s website and download the most current version of BIND 9. Extract the downloaded file and double-click the BIND install icon in the newly created directory.

On the BIND 9 Installer screen, verify that the target directory is set to C:\Program Files\ISC BIND 9 (or C:\Program Files (x86)\ISC BIND 9 if you are using an x86 architecture) and select the Tools Only check box. Then click Install.

Dig is a part of DNS utility package that often gets installed with BIND name servers. You can also install the utility package on Debian Linux that contains dig with ran apt-get install dnsutils