Skip to content

Instantly share code, notes, and snippets.

@thinkycx
Last active May 1, 2018 02:11
Show Gist options
  • Save thinkycx/2d67f8684210f854511d6d534e02e7a6 to your computer and use it in GitHub Desktop.
Save thinkycx/2d67f8684210f854511d6d534e02e7a6 to your computer and use it in GitHub Desktop.
send you an email after finish the job
#!/usr/bin/env python
# coding=utf-8
# author: thinkycx
# date: 20180430
"""
purpose:
send your an email after finish the job
notice that qq smtp cannot send emails frequently, about 2 times per 10 minutes.
and it's enough!
usage:
0. alias pynotice="python ~/scripts/pynotice.py"
1. pynotice echo 123 # command should not have ' or "
or
2. pynotice ./start.sh
start.sh content -> python -c "import time;time.sleep(10)"
3. pynotice python subDomainsBrute.py thinkycx.me > log | tail -f log
TODO
1. NOHUB ISSUE COMMANT IS NULL
echo "python ~/scripts/pynotice.py" > ~/scripts/pnotice.sh
chmod u+x ~/scripts/pnotice.sh
ln -s ~/scripts/pnotice.sh /bin/pnotice
source ~/.zshrc
"""
import multiprocessing
import os
import sys
import time
import smtplib
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import parseaddr, formataddr
sender = '10000@qq.com'
receiver = [ '10000@qq.com']
subject = {}
subject['from'] = ':)'
subject['to'] = ':)'
subject['subject'] = 'jobs done'
smtpserver = 'smtp.qq.com'
smtpport = '587'
username = '10000@qq.com'
password = '16-token' # get from your qq mail settings
log_name = "pynotice-log.txt"
def start():
command = ' '
command = command.join(sys.argv[1:]) # command is before > or >>
print "[*] command # ", command # command should not have " or '
os.system(command)
def _format_addr(s):
#如果包含中文,需要通过Header对象进行编码。Header对象编码的文本,包含utf-8编码信息和Base64编码的文本
name, addr = parseaddr(s)
return formataddr(( \
Header(name, 'utf-8').encode(), \
addr.encode('utf-8') if isinstance(addr, unicode) else addr))
def sendemail():
command = ' '
command = command.join(sys.argv[1:]) # join nb!
content = "[*] %s jobs done!\r\n" % command
# if os.path.exists(log_name):
# with open(log_name,'r+') as f:
# content += f.read()
msg_text = MIMEText(content, 'html', 'utf-8') # 中文需参数‘utf-8’,单字节字符不需要
msg = MIMEMultipart()
msg['From'] = _format_addr(u'%s<%s>' % (subject['from'], sender))
msg['To'] = _format_addr(u'%s<%s>' % (subject['to'], receiver))
msg['Subject'] = Header(u'%s' % subject['subject'], 'utf-8').encode()
msg.attach(msg_text)
smtp = smtplib.SMTP()
# smtp.set_debuglevel(1)
smtp.connect(smtpserver, smtpport)
smtp.starttls() #ssl加密
time.sleep(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
if __name__ == "__main__":
start_time = time.time()
proc = multiprocessing.Process(target=start )
proc.deamon = True
proc.start()
proc.join()
print "[*] total time # ", time.time() - start_time
try:
sendemail()
except Exception as e:
print e
print "[!] job is done! email send failed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment