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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
import smtplib from email.mime.text import MIMEText from email.header import Header from smtplib import SMTP_SSL
from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.mime.text import MIMEText from email.mime.base import MIMEBase from email.encoders import encode_base64 import os import traceback
def send_mail(mail_title, mail_content=None, attachment_img=None, attachment_txt=None, attachment_pdf=None, attachment_excel=None, attachment_word=None): host_server = 'smtp.qq.com' sender_qq = '947118251' pwd = 'tvjl******zpbebb' sender_qq_mail = '947118251@qq.com' receiver = '947118251@qq.com'
try: smtp = SMTP_SSL(host_server) smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd)
msg = MIMEMultipart('related') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = receiver
msgAlternative = MIMEMultipart('alternative') msg.attach(msgAlternative)
if attachment_img: mail_body = '<b>%s</b><br><img src="cid:%s"><br>' % (mail_content, attachment_img) msgText = (MIMEText(mail_body, 'html', 'utf-8')) msgAlternative.attach(msgText) with open(attachment_img, 'rb') as fp: msgImage = MIMEImage(fp.read()) msgImage.add_header('Content-ID', '<{}>'.format(attachment_img)) msg.attach(msgImage)
if attachment_txt: file_name = os.path.split(attachment_txt)[1] att1 = MIMEText(open(attachment_txt, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = f'attachment; filename="{file_name}"' msg.attach(att1)
if attachment_pdf: with open(attachment_pdf, "rb") as fp: fileMsg = MIMEBase('application', 'pdf') fileMsg.set_payload(fp.read()) encode_base64(fileMsg) fileMsg.add_header('Content-Disposition', f'attachment;filename={os.path.split(attachment_pdf)[1]}') msg.attach(fileMsg)
if attachment_excel: part = MIMEBase('application', "vnd.ms-excel") with open(attachment_excel, "rb") as fp: part.set_payload(fp.read()) encode_base64(part) part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(attachment_excel)[1]}"') msg.attach(part)
if attachment_word: with open(attachment_word, "rb") as fp: part = MIMEApplication(fp.read()) part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(attachment_word)[1]}"') part.set_charset('utf-8') msg.attach(part)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit() print('Success!') except: print('Error!') traceback.print_exc()
if __name__ == '__main__': send_mail(mail_title='爬虫结束了,正常退出!', mail_content='你好,这是使用python登录qq邮箱发邮件的测试', attachment_img='../data/test.jpg', attachment_txt='../data/start_urls.txt', attachment_pdf='../data/Gmail - How to add images in the product description_.pdf', attachment_excel='../data/shops.xlsx', attachment_word='../data/asdasd.docx')
|