邮件发送脚本

python调用外部smtp服务器发送邮件

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
#!/usr/bin/env python

from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Utils,Encoders
import mimetypes,sys
import smtplib

def send_mail(user,passwd,host,context,sub,to_user):
self_user = user
self_pass = passwd
self_smtp = host
self_sub = sub
self_context = context
self_touser = to_user

msg = MIMEMultipart()
msg["To"] = self_touser
msg["From"] = "xxxx@unxn.com"
msg["Subject"] = self_sub
msg["Data"] = Utils.formatdate(localtime = 1)

body = MIMEText(self_context,_subtype = "plain")

msg.attach(body)
send = smtplib.SMTP(self_smtp)
send.login(self_user,self_pass)
send.sendmail(self_user,self_touser,msg.as_string())
send.close()

if __name__=='__main__':
host= "smtp.qq.com"
user= "xxxxxxxx@qq.com"
passwd= "xxxxxxxx"
to_user = "xxxxxxxxx@163.com"
sub = "Log anaylse result!"

f = open("log","r")
context = f.read()
f.close()

send_mail(user,passwd,host,context,sub,to_user)