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?


It can all be done with only curl.

Send email

MSG="From: a@gmail.com
To: Receiver
Subject: Test"

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

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

EXISTS="$(curl --insecure \
--user "a@gmail.com: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 "a@gmail.com: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.