python 读取麦克风,并发送邮件

1、设置邮箱

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
# from PIL import ImageGrab
from io import BytesIO
import time
import os
import pyaudio
import wave
 
#设置服务器所需信息
#163邮箱服务器地址
mail_host = 'smtp.163.com'  
#163用户名
mail_user = '****'  
#密码(部分邮箱为授权码) 
mail_pass = '******'   
#邮件发送方邮箱地址
sender = '*****@163.com'  
#邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
# receivers = ['*****@163.com'] 
receivers = ['******@qq.com']

2、读取麦,并存入io缓存

def record_audio(img_byte,record_second):
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 16000
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    wf = wave.open(img_byte, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    print("* recording")
    for i in range(0, int(RATE / CHUNK * record_second)):
        data = stream.read(CHUNK)
        wf.writeframes(data)
    print("* done recording")
    stream.stop_stream()
    stream.close()
    p.terminate()
    wf.close()
    return img_byte

3、发送邮件

def mysend():    
    message = MIMEMultipart()
    #发送方信息
    message['From'] = sender 
    #接受方信息     
    message['To'] = receivers[0] 
    #邮件主题       
    message['Subject'] = time.strftime("%d/%m/%Y-%H:%M:%S")

    img_byte = BytesIO()
    img_byte=record_audio(img_byte,record_second=10)    
    myaudio=MIMEAudio(img_byte.getvalue())
    myaudio['Content-Type'] = 'application/octet-stream'
    myaudio['Content-Disposition'] = 'attachment;filename="%s.wav"'%time.strftime("%d/%m/%Y-%H:%M:%S")
    message.attach(myaudio)
    print(time.strftime("%d/%m/%Y-%H:%M:%S"))
            
    #登录并发送邮件
    try:
        smtpObj = smtplib.SMTP() 
        #连接到服务器
        smtpObj.connect(mail_host,25)
        #登录到服务器
        smtpObj.login(mail_user,mail_pass) 
        #发送
        smtpObj.sendmail(
            sender,receivers,message.as_string()) 
        #退出
        smtpObj.quit() 
        print('send success')
    except smtplib.SMTPException as e:
        print('error',e) #打印错误`

4、完整流程

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
# from PIL import ImageGrab
from io import BytesIO
import time
import os
import pyaudio
import wave

#设置服务器所需信息
#163邮箱服务器地址
mail_host = 'smtp.163.com'  
#163用户名
mail_user = 'bzwy132'  
#密码(部分邮箱为授权码) 
mail_pass = 'IYZTTTFODQTMJKCI'   
#邮件发送方邮箱地址
sender = 'bzwy132@163.com'  
#邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
receivers = ['bzwy132@163.com'] 
# receivers = ['1163038589@qq.com']

def record_audio(img_byte,record_second):
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 16000
    p = pyaudio.PyAudio()
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    wf = wave.open(img_byte, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    print("* recording")
    for i in range(0, int(RATE / CHUNK * record_second)):
        data = stream.read(CHUNK)
        wf.writeframes(data)
    print("* done recording")
    stream.stop_stream()
    stream.close()
    p.terminate()
    wf.close()
    return img_byte
 
def mysend():    
    message = MIMEMultipart()
    #发送方信息
    message['From'] = sender 
    #接受方信息     
    message['To'] = receivers[0] 
    #邮件主题       
    message['Subject'] = time.strftime("%d/%m/%Y-%H:%M:%S")

    img_byte = BytesIO()
    img_byte=record_audio(img_byte,record_second=10)    
    myaudio=MIMEAudio(img_byte.getvalue())
    myaudio['Content-Type'] = 'application/octet-stream'
    myaudio['Content-Disposition'] = 'attachment;filename="%s.wav"'%time.strftime("%d/%m/%Y-%H:%M:%S")
    message.attach(myaudio)
    print(time.strftime("%d/%m/%Y-%H:%M:%S"))
            
    #登录并发送邮件
    try:
        smtpObj = smtplib.SMTP() 
        #连接到服务器
        smtpObj.connect(mail_host,25)
        #登录到服务器
        smtpObj.login(mail_user,mail_pass) 
        #发送
        smtpObj.sendmail(
            sender,receivers,message.as_string()) 
        #退出
        smtpObj.quit() 
        print('send success')
    except smtplib.SMTPException as e:
        print('error',e) #打印错误`
def cspath():
    print(os.getcwd(),'F:\\sh\\csspy'==os.getcwd())
if __name__=="__main__":
    # cspath()
    mysend()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抱枕无忧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值