#-*- coding:utf-8 -*-
import smtplib
import sys
import random
import socket
from email.mime.text import MIMEText
true_socket = socket.socket
def bound_socket(*a, **k):
sock = true_socket(*a, **k)
#随机出一个IP
ip_list = ['XXX.XXX.XXX.XXX','XXX.XXX.XXX.XXX']
index = random.randint(1,1000000)
ip_index = 0
if index>=1 and index<=500000:
ip_index = 0
else:
ip_index = 1
ip = ip_list[ip_index]
sock.bind((ip, 0))
return sock
#绑定IP
socket.socket = bound_socket
mail_host="smtp.XXX.XXX" #smtp
mail_user="XXX@XXX.XXX" #user
mail_pass="XXX" #pwd
mail_postfix="XXX.com" #发件箱的后缀
def send_mail(to_list,sub,content):
me="<XXX@XXX.XXX>"
msg = MIMEText(content,_subtype='html',_charset='utf-8')
msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8"
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
return e
if __name__ == '__main__':
#获取参数
mailto = [sys.argv[1]]
title = sys.argv[2]
content = sys.argv[3]
status = send_mail(mailto,title,content)
if status == True:
print 1
else:
print status