0

I want to send mails from a bash script and to know that they sucessfully left my server.

There are many ways to send mails from bash (see 1 2 for more possibilities). A common way is to use mail or mutt together with the build-in MTA (e.g. postfix). If one does not run a "true" mailserver, than postfix can be configured as a "smart host" or "satelite system" where it connects to another SMTP server (the so-called relay) and sends everything over this server.

I can assume any MTA. Since I control the server, I am willing to install any MTA which is easy to maintain = has an ubuntu (18.04 LTS and newer) and centos (7.4 and newer) package. Right now I am using postfix, since I know it okay and the setup as a relay is quite simple.

Assuming such a setup, I want to write a bash script which sends a mail and then awaits the confirmation that sending it to the relay was successfull. My problem is the last part.

echo "hello world" | mail -s "a subject" [email protected]
#
# Check that the mail has arrived at the relay server. But how?
#
3
  • Can you assume that a relay-only MTA such as msmtp will be used, or do you need this to work with any MTA? May 7, 2019 at 4:48
  • I can assume any MTA. Since I control the server, I am willing to install any MTA which is easy to maintain = has an ubuntu (18.04 LTS and newer) and centos (7.4 and newer) package. Right now I am using postfix, since I know it quite okay and the setup as a relay is quite simple.
    – masgo
    May 7, 2019 at 7:05
  • Please edit your question with new information such as the one in your comment; comments are not guaranteed to stick around.
    – dirkt
    May 7, 2019 at 7:22

1 Answer 1

2

None of the widely used MTAs will give you confirmation of the actual sending process and with good reason: MTAs are by definition queue-based and the time spent in a queue on a busy mailserver can be orders of magnitude higher than what a shell script is expected to wait.

Think e.g. of the very common greylisting scenario: Would you want your script to wait 10 minutes?

You have a few options:

  • if your recipients send delivery notifications, you can use those
  • you can throw together a script that sends the mail directly to the MX of the domain in question (there might be libraries for that) and live with potential multi-minute waits
  • use the -N 'success' parameter in postfix's version of sendmail, then poll for the DSN mail

I have used all three variations and the last one seems to fit your problem best.

4
  • I would want my script to wait 10 minutes. Actually, I can be pretty sure that greylisting does not occur since my postfix has to authenticate at the relay. (How the mail is processed after the relay is none of my busines, because errors there are extremly rare). And even if, the script runs rarely, and waiting some minutes is fine.
    – masgo
    May 7, 2019 at 7:12
  • In this case I recommend sending not via your MTA but directly via the relay (i.e. the second bullet). I normally use PHP on the command line for that, but it might be quite easily doable via pure bash. May 7, 2019 at 7:26
  • Using SMTP directly is quite cubersome. Authentication to the relay neets to be handled properly. And also some of these mails do contain attachments and other "more complicated" things. All of wich is perfectly simple (e.g. just a commandline parameter) in programms like mutt.
    – masgo
    May 8, 2019 at 7:11
  • By outsourcing work to other programs (such as mutt) you inherit their limitations. No mail client can give you information, that itself doesn't get from the MTA. May 8, 2019 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .