Solved bash - cannot find a mailer (dma)

I am running a bash script that contains these statements:

Code:
MAILER="/usr/sbin/sendmail -t"   # -t get To:, CC:, and BCC:, from message 
. . .
local _mailer_return=$(cat "${_msg_file_name}" | "${MAILER}")        #                  <<== this is the error, using a variable instead of a literal

which gives this error:

Code:
line 266: /usr/sbin/sendmail -t: No such file or directory

despite this:
Code:
ls -l /usr/sbin/sendmail
lrwxr-xr-x  1 root  wheel  11 Nov  1  2019 /usr/sbin/sendmail -> mailwrapper

Following the trail of breadcrumbs gives this:
Code:
ls -l /usr/sbin/mailwrapper
-r-xr-xr-x  1 root  wheel  9224 Dec 24 10:44 /usr/sbin/mailwrapper

However, changing the mailer does not work either:
Code:
line 266: /usr/sbin/mailwrapper -t: No such file or directory

This probably has something to do with dma
Code:
cat /etc/mail/mailer.conf
# $FreeBSD: releng/12.3/etc/mail/mailer.conf 363973 2020-08-06 18:13:45Z kevans $
#
# Execute the "real" sendmail program, named /usr/libexec/sendmail/sendmail
#
# If dma(8) is installed, an example mailer.conf that uses dma(8) instead can
# can be found in /usr/share/examples/dma.
#
sendmail        /usr/local/libexec/dma
send-mail       /usr/local/libexec/dma
mailq           /usr/local/libexec/dma

#sendmail        /usr/libexec/sendmail/sendmail
#mailq           /usr/libexec/sendmail/sendmail
#newaliases      /usr/libexec/sendmail/sendmail
#hoststat        /usr/libexec/sendmail/sendmail
#purgestat       /usr/libexec/sendmail/sendmail

ls -l /usr/local/libexec/dma
-r-xr-sr-x  1 root  mail  66336 Apr  6 11:22 /usr/local/libexec/dma

Using that works no better:
Code:
line 266: /usr/local/libexec/dma -t: No such file or directory

So, how does one send mail from inside a bash script on FreeBSD?
 
Last edited:
Is the variable ${_msg_file_name} actually pointing to an existing file? Because a cat /some/nonexistent/file would get you the same error message. It's not the mailer it cannot find, the file you're trying to stuff into it is not there.
 
Read the man page for dma. Does it actually support the -t flag?

On my machine, I use ssmtp (not sendmail, but also not dma), and if I use the -t option with it, it actually says "recipients with -t option not supported".

And also: Make sure the file /usr/local/libexec/dma actually exists, and is readable and executable. Otherwise mailwrapper will read /etc/mail/mailer.conf, will try to find and execute it, and perhaps the error message you are getting comes from that step failing.
See post from Rigoletto below: It is not in /usr/local, but in /usr. That's probably the problem.
 
Is the variable ${_msg_file_name} actually pointing to an existing file? Because a cat /some/nonexistent/file would get you the same error message. It's not the mailer it cannot find, the file you're trying to stuff into it is not there.
Code:
echo "${MP_BOUNDARY}--" >> "${_msg_file_name}"
  echo "_msg_file_name is: ${_msg_file_name} location: $(ls -l ${_msg_file_name})"
  local _mailer_return=$(cat "${_msg_file_name}" | "${MAILER}")


_msg_file_name is: /tmp/hll_inv_email_tmp.cqmPCwuz
location: -rw-------  1 byrnejb_hll  wheel  283391 Jun  9 09:29 /tmp/hll_inv_email_tmp.cqmPCwuz
/home/byrnejb_hll/Projects/hll_powerhouse/freebsd/bash/email_invoices/email_invoices.sh: line 271: /usr/local/libexec/dma -t: No such file or directory

I can also cat the contents of _msg_file_name to the session and it displays this, among other things:
Code:
To: byrnejb@harte_lyne.ca
Date: Thu, 09 Jun 2022 09:35:19 -0400
Subject: IN356753 - Invoice
From: "INVOICE" <accounting@harte_lyne.ca>
Reply-To: accounting@harte_lyne.ca
MIME-Version: 1.0
Content-Type: multipart/mixed;boundary="--=00000000000029833c05e03abcd2"
X-Priority: 3 (Normal)
Importance: Normal

--=00000000000029833c05e03abcd2
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit

Accounts Payable:

Please remit payment for the attached invoice.
. . .
 
dma(8) is in base, in this case: /usr/libexec/dma
Code:
$ ll /usr/libexec/dma
-r-xr-sr-x  1 root  mail  64232 Dec 24 10:43 /usr/libexec/dma

$ ll /usr/local/libexec/dma
-r-xr-sr-x  1 root  mail  66336 Apr  6 11:22 /usr/local/libexec/dma

We have dma installed from ports. I do not know when dma was added to base but it was at some point after this host was initially configured. In any case changing the file path has no effect on the error:
Code:
. . ._invoices.sh: line 272: /usr/libexec/dma -t : No such file or directory

Nor does removing the option -t:
Code:
. . ._invoices.sh: line 272: /usr/libexec/dma : No such file or directory
 
Well, I found the error. MAILER is defined as: MAILER="/usr/sbin/sendmail -t" # -t get To:, CC:, and BCC:, from message.

The problem arises from the use of " instead of ' on the right hand side
 
Well, that is not it either. The only way I can get this to work to to put that literal command in the script. This works:
Code:
  cat "${_msg_file_name}" | /usr/sbin/sendmail -t

This does not:
Code:
  cat "${_msg_file_name}" | ${MAILER}
 
Your ${MAILER} command contains spaces. Think about what happens when the line "cat ... | ${MAILER}" gets expanded, and think through how shell parameter expansion works, and how it interacts with the shell splitting the input into tokens. Depending on whether you use no quotes, ' or ", you will get different answers. My educated guess is that your shell is trying to execute a command called "/usr/sbin/sendmail -t" (a single token with a space in the middle), and there is no such executable; the only one that exists is /usr/sbin/sendmail.

I don't have time to look up the rules for variable expansion in the different shells, and how ' and " act differently (they do!); I think someone needs to spend half hour with a shell manual.
 
Your ${MAILER} command contains spaces. Think about what happens when the line "cat ... | ${MAILER}" gets expanded, and think through how shell parameter expansion works, and how it interacts with the shell splitting the input into tokens. Depending on whether you use no quotes, ' or ", you will get different answers. My educated guess is that your shell is trying to execute a command called "/usr/sbin/sendmail -t" (a single token with a space in the middle), and there is no such executable; the only one that exists is /usr/sbin/sendmail.

I don't have time to look up the rules for variable expansion in the different shells, and how ' and " act differently (they do!); I think someone needs to spend half hour with a shell manual.
The difficulty with this analysis is that removing the -t option together with the space associated with it did not change behaviour:

Nor does removing the option -t:
. . ._invoices.sh: line 272: /usr/libexec/dma : No such file or directory
 
Back
Top