import json
import urllib
import requests
class Email:
apiUser = "* *"
apiKey = "* *"
send_from = " * * "
def send_email(self, send_to, subject, templateInvokeName):
xsmtpapi = {
'to': “ * * ”,
'sub': {
'%content%': ‘ * * ’
}
}
data = {
"apiUser": self.apiUser,
"apiKey": self.apiKey,
"from": self.send_from,
"subject": subject,
"templateInvokeName":templateInvokeName,
"xsmtpapi": json.dumps(xsmtpapi),
}
filename = r'C:\Users\Administrator\Desktop\a.png'
display_filename = "filename.png"
files = {"attachments": (urllib.parse.quote(display_filename), open(filename, "rb"))}
url = "https://api.sendcloud.net/apiv2/mail/sendtemplate"
try:
response = requests.post(url, data=data,files=files)
return (1, response.text)
except Exception as e:
print(e)
return (0, "邮件发送失败")
if __name__ == '__main__':
send_to = ‘ * * ’
subject = '测试'
templateInvokeName = 'world2'
Email().send_email(send_to, subject, templateInvokeName)