Python crawler sends email

created at 06-26-2021 views: 2

Send HTML email

# -*- encoding=UTF-8 -*-
'''
author:vfast
name:spider
data:2021/6/24
'''

from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


# Sender address
from_addr = 'xxx@qq.com'
# email Password
password = 'Authorization code'
# receiver's address
to_addr = 'xxxx'

# email server
smtp_server = 'smtp.qq.com'

# Set email information
msg = MIMEText('<html><body><h1>Hello</h1>'+'<p>Abnormal page<a href="http://www.cnblogs.com">cnblogs</a>...</p>'+'</body></html>', 'html', 'utf-8')
msg['From'] = _format_addr('No. 1 crawler<%s>' % from_addr)
msg['To'] = _format_addr('admin<%s>' % to_addr)
msg['Subject'] = Header('No. 1 crawler running status', 'utf-8').encode()

# send email
server = smtplib.SMTP(smtp_server, 25)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()

Send plain text mail

# -*- encoding=UTF-8 -*-
'''
author:vfast
name:spider
data:2021/6/24
'''

from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


# Sender address
from_addr = 'xxxx@qq.com'
# email Password
password = 'Authorization code'
# receiver's address
to_addr = 'xxxx'

# Mailbox server
smtp_server = 'smtp.qq.com'

# Set email information
msg = MIMEText('Python crawler runs abnormally, prompt 403', 'plain', 'utf-8')
msg['From'] = _format_addr('No. 1 crawler<%s>' % from_addr)
msg['To'] = _format_addr('admin<%s>' % to_addr)
msg['Subject'] = Header('No. 1 crawler running status', 'utf-8').encode()

# send email
server = smtplib.SMTP(smtp_server, 25)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
created at:06-26-2021
edited at: 06-26-2021: