Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python发送邮件 #123

@qingquan-li

Description

@qingquan-li

参考:


应用场景:

  • 发送网站访问失败、应用测试失败的异常情况。

如果本机 localhost 没有安装 Sendmail、Postfix 等 SMTP 客户端,可以使用其他邮件服务商的 SMTP 访问,如QQ(smtp.qq.com)、网易(smtp.163.com)。

QQ邮箱

import smtplib
from email.mime.text import MIMEText

# 设置服务器所需信息
# QQ邮箱服务器地址
# 如果本机localhost没有安装Sendmail、Postfix等SMTP客户端,可以使用其他邮件服务商的SMTP访问,如QQ、网易。
mail_host = 'smtp.qq.com'  
# 邮箱用户名
mail_user = '[email protected]'
# 密码(部分邮箱为授权码)
mail_pass = 'xxxxxxxxxxxxxxxx' # 这里写16位QQ邮箱授权码
# 发件方邮箱地址
sender = '[email protected]' # 一般和邮箱用户名一样
# 收件方邮箱地址,注意这里是[]列表,意味着可以写多个邮件地址群发
receivers = ['[email protected]']

# 设置Email信息
# 邮件内容设置
# 使用MIMEText对象,为底层的MIME协议传输创建了一封空邮件,最后通过高层的SMTP协议发送出去。
# MIMEText对象message包括收发邮箱地址、正文和主题,Python通过它就可以创建一封格式正确的邮件。
message = MIMEText('Email body. The body of the mail is here.', 'plain', 'utf-8')
# 邮件主题       
message['Subject'] = 'Email title'
# 发送方信息
message['From'] = sender
# 接受方信息
message['To'] = receivers[0]

# 登录并发送邮件
try:
    smtpObj = smtplib.SMTP()
    # 连接到服务器
    smtpObj.connect(mail_host, 25)
    # 登录到服务器
    smtpObj.login(mail_user, mail_pass)
    # 发送
    smtpObj.sendmail(sender, receivers, message.as_string())
    # 退出
    # smtplib模块用来设置服务器连接的相关信息,用完后需关闭,以免同时创建太多连接而浪费资源。
    smtpObj.quit()
    print('success')
except smtplib.SMTPException as e:
    print('error', e) # 打印错误

Gmail




使用实例:每小时检查一次 https://isitchristmas.com 网站,根据日期判断当天是不是圣诞节,进行邮件通知。

import time
from urllib.request import urlopen

import smtplib
from email.mime.text import MIMEText
from bs4 import BeautifulSoup

# 设置服务器所需信息
# QQ邮箱服务器地址
mail_host = 'smtp.qq.com'  
# 邮箱用户名
mail_user = '[email protected]'
# 密码(部分邮箱为授权码)
mail_pass = 'xxxxxxxxxxxxxxxx' # 这里写16位QQ邮箱授权码
# 发件方邮箱地址
sender = '[email protected]'
# 收件方邮箱地址,注意这里是[]列表,意味着可以写多个邮件地址群发
receivers = ['[email protected]'] # 可以发送给自己

def send_mail(subject, body):
    # 设置Email信息
    # 邮件内容设置
    # 使用MIMEText对象,为底层的MIME协议传输创建了一封空邮件,最后通过高层的SMTP协议发送出去。
    # message = MIMEText('Email body. The body of the mail is here.', 'plain', 'utf-8')
    message = MIMEText(body)
    # 邮件主题       
    message['Subject'] = subject
    # 发送方信息
    message['From'] = sender
    # 接受方信息
    message['To'] = receivers[0]

    # 登录并发送邮件
    smtpObj = smtplib.SMTP()
    # 连接到服务器
    smtpObj.connect(mail_host, 25)
    # 登录到服务器
    smtpObj.login(mail_user, mail_pass)
    # 发送
    smtpObj.sendmail(sender,receivers,message.as_string())
    # 退出
    # smtplib模块用来设置服务器连接的相关信息,用完后需关闭,以免同时创建太多连接而浪费资源。
    smtpObj.quit()

# 每小时检查一次 https://isitchristmas.com 网站,根据日期判断当天是不是圣诞节,进行邮件通知。
while True:
    html = urlopen('https://isitchristmas.com')
    bsObj = BeautifulSoup(html, 'html.parser')
    # 在不同地区访问,源代码里显示不一样。例如在中国大陆访问,源代码显示为:不是。
    # 这里只对在中国大陆、美国、香港地区访问显示的语言作判断。
    if (bsObj.find('a', {'id':'answer'}).attrs['title'] == '不是' 
                or bsObj.find('a', {'id':'answer'}).attrs['title'] == 'NO'
                or bsObj.find('a', {'id':'answer'}).attrs['title'] == '唔係'):
        print('It is not Christmas yet.')
        time.sleep(3600)
    else:
        send_mail("It's Christmas!", "According to https://isitchristmas.com , it is Christmas!")
        time.sleep(3600)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions