Trying to setup a simple automation system has proved exceedingly complicated for me. Using scripts, I want to use email to perform remote tasks. The script outline:

  1. Send an empty body email with the header as "Pattern1 (number)"
  2. Read the last 50 email headers.
  3. Find latest header matching "Pattern2 (number)".
  4. Save (echo) the number from that header into a file.
  5. Wait 5 minutes
  6. Loop to 1.

I'm trying to using as few programs as possible. Currently, have Mutt for email sending and retrieval. And grepmail (with grepm script) for searching emails.

Am I way off on using Mutt and grepmail? I'm confused as a non-sysadmin. Mutt isn't great for scripting. What's the simplest way to set this up?

1 Answer 1


It can all be done with only curl.

Send email

MSG="From: [email protected]
To: Receiver
Subject: Test"

echo "$MSG" | curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
--mail-from "[email protected]" --mail-rcpt "[email protected]" \
--upload-file - --user "[email protected]:password" --insecure

To read mail, first you have to know how many emails are in the INBOX.

EXISTS="$(curl --insecure \
--user "[email protected]:password" \
--url 'imaps://imap.gmail.com:993/' \
--request "EXAMINE INBOX" | grep "EXISTS" | grep -oP '\d*' | head -n1 )"

echo "$EXISTS"

The newest email has the highest number. Print the subject of the last 5 emails, newest first.

for ((i=$EXISTS;i!=$EXISTS-5;i--)); do
   SUBJECT="$(curl --insecure -u "[email protected]:password" \
   --url "imaps://imap.gmail.com:993/INBOX;UID=$i" | \
   grep "Subject: " | head -n1 )"

   echo "$SUBJECT"

Notice: To enable smtp/imap access like this, you must go into gmail settings disable secure settings.

You must log in to answer this question.

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