58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
![]() |
import json
|
||
|
from tencentcloud.common import credential
|
||
|
from tencentcloud.common.profile.client_profile import ClientProfile
|
||
|
from tencentcloud.common.profile.http_profile import HttpProfile
|
||
|
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
|
||
|
from tencentcloud.ses.v20201002 import ses_client, models
|
||
|
|
||
|
SECRET_ID = "AKIDjNrBSnBXnAQXg6ythOADLJ2nOuLpFaoo"
|
||
|
MAIL_SECRET_KEY = "ENPZnwADD623HUm15EITdvDhAa8nQxLk"
|
||
|
EMAIL_HOST_USER = 'admin@typeframes.cc'
|
||
|
TO_EMAIL = '1726850085@qq.com'
|
||
|
SUBJECT = '您的验证码'
|
||
|
TEMPLATE_ID = 125447 # 替换为实际的模板 ID
|
||
|
|
||
|
def send_email_with_template(secret_id, secret_key, from_email, to_email, subject, template_id, code):
|
||
|
try:
|
||
|
# 实例化一个认证对象
|
||
|
cred = credential.Credential(secret_id, secret_key)
|
||
|
httpProfile = HttpProfile()
|
||
|
httpProfile.endpoint = "ses.tencentcloudapi.com"
|
||
|
clientProfile = ClientProfile()
|
||
|
clientProfile.httpProfile = httpProfile
|
||
|
client = ses_client.SesClient(cred, "ap-hongkong", clientProfile)
|
||
|
|
||
|
# 实例化一个请求对象
|
||
|
req = models.SendEmailRequest()
|
||
|
params = {
|
||
|
"FromEmailAddress": from_email,
|
||
|
"Destination": [to_email],
|
||
|
"Subject": subject,
|
||
|
"ReplyToAddresses": from_email,
|
||
|
"Template": {
|
||
|
"TemplateID": template_id,
|
||
|
"TemplateData": json.dumps({"name": from_email, "code": code})
|
||
|
}
|
||
|
}
|
||
|
req.from_json_string(json.dumps(params))
|
||
|
|
||
|
# 发送请求并返回响应
|
||
|
resp = client.SendEmail(req)
|
||
|
return resp.to_json_string()
|
||
|
|
||
|
except TencentCloudSDKException as err:
|
||
|
return str(err)
|
||
|
|
||
|
def generate_verification_code(length=6):
|
||
|
import random
|
||
|
import string
|
||
|
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
||
|
|
||
|
def main():
|
||
|
code = generate_verification_code()
|
||
|
response = send_email_with_template(SECRET_ID, MAIL_SECRET_KEY, EMAIL_HOST_USER, TO_EMAIL, SUBJECT, TEMPLATE_ID, code)
|
||
|
print(response)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|