python調用sendcloud模板發送帶附件的郵件


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,
            # "to": send_to,
            "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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章