代码库-python推送邮件

#coding=utf-8
# #!/usr/bin/python3
# @author zouwh
# @date 2020-10-29
# python推送邮件代码库

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from datetime import datetime, timedelta
import sys

my_sender = 'xxx@xxx.com'  # 发件人邮箱账号
my_pass = 'xxx'  # 发件人邮箱密码
subject = "邮件推送报表信息"  # 邮件的主题

def send_mail(context, filename1=None):
    my_user = email_arr
    print("my_user: " + ",".join(my_user))

    m = MIMEMultipart()
    m['From'] = formataddr(["python推送邮件测试", my_sender])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
    m['To'] = formataddr(["人员", ','.join(my_user)])  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
    m['Subject'] = subject
    m.attach(context)

    # 添加附件
    if filename1 is not None:
        attachment = MIMEApplication(open(filename1, 'rb').read())
        attachment.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', '音乐类型及特点.csv'))
        m.attach(attachment)

    try:
        server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)  # 发件人邮箱中的SMTP服务器
        server.login(my_sender, my_pass)  # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(my_sender, my_user, m.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
        return True
    except Exception as e:
        print(e)
        return False


if __name__ == '__main__':
    email_str = "xxx@xxx.com"
    email_arr = email_str.split(',')
    filename1 = sys.argv[1]
    print("email_str: " + email_str)

    html = """<font size="3">邮件附件推送音乐类型及特点<br>"""
    context = MIMEText(html, _subtype='html', _charset='utf-8')

    if send_mail(context,filename1):
        print("发送成功")
    else:
        print("发送失败")

效果:
在这里插入图片描述

Python可以通过企业微信开放的接口实现消息推送功能。 首先,需要在企业微信后台注册应用并获取应用的相关参数,比如应用ID、应用密钥等。然后,使用Python的requests发送HTTP请求来调用企业微信提供的消息推送接口。 可以使用以下代码示例通过企业微信发送文本消息: ```python import requests import json # 获取企业微信应用的相关信息 corpid = '企业ID' corpsecret = '应用密钥' agentid = '应用ID' # 获取access_token url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}' response = requests.get(url) access_token = json.loads(response.text)['access_token'] # 构造消息内容 message = { 'touser': '@all', # 发送给所有用户 'msgtype': 'text', # 发送文本消息 'agentid': agentid, 'text': { 'content': '这是一条测试消息' # 消息内容 } } # 发送消息 send_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}' response = requests.post(send_url, data=json.dumps(message)) result = json.loads(response.text) if result['errcode'] == 0: print('消息发送成功') else: print('消息发送失败') ``` 上述代码首先获取了access_token,然后构造了一个文本消息的消息体,包含消息内容和发送对象(这里使用@all表示发送给所有用户)。最后,通过POST请求将消息发送给企业微信。 在实际使用中,可以根据需要自定义消息内容、发送对象等相关参数。此外,企业微信还支持推送图片、文件、图文等多种类型的消息,开发者可以按需进行配置。 以上是使用Python实现通过企业微信进行消息推送的简单示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值