0
点赞
收藏
分享

微信扫一扫

py 发送邮件和短信

googlefrank 2022-02-04 阅读 68
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from urllib.parse import quote

#创建邮件主体对象
email = MIMEMultipart()
#设置发件人、收件人和主题
email['From'] = '396434855@qq.com'
email['To'] = '484439767@qq.com'
email['Subject'] = Header('test', 'utf-8')
#添加邮件正文内容
content = """<p>亲爱的前同事:</p>
<p>你需要的离职证明在附件中,请查收!</p>
<br>
<p>祝,好!</p>
<hr>
<p>孙美丽 即日</p>"""
email.attach(MIMEText(content, 'html', 'utf-8'))
#创建SMTP_SSL对象(连接邮件服务器)
with open(f'王大锤离职证明.docx', 'rb') as file:
    attachment = MIMEText(file.read(), 'base64', 'gbk')
    #指定内容类型
    attachment['content-type'] = 'application/octet-stream'
    #将中文文件名处理成百分号编号
    filename = quote('王大锤离职证明.docx')
    attachment['content-disposition'] = f'attachment; filename="{filename}"'
    email.attach(attachment)

smtp_obj = smtplib.SMTP_SSL('smtp.qq.com', 465)
#通过用户名和授权码进行登录
smtp_obj.login('396434855@qq.com', 'tesyfqhfuofqcaee')
#发送邮件(发件人、收件人、邮件内容(字符串))
smtp_obj.sendmail(
    '396434855@qq.com',
    '484439767@qq.com',
    email.as_string()
)
print(email.as_string())

import random
import requests

def send_message_by_luosimao(tel, message):
    """发送短信"""
    resp = requests.post(
        url='http://sms-api.luosimao.com/v1/send.json',
        auth=('api','f2ecf586f7e3a6822f6144de9a810fc2'),
        data={
            'mobile':tel,
            'message':message
        },
        timeout=10,
        verify=False
    )
    return resp.json()
def gen_mobile_code(length=6):
    return ''.join(random.choices('0123456abcde'))

code = gen_mobile_code()
message = f'验证码是{code} 哈哈哈哈'
print(send_message_by_luosimao('15533946558', message))
举报

相关推荐

0 条评论