python可以发短信吗_python 发微信

python可以发短信吗_python 发微信Python 发送短信 邮件 微信消息和通过企业微信发送消息的方法如下 发送短信 注册腾讯云账号 访问腾讯云官网 注册账号 创建签名和短信模板 获取短信应用参数 获取 AppID 和 AppKey 获取短信模板 ID 安装腾讯云短信 SDK 使用 pip3 install qcloudsms py 安装 编写发送程序 使用以下代码示例发送短信 pythonimport sslfrom

Python发送短信、邮件、微信消息和通过企业微信发送消息的方法如下:

发送短信

注册腾讯云账号

访问腾讯云官网,注册账号。

创建签名和短信模板。

获取短信应用参数

获取AppID和AppKey。

获取短信模板ID。

安装腾讯云短信SDK

使用`pip3 install qcloudsms_py`安装。

编写发送程序

使用以下代码示例发送短信:

 import ssl from qcloudsms_py import SmsSingleSender 配置参数 appid = "你的APPID" appkey = "你的APPKey" template_id = "你的模板ID" phone_numbers = ["要接收的号码"] sms_sign = "你的签名" 创建发送对象 ssender = SmsSingleSender(appid, appkey) params = [] 发送短信 ssender.send_with_param(86, phone_numbers, template_id, params, sign=sms_sign, extend="", ext="") 

发送邮件

安装邮件发送库

使用`pip install smtplib`安装。

配置SMTP服务器

设置发件人、收件人、SMTP服务器地址、端口、用户名和密码。

编写发送邮件程序

使用以下代码示例发送邮件:

 import smtplib from email.mime.text import MIMEText from email.header import Header 配置参数 sender = "" receivers = [""] 创建邮件对象 message = MIMEText("邮件内容\nPython 邮件发送测试...", "plain", "utf-8") message["From"] = Header("Python邮件测试", "utf-8") message["To"] = Header("测试接收者", "utf-8") 邮件主题 subject = "Python SMTP 邮件测试" message["Subject"] = Header(subject, "utf-8") SMTP服务器配置 smtp_server = "smtp.example.com" smtp_port = 25 smtp_user = "" smtp_password = "password" 发送邮件 try: smtpObj = smtplib.SMTP(smtp_server, smtp_port) smtpObj.login(smtp_user, smtp_password) smtpObj.sendmail(sender, receivers, message.as_string()) smtpObj.quit() except smtplib.SMTPException as e: print("Error: 无法发送邮件", e) 

发送微信消息

安装itchat库

使用`pip install itchat`安装。

登录微信

使用`itchat.auto_login()`自动登录,或手动扫描二维码登录。

发送消息

使用以下代码示例发送微信消息:

 import itchat 自动登录 itchat.auto_login(hotReload=True) 可选,用于缓存登录状态 搜索好友 userfinfo = itchat.search_friends("好友昵称") 发送消息 itchat.send("消息内容", userid) 通过用户ID发送 或 itchat.send_msg(msg="消息内容", toUserName=userid) 通过用户名发送 

通过企业微信发送消息

创建企业微信应用

登录企业微信管理后台,创建应用并获取AppID和AppSecret。

获取access_token

使用`https://qyapi.weixin..com/cgi-bin/gettoken`接口获取。

发送消息

使用`https://qyapi.weixin..com/cgi-bin/message/send`接口发送。

示例代码如下:

编程小号
上一篇 2025-01-25 23:28
下一篇 2025-01-25 23:24

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/132598.html