1.处理邮箱附件上传,发送邮件附件
This commit is contained in:
parent
b9a728f394
commit
41219df9e6
@ -22,7 +22,7 @@ from schemas import ExtendendOAuth
|
||||
from utils.dingding import *
|
||||
from utils.jianli import get_resume
|
||||
from utils.re_to_jianli import fmt_txt, getText_pdf
|
||||
from utils.func import get_uid, send_affix_mail, location_file
|
||||
from utils.func import get_uid, send_affix_mail
|
||||
import crud, schemas
|
||||
from datetime import datetime
|
||||
from core.configuration import *
|
||||
@ -1415,6 +1415,49 @@ async def resume_affix(
|
||||
return schemas.Msg(code=400, msg='上传华为云失败', data='')
|
||||
|
||||
|
||||
@router.post("/mail_affix")
|
||||
async def mail_affix(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
ckdb: CKDrive = Depends(get_ck_db),
|
||||
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
||||
) -> schemas.Msg:
|
||||
"""邮箱附件上传到华为云"""
|
||||
path_data = os.getcwd() + '/mail_affix' # 当前文件所在的目录
|
||||
if not os.path.exists(path_data):
|
||||
os.makedirs(path_data)
|
||||
contents = await file.read()
|
||||
filename = file.filename
|
||||
try:
|
||||
with open(path_data + '/' + filename, "wb") as f:
|
||||
# 将获取的file文件内容,写入到新文件中
|
||||
f.write(contents)
|
||||
f.close()
|
||||
except:
|
||||
return schemas.Msg(code=400, msg='上传文件有误', data='')
|
||||
try:
|
||||
# 附件正常上传到华为云
|
||||
res = obsClient.putFile('legu-cdn-source', 'hrms/mail_affix/' + filename, path_data + '/' + filename)
|
||||
if res.status < 300:
|
||||
# 地址
|
||||
url = res.body.objectUrl
|
||||
resData = {'file_url': url, 'filename': 'hrms/mail_affix/' + filename}
|
||||
res = json.dumps(resData)
|
||||
# sql = f"""
|
||||
# ALTER table HR.resumes update resume_affix_id = {url} WHERE uid = '{data_in.uid}'
|
||||
# """
|
||||
# await ckdb.execute(sql)
|
||||
return schemas.Msg(code=200, msg='ok', data=res)
|
||||
else:
|
||||
# print('errorCode:', res.errorCode)
|
||||
# print('errorMessage:', res.errorMessage)
|
||||
return schemas.Msg(code=400, msg='上传华为云失败', data='')
|
||||
except:
|
||||
import traceback
|
||||
print(traceback.format_exc())
|
||||
return schemas.Msg(code=400, msg='上传华为云失败', data='')
|
||||
|
||||
|
||||
@router.post("/updata_entry")
|
||||
async def updata_entry(
|
||||
request: Request,
|
||||
@ -1459,44 +1502,33 @@ async def get_str_mail(
|
||||
@router.post("/send_str_mail")
|
||||
async def send_mail(
|
||||
request: Request,
|
||||
#data_in: schemas.send_str_mail,
|
||||
user_id: str =Form(...), # 接收者的唯一id
|
||||
name: str =Form(...), # 接收者的名字
|
||||
email: str=Form(...), # 接收者的邮箱
|
||||
email_str: str =Form(...), # 需要发送的文本内容
|
||||
file:Union[List[UploadFile], None] = File(None),
|
||||
data_in: schemas.send_str_mail,
|
||||
#file: List[UploadFile] = File(...),
|
||||
db: AsyncIOMotorDatabase = Depends(get_database),
|
||||
current_user: schemas.UserDB = Depends(deps.get_current_user)
|
||||
) -> schemas.Msg:
|
||||
"""发送邮件"""
|
||||
path_data = os.getcwd() + '/mail_affix' # 当前文件所在的目录
|
||||
if not os.path.exists(path_data):
|
||||
os.makedirs(path_data)
|
||||
filename = data_in.file.split('/')[-1]
|
||||
try:
|
||||
if file == None:
|
||||
# 发送邮件
|
||||
send_affix_mail(email_str, [email])
|
||||
else:
|
||||
#带附件的
|
||||
file_list=[]
|
||||
for i in file:
|
||||
contents = await i.read()
|
||||
filename = i.filename
|
||||
file_list.append(filename)
|
||||
with open(location_file + '/' + filename, "wb") as f:
|
||||
# 将获取的file文件内容,写入到新文件中
|
||||
f.write(contents)
|
||||
send_affix_mail(email_str, [email],file_list)
|
||||
#send_str_mail(data_in.email_str, data_in.email)
|
||||
res = obsClient.getObject('legu-cdn-source', data_in.file, path_data + '/' + filename)
|
||||
if res.status < 300:
|
||||
send_affix_mail(data_in.email_str, [data_in.email], path_data + '/' + filename)
|
||||
# send_str_mail(data_in.email_str, data_in.email)
|
||||
# 保存发送邮件的记录
|
||||
now_time = str(datetime.now()).split('.')[0]
|
||||
await crud.email_record.create(db, schemas.email_record(user_id=user_id, text=email_str,
|
||||
await crud.email_record.create(db, schemas.email_record(user_id=data_in.user_id, text=data_in.email_str,
|
||||
times=now_time, name=name, state=1,
|
||||
read_status=0, type='email'))
|
||||
read_status=0, type='email',mail_affix=data_in.file))
|
||||
return schemas.Msg(code=200, msg='邮件发送成功', data='')
|
||||
except Exception as e:
|
||||
print(e)
|
||||
now_time = str(datetime.now()).split('.')[0]
|
||||
await crud.email_record.create(db, schemas.email_record(user_id=user_id, text=email_str,
|
||||
await crud.email_record.create(db, schemas.email_record(user_id=data_in.user_id, text=data_in.email_str,
|
||||
times=now_time, name=name, state=0,
|
||||
read_status=0, type='email'))
|
||||
read_status=0, type='email',mail_affix=data_in.file))
|
||||
return schemas.Msg(code=200, msg='邮件发送失败', data='')
|
||||
|
||||
|
||||
|
@ -11,6 +11,7 @@ class email_record(BaseModel):
|
||||
state: int # 邮件是否发送成功
|
||||
read_status: int # 是否已读
|
||||
type: str
|
||||
mail_affix: str # 邮件附件存储地
|
||||
|
||||
|
||||
class get_email_record(BaseModel):
|
||||
@ -25,3 +26,4 @@ class email_records(DBBase):
|
||||
state: int # 邮件是否发送成功
|
||||
read_status: int # 是否已读
|
||||
type: str # 消息类型
|
||||
mail_affix: str # 邮件附件存储地
|
||||
|
@ -32,7 +32,6 @@ class Post_uid(BaseModel):
|
||||
uid: str # 面试者简历在CK中的唯一标识
|
||||
|
||||
|
||||
|
||||
class get_uids(BaseModel):
|
||||
uid: str # 面试者简历在CK中的唯一标识
|
||||
|
||||
@ -58,6 +57,7 @@ class send_str_mail(BaseModel):
|
||||
name: str # 接收者的名字
|
||||
email: str # 接收者的邮箱
|
||||
email_str: str # 需要发送的文本内容
|
||||
file: str # 华为云的邮箱附件地址
|
||||
|
||||
|
||||
class user_id(BaseModel):
|
||||
@ -168,9 +168,6 @@ class nterview(BaseModel):
|
||||
# interview_round: int # 面试轮次
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class interviews(BaseModel):
|
||||
date: dict # 要查询的条件
|
||||
|
||||
|
@ -28,7 +28,7 @@ my_sender = '250213850@qq.com' # 发件人邮箱账号
|
||||
my_pass = 'whrsugtgkstibjdj' # 发件人邮箱密码
|
||||
subject = '入职通知' # 邮件的主题,也可以说是标题
|
||||
mail_host = 'smtp.qq.com'
|
||||
location_file = 'C:/Users/Administrator/Desktop/ddemo' # 默认读取转存的文件地址(上传至邮箱的文件)
|
||||
|
||||
def get_uid():
|
||||
return hex(int(time.time() * 10 ** 7) + random.randint(0, 10000))[2:]
|
||||
|
||||
@ -252,12 +252,12 @@ def send_str_mail(str_msg, my_user):
|
||||
server.quit() # 关闭连接
|
||||
|
||||
|
||||
def send_affix_mail(str_msg: str, send_to: list, file_name: list = None):
|
||||
def send_affix_mail(str_msg: str, send_to: list, file_name: str = None):
|
||||
"""
|
||||
发送邮件,包含文本或和附件
|
||||
:param str_msg: 发送的邮件内容
|
||||
:param send_to: 接收者的邮箱
|
||||
:param file_name: 上传的附件
|
||||
:param file_name: 上传的附件名,绝对路径
|
||||
"""
|
||||
msg = MIMEMultipart() # 创建一个带附件的实例
|
||||
msg["Subject"] = "面试通知"
|
||||
@ -268,10 +268,10 @@ def send_affix_mail(str_msg: str, send_to: list, file_name: list = None):
|
||||
msg.attach(part)
|
||||
# ---附件部分---
|
||||
if file_name != None:
|
||||
for i in file_name:
|
||||
part = MIMEApplication(open(location_file + '/' + i, 'rb').read())
|
||||
part.add_header('Content-Disposition', 'attachment', filename=i)
|
||||
msg.attach(part)
|
||||
part = MIMEApplication(open(file_name, 'rb').read())
|
||||
filename=file_name.split('/')[-1]
|
||||
part.add_header('Content-Disposition', 'attachment', filename=filename)
|
||||
msg.attach(part)
|
||||
smpt = smtplib.SMTP_SSL(mail_host, 465, 'utf-8')
|
||||
smpt.login(my_sender, my_pass)
|
||||
smpt.sendmail(my_sender, send_to, msg.as_string())
|
||||
|
Loading…
Reference in New Issue
Block a user