{"id":730,"date":"2020-05-14T15:24:28","date_gmt":"2020-05-14T07:24:28","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=730"},"modified":"2020-05-14T15:28:49","modified_gmt":"2020-05-14T07:28:49","slug":"fastapi-mailbox-alarm-interface","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2020\/05\/fastapi-mailbox-alarm-interface\/","title":{"rendered":"FastApi \u90ae\u7bb1\u544a\u8b66\u63a5\u53e3"},"content":{"rendered":"\n
# pip3.7 install fastapi uvicorn \u5b89\u88c5 \n# uvicorn sendmail:app --reload --port 8888 --host 0.0.0.0 \u542f\u52a8<\/pre>\n\n\n\n#vim sendmail.py\n\n# -*- coding: UTF-8 -*-\nimport smtplib\nfrom email.mime.text import MIMEText\nfrom email.header import Header\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel\n\nmail_host = \"*******\" # \u8bbe\u7f6e\u670d\u52a1\u5668\nmail_user = \"********\" # \u7528\u6237\u540d\nmail_pass = \"********\" # \u53e3\u4ee4\n\nclass Item(BaseModel):\nReceives: list\nContent: str\ncc: list = None\n\napp = FastAPI()\n@app.post(\"\/sendmail\/\")\ndef send_mail(item: Item):\nmsg = MIMEText(item.Content)\nmsg['Subject'] = Header(\"\u533a\u5757\u94fe\u7cfb\u7edfv1.0\u4e0a\u7ebf\u901a\u77e5\",\"utf-8\")\nmsg['From'] = (\"%s<admin@*****.com>\") % (Header(\"\u96c6\u56e2\u533a\u5757\u94fe\u9879\u76ee\u90e8\",\"utf-8\"),)\nmsg['To'] = \",\".join(item.Receives)\ntry:\nserver = smtplib.SMTP()\nserver.connect(mail_host)\nserver.login(mail_user, mail_pass)s\nserver.sendmail(msg['From'], item.Receives, msg.as_string())\nserver.close()\nreturn True\nexcept Exception as e:\nprint(str(e))\nreturn str(e)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"# pip3.7 install fastapi uvicorn \u5b89\u88c5 # uvicorn sendmail:<\/p>\n","protected":false},"author":1,"featured_media":735,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n
FastApi \u90ae\u7bb1\u544a\u8b66\u63a5\u53e3 - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n