在Python里我们可以使用smtplib模块发送email,smtp(Simple Mail Transfer Protocal),是简单邮件传输协议,通过它可以和smtp server进行通讯。
-
from datetime import datetime,timedelta,time
-
import smtplib
-
import time
-
from email.mime.text import MIMEText
-
from email.mime.image import MIMEImage
-
from email.mime.multipart import MIMEMultipart
-
-
date_from = datetime.now()+timedelta(days=-7)
-
_date_from = date_from.strftime("%Y-%m-%d")
-
date_to = datetime.now()+timedelta(days=-3)
-
_date_to = date_to.strftime("%Y-%m-%d")
-
-
-
def send_mail_details():
-
#first we compose some of the basic message headers:
-
from_address = "shi.dong@163.com"
-
to_address = "shi.dong@163.com"
-
cc_address = "shi.dong@163.com"
-
msg = MIMEMultipart()
-
msg['From'] = from_address
-
msg['To'] = to_address
-
msg['Cc'] = cc_address
-
msg['Subject'] = "dongyuegerenzhoubao"+ _date_from + "--" + _date_to
-
-
#next we attach the body of the email to the MIME message:
-
body = "python test email"
-
msg.attach(MIMEText(body,'plain'))
-
-
#For sending the mail, we have to convert the object to a string, and then use the same prodecure as above to send sing the SMTP server..
-
server = smtplib.SMTP('*****')
-
text = msg.as_string()
-
-
return server.sendmail(from_address, to_address, text)
-
-
-
-
-
-
-
if __name__ == '__main__':
-
send_mail_details()
阅读(2625) | 评论(2) | 转发(0) |