我们通过http协议可以访问网站,同样,我们通过SMTP协议可以发送邮件,python中的smtplib模块对SMTP进行了简单封装,可以实现源地址向目标地址发送邮件。
方法 | 含义 |
---|---|
SMTP(host [, port [, local_hostname]]] ) | 创建smtp对象。 host:主机,如果是本机,需要本机安装sendemail功能,可以是第三方邮件服务商,如smtp.qq.com, smtp.163.com。port是端口号,一般是25 |
SMTP.sendmail(from_addr, to_addrs, msg) | 发送邮件。from_addr发件人的邮箱,to_addrs:收件人的邮箱,msg:发送的邮件信息。msg是smtp 协议中定义的格式. |
简单发送邮件
#因为是在本地模拟登陆客户端,需要本机安装 sendmail(邮件传输代理程序),如果没有可以使用第三方其他邮件服务商的 SMTP 访问(QQ、网易、Google等)
#第三方邮件房屋上smtp服务需要客户端授权密码,用授权密码代替邮箱登陆密码登陆。
#授权密码获取 登陆邮箱——设置——用户——smtp——发送短信——获取授权密码
from smtplib import SMTP
from email.utils import formataddr
from email.header import Header
from email.mime.text import MIMEText
def main():
#发送者
sender = 'senter@163.com'
#登陆密码,用第三方就是客户端授权码
password = 'secretpass'
#接受者
reciver = ['reciver@qq.com','reciver2@qq.com']
#第一参数是正文,第二是格式,第三是编码
msg = MIMEText('You are a good boy,请相信我!!!!','plain','utf-8')
msg['from'] = formataddr(['胖子',sender])
msg['to'] = formataddr(['python菜鸟','1055184304@qq.com'])
msg['subject'] = Header('python学习教程和安排')
try:
smtpObj = SMTP('smtp.163.com',25)
smtpObj.login(sender,password) #登陆
smtpObj.sendmail(sender, reciver, msg.as_string())
print("邮件发送成功")
smtpObj.quit()
except Exception as e:
print(e)
print('邮件发送失败')
if __name__ == '__main__':
main()
效果如下
发送带附件的邮件
附件无论是纯文本文件,还是图片,excel,mp3等,只要转换为二进制文件,都是一样的,再使用MIMEApplication进行包装一下,然后设置一下内容。最后添加到邮件内容。
from smtplib import SMTP
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
from email.header import Header
from email.mime.application import MIMEApplication
def main():
sender = 'sender @163.com'
#d登陆密码或者授权码
password = 'secretpass'
reciver = ['reciver1@163.com','reciver12@qq.com']
#创建带附件的邮件对象
msg = MIMEMultipart()
#正文
msgTest = MIMEText('you are a good boy,哈哈!!附件是课程安排!!','plain','utf-8')
#将附件正文添加到邮件对象
msg.attach(msgTest)
msg['from'] = Header('菜鸟教程',charset='utf-8')
msg['to'] = Header('测试',charset='utf-8')
msg['subject'] = Header('这是一封好邮件',charset='utf-8')
# 读取文件作为附件添加到邮件对象中
textFu = MIMEApplication(open('text.txt','rb').read())
# filename可以随意设置
textFu.add_header('content-disposition', 'attachment', filename='text.txt')
msg.attach(textFu)
#t图片
jpgFu = MIMEApplication(open('935.jpg','rb').read())
jpgFu.add_header('content-disposition', 'attachment', filename='935.jpg')
msg.attach(jpgFu)
#pdf
pdfFu = MIMEApplication(open('citespace使用(2012讲课版).pdf','rb').read())
pdfFu.add_header('content-disposition', 'attachment', filename='citespace使用(2012讲课版).pdf')
msg.attach(pdfFu)
#excel
excelFu = MIMEApplication(open('司法文明创新中心.xlsx','rb').read())
excelFu.add_header('content-disposition', 'attachment', filename='司法文明创新中心.xlsx')
msg.attach(excelFu)
try:
smtpObj = SMTP('smtp.163.com', 25)
smtpObj.login(sender,password)
smtpObj.sendmail(sender,reciver,msg.as_string())
print('发送成功')
smtpObj.quit()
except Exception as e:
print(e)
print('发送失败')
if __name__ == '__main__':
main()
有时候会出现以下错误:
(554, b’DT:SPM 163 smtp2,GtxpCgB3f26wpvhdnlTHAA–.57S2 15765
这是因为163邮件服务商将邮件定义为垃圾邮件,不能发出去。只要在收件人里面,添加发件人邮箱即可。
发送短信
测试使用互亿无线短信平台(注册赠送一定的短信条数,测试够了)
#"测试发送短信功能"
#content内容,未付费用户只能只能使用这个模板
import requests
from random import randint
def main():
num = randint(1000,10000)
data = {
'account':'C36741014',
'password': '1ed67985622d0f83f923f1919cccca33',
'mobile':'18811506911',
'content':'您的验证码是:{}。请不要把验证码泄露给其他人。'.format(num),
'format':'json'
}
response = requests.post('https://106.ihuyi.com/webservice/sms.php?method=Submit',data=data)
print(response.json())
if __name__ == '__main__':
main()
互亿无线API
来源:CSDN
作者:sgcwddhr
链接:https://blog.csdn.net/sgcwudi/article/details/103593123