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
|
import sys import smtplib from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart
mail_host="smtp.sina.com" mail_user="username" mail_pass="password" sender = '发送人' receivers = ['接收邮箱1','接收邮箱2']
def send_file(title,file): message = MIMEMultipart() message['From'] = Header("发送人") subject = title message['Subject'] = Header(subject, 'utf-8')
message.attach(MIMEText(title, 'plain', 'utf-8'))
att1 = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="att.log"' message.attach(att1) smtpObj = smtplib.SMTP() try: smtpObj.connect(mail_host, 25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "邮件发送成功" except smtplib.SMTPException: print "Error: 无法发送邮件"
def send_html(title,msg): message = MIMEMultipart() message['From'] = Header("发送人") subject = title message['Subject'] = Header(subject, 'utf-8')
message.attach(MIMEText("<font color='red'>"+msg+"</font>", 'html', 'utf-8')) smtpObj = smtplib.SMTP() try: smtpObj.connect(mail_host, 25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "邮件发送成功" except smtplib.SMTPException: print "Error: 无法发送邮件"
if __name__ == "__main__": if len(sys.argv) < 3: print "usage: python send_mail.py title msg" sys.exit(1) title = sys.argv[1] msg = sys.argv[2] send_html(title,msg)
|