Skip to content

Instantly share code, notes, and snippets.

@shanecowherd
Created October 11, 2019 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecowherd/c74928754839e28261ab2c46577d4283 to your computer and use it in GitHub Desktop.
Save shanecowherd/c74928754839e28261ab2c46577d4283 to your computer and use it in GitHub Desktop.
Bash script to automate sending email from Mail.app using AppleScript. (For debugging)
#!/bin/bash
# Generate a random text email and send it to test@example.com
# Used for testing email apps when you need a lot of messages in a test account.
# Configure the script in this section.
NUMBER_OF_MESSAGES=10
WORDS_IN_MESSAGE=10
RECIPIENT=test@example.com
WORDFILE=/usr/share/dict/words
# using cat means wc outputs only a number, not number followed by filename
LINECOUNT=$(cat $WORDFILE | wc -l);
until [ $NUMBER_OF_MESSAGES -lt 1 ]; do
let NUMBER_OF_MESSAGES-=1
MESSAGE_WORDS=""
# Get random line numbers in the word file.
RANDOM_NUMBER_LIST=`jot -w %d -n -r -p 1 $WORDS_IN_MESSAGE 1 $LINECOUNT`
echo $RANDOM_NUMBER_LIST
for RANDWORDINDEX in $RANDOM_NUMBER_LIST
do
MESSAGE_WORDS="$MESSAGE_WORDS `sed -n "$RANDWORDINDEX p" $WORDFILE;`"
done
echo Words: $MESSAGE_WORDS
subjectRand=`jot -w %d -n -r -p 1 1 1 $LINECOUNT`
SUBJECT=`sed -n "$subjectRand p" $WORDFILE;`
echo Subject: $SUBJECT
osascript -e "
tell application \"Mail\"
set newMessage to make new outgoing message with properties {visible:true, subject:\"Random: $SUBJECT\", content:\"$MESSAGE_WORDS\"}
tell newMessage to make new to recipient at end of every to recipient with properties {address:\"$RECIPIENT\"}
send newMessage
end tell
"
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment