-2

I have a Bash Script to send email which is excuted via Kubernetes CronJob but when the job runs the email is not being sent as in the error log below :

kubectl -n labs logs send-mail-28210503--1-grtpv
bin dev mail.sh etc home lib media mnt opt proc root run sbin srv sys tmp usr var
Starting-MAILSEND
bin dev mail.sh etc home lib media mnt opt proc root run sbin srv sys tmp usr var
mail: Field already retained: from
mail: No such file to load: /root/.mailrc
mail: Warning -- v15-compat=yes will be default in v14.10.0!
mail: P(seudo)R(andom)N(umber)G(enerator): *TLS RAND_*
mail: smtp=mail2.mycompany.com:25 contains invalid byte ':'
/root/dead.letter 10/269
sendmail: can't connect to remote host (127.0.0.1): Connection refused
/root/dead.letter 10/269
mail: ... message not sent
Failed to send the email

UPDATE

This is how the Bash script looks like after changes (will reintroduce error checking later):

#!/bin/sh
# Function to send an email notification
echo ******************************************************
echo Starting-MAILSEND
echo ******************************************************
    
echo -e "This is a Email test." | mailx -v -s "Job Failed" -r "ops@mycompany.com" jonhdoe@mycompany.com -S "smtp=mail2.mycompany.com"

I have removed port from the SMTP server value but I am facing a new error which seems to suggest that its an invalid value:

bin dev email.sh etc home lib media mnt opt proc root run sbin srv 
sys tmp usr var
Starting-MAILSEND
bin dev email.sh etc home lib media mnt opt proc root run sbin srv sys tmp usr var
mail: Field already retained: from
mail: No such file to load: /root/.mailrc
mail: Warning -- v15-compat=yes will be default in v14.10.0!
mail: P(seudo)R(andom)N(umber)G(enerator): *TLS RAND_*
mail: smtp=mail2.mycompany.com is an invalid alias name
/root/dead.letter 10/268
sendmail: can't connect to remote host (127.0.0.1): Connection refused
/root/dead.letter 10/268
mail: ... message not sent
Failed to send the email.

The Bash script is bundled with the following Dockerfile below and the Docker image is used in a K8s CronJob resource:

FROM dockette/alpine:3.11
COPY mail.sh .
RUN chmod +x mail.sh
RUN apk update && \
    apk upgrade && \
    apk add --no-cache s-nail && \
    ln -s /usr/bin/mail /usr/bin/mailx && \
    rm -rf /var/cache/apk/*
CMD ["/usr/bin/mailx"]

NB: The mail credentials are valid because I am able to send emails using the same paramaters (sender, smtp server,recipient), only difference is that this is via a Prometheus AlertManagerConfig.

What am I missing ?

Golide
  • 835
  • 3
  • 13
  • 36
  • 2
    BTW, `BACKUP_RETURN_CODE=${?}` is storing the exit status of the preceding `echo`. I don't know how you expect that to be useful. (This is part of why I always collect `$?` _on the same line as the command it's intended to refer to_ -- `somecommand; somecommand_rc=$?` is harder to break when folks add logging or restructure code -- and btw, note the lowercase variable name; all-caps names are used by the shell itself and OS-provided tools, [lowercase names are reserved](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) for user-developed scripts/applications/tools) – Charles Duffy Aug 21 '23 at 15:52
  • 3
    Anyhow, the obvious answer to `mail: smtp=mail2.mycompany.com:25 contains invalid byte ':'` is to take the port number out and just have `smtp=mail2.mycompany.com`. I'd need to read the manual to the particular `mail` you're using to know how to provide the port. – Charles Duffy Aug 21 '23 at 15:54
  • @CharlesDuffy [`mailx(1)`](https://linux.die.net/man/1/mailx) says **If the SMTP server does not use the standard port, a value of server:port can be given, with port as a name or as a number.** So `:25` should theoretically work there. But since 25 is the standard SMTP port, it doesn't seem to be needed. – Barmar Aug 21 '23 at 17:41
  • @Barmar, is that the s-nail `mailx` or someone else's that that man page is from? (Granted, my recollection of the s-nail implementation is that it's a pretty good / comprehensive one, but it was something like 15 years ago I last used it, so that's a really hazy recollection). – Charles Duffy Aug 21 '23 at 18:09
  • 1
    @Golide, BTW, note that `#!/bin/sh` is not a "bash script"'s shebang; sh and bash are different shells -- even if sh is a symlink to bash, bash disables some features when called under the `sh` name, and on Alpine, sh is _probably not_ bash at all in the first place, and is likely instead a busybox-modified version of dash. – Charles Duffy Aug 21 '23 at 18:11
  • 3
    (Also see [Why is testing "$?" to see if a command succeeded or not an antipattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern)) – Charles Duffy Aug 21 '23 at 18:16
  • @CharlesDuffy I'm not familiar with s-nail. I don't know what distro it's from, it's what I got when I googled "man mailx". – Barmar Aug 21 '23 at 19:19
  • @Barmar, ...fwiw, I don't recall s-nail being a distro-specific tool, but if my recollection of the timeline I last used it in is accurate, I was probably at an Arch shop at the time. – Charles Duffy Aug 21 '23 at 19:29
  • @CharlesDuffy See my update above. Even after removing port number it seems the smtp server value is being interpreted as a wrong value. – Golide Aug 22 '23 at 08:50
  • The `smtp` variable is relevant when mailx is sending the message internally; it's _not_ relevant when mailx starts sendmail to do the network traffic for it. – Charles Duffy Aug 22 '23 at 14:42
  • mind, the docs say it _shouldn't_ be using sendmail when `smtp` is set, so there _is_ a mystery here; but of course you could also configure sendmail and work around the problem from that direction. – Charles Duffy Aug 22 '23 at 14:43
  • One thing I note, btw, is that you're putting `-S` **after** the positional argument `johndoe@mycompany.com`. Traditional UNIX tools require all options to come _before_ any positional arguments; try moving `johndoe@mycompany.com` to the very end of the command line. – Charles Duffy Aug 22 '23 at 14:44

0 Answers0