SMTP发送邮件的用处是 可以让程序运行出错时给自己的手机发送出错日志。就可以及时刚回来给修改BUG。
代码:
from smtplib import SMTP
from email.mime.text import MIMEText
from email.header import Header
def send_email(SMTP_host, from_account, from_passwd, to_account, subject, content):
email_client = SMTP(SMTP_host)
email_client.login(from_account, from_passwd)
# create msg
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = from_account
msg['To'] = to_account
email_client.sendmail(from_account, to_account, msg.as_string())
email_client.quit()
if __name__ == "__main__":
'''
print('请登录您的邮箱!')
from_account = input('please input your email: ')
from_passwd = input('Password: ')
SMTP_host = input('SMTP_host(like smtp.163.com): ')
to_account = input('to_send email: ')
subject = input('the theme:\n')
content = input('Please write your words:\n')
send_email(SMTP_host,from_account,from_passwd,to_account,subject,content)
'''
send_email('smtp.163.com','qton_mail@163.com','*********','qton_pri@163.com','哈哈哪有什么主题','哈哈哪有什么内容 不过是试试程序而已!哈哈')
发送带图片附件邮件
import cv2
import numpy as np
import sys
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
sender = 'wbp2010_love@yeah.net'
receiver = ['qton_pri@163.com']
smtpserver = 'smtp.yeah.net'
username = 'wbp2010_love@yeah.net'
password = '***********'
message = MIMEMultipart()
message['From'] = Header("wbp2010_love@yeah.net", 'utf-8')
message['To'] = Header("qton_pri@163.com", 'utf-8')
subject = 'Qton'
message['Subject'] = Header(subject, 'utf-8')
message.attach(MIMEText('这是邮件正文哦', 'plain', 'utf-8'))
for i in range(1,31):
try:
time.sleep(1)
# cap = cv2.VideoCapture(0)
# ret,frame = cap.read()
# cv2.imwrite('../../12/000.jpg',frame)
# time.sleep(3)
att1 = MIMEText(open('F:/Scraping/5/%s.jpg'%i, 'rb').read(), 'base64', 'utf-8')
att1["Content-Disposition"] = 'attachment; filename="0.jpg" '
message.attach(att1)
print ('添加图片成功')
except smtplib.SMTPException:
print ('添加图片失败!\\')
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, message.as_string())
smtp.quit()
except:
print('邮件发送出错!')