summaryrefslogtreecommitdiff
path: root/mail2mms.sh
blob: adc51d6815c01f1e67c5a38e501e45a3545be798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh

# Send an MMS message to your phone when new emails come in.
# The message will have the number of new mails in the subject
# and the body will contain the From and Subject fields of up to the 3
# most recent new mails. If the new mail count is above 3, there will also
# be 'and more...' at the end.
# Assumes you either have mbsync setup to sync your inbox folder,
# or use -l if this machine is actually your mailserver

# import the config file ./.config
# addr=<the address the notification will appear to be sent from>
# phone=<your phone number in email address form (eg, 9876543210@mms.att.net)>
# inbox=<the location of the maildir folder corresponding to the inbox>
# smtp_server=<the address and port (address:port) of your smtp server>
# smtp_user=<the username you use on your smtp server>
# smtp_password<the password to login to the smtp server>

. ./.config

oldnew=0

summary() {
  i=0
  for m in $(\ls -1r ${inbox}/new/*); do
    i=$((i + 1))
    if [ ${i} -gt 3 ]; then
      echo "and more..."
      break
    fi
    subject=$(grep ^Subject: ${m} | head -n 1)
    from=$(grep ^From: ${m} | head -n 1)
    echo ${from}
    echo ${subject}
    echo
  done
}

while true; do

  # sync mailbox if this is not the mailserver
  if [ "$1" = "-l" ]; then
    mbsync -a
  fi

  # Count the number of new mails
  newnew=$(\ls -1 ${inbox}/new | wc -l)

  # If the number of new mails has increased  
  if [ ${newnew} -gt ${oldnew} ]; then

    # If we are on the mailserver, just mail the alert out from here
    if [ "$1" = "-l" ]; then
      echo "$(summary)" \
      | mail -r ${addr} \
        -s "new mail [${newnew}]" \
        ${phone}

    # Otherwise, use the smtp configuration from the config file to send it
    else
      echo "$(summary)" \
      | mail -r ${addr} \
        -s "new mail [${newnew}]" \
        -S smtp=${smtp_server} \
        -S smtp-use-starttls \
        -S smtp-auth=login \
        -S smtp-auth-user=${smtp_user} \
        -S smtp-auth-password=${smtp_password} \
        ${phone}
    fi
  fi

  oldnew=${newnew}
  sleep 2m
done