TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
1. 基础语法
1.1 发送邮件SMTP
用书上提供的代码一直显示ConnectionClosed 或者554错误,之后换了一种写法,虽然还是不稳定(有时候能发出去有时候就显示554——垃圾邮件错误)
以下是一种发送成功的写法(不稳定):
- [/code]
- [code]import smtplib
- from email.mime.text import MIMEText
- from email.header import Header
- msg = MIMEText("Hello from the other side","plain","utf-8") #正文
- from_addr = "你的邮箱" #发送邮箱地址
- password = "授权码" #邮箱授权码,非登陆密码
- to_addr = "收件人邮箱" #收件箱地址
- smtp_server = "smtp.163.com" #smtp服务器 在这里用的是163
- msg["From"] = from_addr #发送邮箱地址
- msg["To"] = to_addr #收件箱地址
- msg["Subject"] = "为什么发不出去呢" #主题
- server = smtplib.SMTP(smtp_server,25) #端口465显示connetclosed
- #server.set_debuglevel(1) #打印出和SMTP服务器交互的所有信息
- server.starttls() #SSL加密,感觉这部应该可以不要,因为网易本身就是SSL加密
- server.login(from_addr,password) #邮箱登录
- server.sendmail(from_addr,[to_addr],msg.as_string()) #邮件发送(发件人,收件人群发列表,内容的字符串形式)
- server.quit() #退出登录
复制代码 出现问题
(1)出现554解决方案:
a: 更换邮件主题,不要用“测试”、“test”等主题
b: 使用MIMEtext方法,添加 “From”“To”“Sbuject”关键字,这里采用了这种方法
(2)SSL问题 :ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:777)
将smtplib.SMTP_SSL 更换为 smtplib.SMTP(smtp_server,25)
(3)ConnectionClosed 错误
a:尝试更换端口
b:检查是否开启IMAP/SMTP服务
c:网易163邮箱好像不能频繁发送,需要等待一段时间后在发送就可以了...
(4)认证错误
确保账号填写正确后,密码输入是授权码,而不是邮箱设定的密码
1.2 用Twilio发送短信
(1)注册账号
在 https://www.twilio.com/ 注册新账号,需要验证一个手机,防止用该账号给别的手机发送垃圾短信,得到几个参数,在之后的程序中用到:账户SID,账户AUTH认证标志,你的Twilio号码
(2)发送短信
- from twilio.rest import Client
- accountSID = "ACXXXX" #账户SID
- authtoken = "XXXXXX" #账户AUTH认证标志
- myTwilionumber = "+XXXXX" #你的Twilio号码
- myphonenumber = "+86XXXXX" #接收的手机号
- twiliocli = Client(accountSID, authtoken) #创建一个Client对象
- msg = twiliocli.messages.create(body=message, from_=myTwilionumber, to=myphonenumber) #创建message(正文内容,发件人,收件人)
复制代码 (3)发送状态
- msgsid = msg.sid #显示消息sid
- updatemsg = twiliocli.messages.get(msg.sid) #根据SID更新消息
- print(updatemsg.status) #查询发送状态
- datetime = msg.date_created #查询datetime创建时间 date_sent 为发送时间
- time = datetime.strftime("%Y/%m/%d %H:%M:%S")
- print(time)
复制代码 不过在调用的过程中,显示没有get属性不知道为啥...
2. 实例应用
3. 课后习题
3.1 天气预报短信提醒程序
首先从天气网站调用API得到某地天气,进行JSON解析之后,将信息通过Twilio模块发送给指定号码。本来写了一个具有归属地和手机号码的字典数据结构,想要实现消息的群发,不过Twilio好像只能发送给自己?
- #!python3
- #-*- coding: utf-8 -*-
- # 2018/4/13 0013 10:41
- #天气预报短信提醒程序 输入城市,通过twilio发送短信
- import requests,json,sys
- import twilio
- import datetime
- import time
- from twilio.rest import Client
复制代码- #发送短信列表,手机号-地点
- location = {
- "X1":"+86XXXXXXXXXXX",
- "X2":"+86XXXXXXXXXXX"
- }
- accountSID = "ACXXXXXXXXXXXXXXX"
- authtoken = "XXXXXXXXXXXXXXXXXXX"
- myTwilionumber = "+XXXXXXXXXXXXXXXXX"
- myphonenumber = location.values() #接收的手机号
- def textme(message):
- twiliocli = Client(accountSID, authtoken)
- msg = twiliocli.messages.create(body=message, from_=myTwilionumber, to=myphonenumber)
- for city in location.keys():
- weatherurl = "https://www.sojson.com/open/api/weather/json.shtml?city=%s"%city #天气API
- hello = "%s天气"%city #短信开头
- response = requests.get(weatherurl)
- response.raise_for_status()
- weatherdata = json.loads(response.text)
- data = weatherdata["data"]
- forecast = data["forecast"]
- j = 0
- text = []
- sendsms = []
- for i in range(1): #i为每一天的数据,字典类型
- content = []
- content.append(hello)
- for k,v in forecast[i].items():
- text.append(v) #将天气预报中文信息加到列表中
- for t in text:
- if isinstance(t, str) : #去除不是string的参数,防止报错
- content.append(t) #添加到一个新的列表content
- x = "\n".join(content) #twilio发送list数据时,只会发送第一项,所以要链接为字符串
- print(x)
- textme(x) #调用发短息模块
- time.sleep(4)
复制代码 TBC。。。明天再写
|
|