当fastapi_mail发送邮件的时候,想对附件的名称进行重命名
以下是fastapi_mail下的schemas.py的源码
import os
from enum import Enum
from mimetypes import MimeTypes
from typing import Dict, List, Optional, Unionfrom pydantic import BaseModel, EmailStr, validator
from starlette.datastructures import UploadFilefrom fastapi_mail.errors import WrongFileclass MultipartSubtypeEnum(Enum):"""for more info about Multipart subtypes visit:https://en.wikipedia.org/wiki/MIME#Multipart_subtypes"""mixed = 'mixed'digest = 'digest'alternative = 'alternative'related = 'related'report = 'report'signed = 'signed'encrypted = 'encrypted'form_data = 'form-data'mixed_replace = 'x-mixed-replace'byterange = 'byterange'class MessageSchema(BaseModel):recipients: List[EmailStr]attachments: List[Union[UploadFile, Dict, str, Tuple]] = []subject: str = ''body: Optional[Union[str, list]] = Nonetemplate_body: Optional[Union[list, dict]] = Nonehtml: Optional[Union[str, List, Dict]] = Nonecc: List[EmailStr] = []bcc: List[EmailStr] = []reply_to: List[EmailStr] = []charset: str = 'utf-8'subtype: Optional[str] = Nonemultipart_subtype: MultipartSubtypeEnum = MultipartSubtypeEnum.mixedheaders: Optional[Dict] = None@validator('attachments')def validate_file(cls, v):temp = []mime = MimeTypes()for file in v:file_meta = Noneif isinstance(file, dict):keys = file.keys()if 'file' not in keys:raise WrongFile('missing "file" key')file_meta = dict.copy(file)del file_meta['file']file = file['file']if isinstance(file, str):if os.path.isfile(file) and os.access(file, os.R_OK) and validate_path(file):mime_type = mime.guess_type(file)f = open(file, mode='rb')_, file_name = os.path.split(f.name)u = UploadFile(file_name, f, content_type=mime_type[0])temp.append((u, file_meta))else:raise WrongFile('incorrect file path for attachment or not readable')elif isinstance(file, UploadFile):temp.append((file, file_meta))else:raise WrongFile('attachments field type incorrect, must be UploadFile or path')return temp@validator('subtype')def validate_subtype(cls, value, values, config, field):"""Validate subtype field."""if values['template_body']:return 'html'return valueclass Config:arbitrary_types_allowed = Truedef validate_path(path):cur_dir = os.path.abspath(os.curdir)requested_path = os.path.abspath(os.path.relpath(path, start=cur_dir))common_prefix = os.path.commonprefix([requested_path, cur_dir])return common_prefix == cur_dir
需要修改源码,让函数调用的时候传入修改后的文件名
import os
from enum import Enum
from mimetypes import MimeTypes
from typing import Dict, List, Optional, Union, Tuplefrom pydantic import BaseModel, EmailStr, validator
from starlette.datastructures import UploadFilefrom fastapi_mail.errors import WrongFileclass MultipartSubtypeEnum(Enum):"""for more info about Multipart subtypes visit:https://en.wikipedia.org/wiki/MIME#Multipart_subtypes"""mixed = 'mixed'digest = 'digest'alternative = 'alternative'related = 'related'report = 'report'signed = 'signed'encrypted = 'encrypted'form_data = 'form-data'mixed_replace = 'x-mixed-replace'byterange = 'byterange'class MessageSchema(BaseModel):recipients: List[EmailStr]attachments: List[Union[UploadFile, Dict, str, Tuple]] = []subject: str = ''body: Optional[Union[str, list]] = Nonetemplate_body: Optional[Union[list, dict]] = Nonehtml: Optional[Union[str, List, Dict]] = Nonecc: List[EmailStr] = []bcc: List[EmailStr] = []reply_to: List[EmailStr] = []charset: str = 'utf-8'subtype: Optional[str] = Nonemultipart_subtype: MultipartSubtypeEnum = MultipartSubtypeEnum.mixedheaders: Optional[Dict] = None@validator('attachments')def validate_file(cls, v):temp = []mime = MimeTypes()for file in v:file_meta = Noneif isinstance(file, dict):keys = file.keys()if 'file' not in keys:raise WrongFile('missing "file" key')file_meta = dict.copy(file)del file_meta['file']file = file['file']if isinstance(file, str):if os.path.isfile(file) and os.access(file, os.R_OK) and validate_path(file):mime_type = mime.guess_type(file)f = open(file, mode='rb')_, file_name = os.path.split(f.name)u = UploadFile(file_name, f, content_type=mime_type[0])temp.append((u, file_meta))else:raise WrongFile('incorrect file path for attachment or not readable')if isinstance(file, tuple):file, file_name = fileif os.path.isfile(file) and os.access(file, os.R_OK) and validate_path(file):mime_type = mime.guess_type(file)f = open(file, mode='rb')_, file_name = os.path.split(file_name)u = UploadFile(file_name, f, content_type=mime_type[0])temp.append((u, file_meta))else:raise WrongFile('incorrect file path for attachment or not readable')elif isinstance(file, UploadFile):temp.append((file, file_meta))else:raise WrongFile('attachments field type incorrect, must be UploadFile or path')return temp@validator('subtype')def validate_subtype(cls, value, values, config, field):"""Validate subtype field."""if values['template_body']:return 'html'return valueclass Config:arbitrary_types_allowed = Truedef validate_path(path):cur_dir = os.path.abspath(os.curdir)requested_path = os.path.abspath(os.path.relpath(path, start=cur_dir))common_prefix = os.path.commonprefix([requested_path, cur_dir])return common_prefix == cur_dir