Skip to content

Instantly share code, notes, and snippets.

@izuolan
Created March 11, 2017 05:56
Show Gist options
  • Save izuolan/831a2781afaa31fcf67512ce5746b523 to your computer and use it in GitHub Desktop.
Save izuolan/831a2781afaa31fcf67512ce5746b523 to your computer and use it in GitHub Desktop.
检测 Gmail 新邮件并发送通知到桌面,同时弹窗提醒。
#! /bin/bash
# 账号密码
USER_NAME="[email protected]"
PASSWORD="user_password"
# 如果想使用邮件客户端打开可以替换,默认打开浏览器。
MAIL_SOFTWARE="xdg-open $LATEST_MAIL_LINK"
# 获取 XML
XML=$(curl -su $USER_NAME:$PASSWORD "https://mail.google.com/mail/feed/atom" | tr -d '\n' | sed 's/&amp;/\&/g' | sed 's:</entry>:\n:g')
# 获取未读邮件数量
UNREAD=$(echo $XML | sed -n 's/.*<fullcount>\(.*\)<\/fullcount.*/\1\n/p')
# 获取最新未读邮件
LATEST_MAIL=$(echo $XML | sed -n 's/.*<fullcount>\(.*\)<\/fullcount.*<title>\(.*\)<\/title.*<summary>\(.*\)<\/summary.*href="\(.*\)" type.*<author><name>\([^<]*\)<\/name><email>\([^<]*\).*/\1*\2*\3*\4*\5*\6\n/p')
# 获取所有未读邮件
MAIL_LIST=$(echo $XML | sed -n 's/.*<title>\(.*\)<\/title.*<summary>\(.*\)<\/summary.*href="\(.*\)" type.*<author><name>\([^<]*\)<\/name><email>\([^<]*\).*/\1*\2*\3*\4\5\n/p')
while IFS=* read UNREAD_COUNT LATEST_MAIL_TITLE LATEST_MAIL_SUMMARY LATEST_MAIL_LINK LATEST_FROM_NAME LATEST_FROM_MAIL
do
# 判断是否有未读邮件,没有就退出,有则发送通知到桌面
if [ "$UNREAD" = "0" ]; then
exit 0
else
notify-send -i geary 您有$UNREAD_COUNT封未读邮件 "$LATEST_FROM_NAME[$LATEST_FROM_MAIL]<br>$LATEST_MAIL_TITLE"
zenity --question --ellipsize --title="新邮件" --icon-name=geary --text="发件人:$LATEST_FROM_NAME\n发件人邮箱:$LATEST_FROM_MAIL\n邮件标题:$LATEST_MAIL_TITLE\n邮件摘要:$LATEST_MAIL_SUMMARY" >/dev/null 2>&1
# 选择“OK”打开指定邮件网页
[ $? -eq 0 ] && $MAIL_SOFTWARE >/dev/null 2>&1 &
fi
done << EOF
$LATEST_MAIL
EOF
# 获取未读邮件列表
#while IFS=* read MAIL_TITLE MAIL_SUMMARY MAIL_LINK FROM_NAME FROM_MAIL
# do
# echo $MAIL_LIST
# done << EOF
#$MAIL_LIST
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment