须要借助第三方平台来发送短信,如阿里云、云通信(对python3不友好)、腾讯云。在这里用的是腾讯云来做为示例html
* 应用列表:管理应用的; * 套餐包管理:管理套餐包的; * SDK & API:就是一些相关的指南手册(开发指南);
更多短信发送参考SDK & API中的开发指南:文档中心 > 短信 > SDK文档 > Python SDK https://cloud.tencent.com/document/product/382/11672python
pip install qcloudsms_py
# 短信应用 SDK AppID 以1400开头 appid = 1400009099 # 短信应用 SDK AppKey 根据本身的短信应用配置 appkey = "9ff91d87c2cd7cd0ea762f141975d1df37481d48700d70ac37470aefc60f9bad" # 须要发送短信的手机号码(非必填项,可在你的开发代码中传入) phone_numbers = ["21212313123", "12345678902", "12345678903"] # 短信模板ID,真实的模板 ID 须要在短信控制台中申请 template_id = 7839 # 这里的模板 ID`7839`只是示例, # 签名,使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名须要在短信控制台中申请 sms_sign = "腾讯云" # 发写个空字符串也行
import random from utils.logging import logger from .settings import * from qcloudsms_py import SmsSingleSender ssender = SmsSingleSender(appid, appkey) # 生成验证码 def get_code(): code = '' for i in range(4): code += str(random.randint(0, 9)) return code def send_sms(mobile, code, exp): """ 发送短信 :param mobile: 电话号码 :param code: 验证码 :param exp: 过时时间 :return: """ try: response = ssender.send_with_param(86, mobile, template_id, (code, exp), sign=sms_sign, extend="", ext="") # 短信发送成功的标识:没有异常且response大字典中的result为0 if response and response['result']==0: return True logger.error('sms error: %s'% response['errmsg']) return except Exception as e: logger.error("sms error: %s" % e) return False if __name__ == '__main__': code = get_code() print(code) result = send_sms('xxxxxxx',code,'1') # 电话号码,验证码,过时时间 print(result)
from email.header import Header from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email import encoders import smtplib import time class EmailPro: def send_mail(self, to_email, code): email_host = 'smtp.163.com' # 服务器地址 163邮箱"smtp.163.com" qq邮箱"smtp.qq.com"都须要开通smtp权限 sender = 'xxx@163.com' # 发件人(本身的邮箱) password = 'xxx' # 邮箱受权码 receiver = 'xxx@qq.com' # 收件人 msg = MIMEMultipart() now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) subject = now + '邮箱激活' h = Header('发件人昵称自定义', 'utf-8') h.append('<xxx@163.com>', 'ascii') msg["From"] = h msg['Subject'] = subject # 标题 msg['To'] = 'xxx' # ...收件人... signature = ''' \n\t You are most welcome! \n\t 点击下面的按钮激活邮箱 ''' # text = MIMEText(signature, 'plain') # 签名 # msg.attach(text) # 正文-图片 只能经过html格式来放图片,因此要注释25,26行 mail_msg = f''' <p>\n\t You are most welcome!</p> <p>\n\t 点击下面的按钮激活邮箱</p> <button style="background-color: #31708f; border-radius: 3px"><a href="http://127.0.0.1:8000/user/email/active/?email={to_email}&code={code}" style="color: white;font-size: 25px;text-decoration: none">激活邮箱</a></button> <p><img src="cid:image1"></p> ''' msg.attach(MIMEText(mail_msg, 'html', 'utf-8')) # 指定图片为当前目录 fp = open(r'E:\rent_house\media\banner\3.jpg', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # 定义图片 ID,在 HTML 文本中引用 msgImage.add_header('Content-ID', '<image1>') msg.attach(msgImage) # ctype = 'application/octet-stream' # maintype, subtype = ctype.split('/', 1) # 附件-图片 # image = MIMEImage(open(r'E:\rent_house\media\banner\3.jpg', 'rb').read(), _subtype=subtype) # image.add_header('Content-Disposition', 'attachment', filename='img.jpg') # msg.attach(image) # 附件-文件 # file = MIMEBase(maintype, subtype) # file.set_payload(open(r'E:\rent_house\apps\utils\response.py', 'rb').read()) # file.add_header('Content-Disposition', 'attachment', filename='test.txt') # encoders.encode_base64(file) # msg.attach(file) # 发送 smtp = smtplib.SMTP() smtp.connect(email_host, 25) smtp.login(sender, password) smtp.sendmail(sender, to_email, msg.as_string()) smtp.quit() print('success') email_worker = EmailPro()