{"id":1188,"date":"2021-05-15T14:22:48","date_gmt":"2021-05-15T14:22:48","guid":{"rendered":"http:\/\/blog.successionua.com\/?p=1188"},"modified":"2022-07-18T17:02:12","modified_gmt":"2022-07-18T14:02:12","slug":"python-%d0%ba%d0%b0%d0%ba-%d0%bf%d1%80%d0%b8%d0%bb%d0%be%d0%b6%d0%b8%d1%82%d1%8c-xlsx-%d1%84%d0%b0%d0%b9%d0%bb-%d0%ba-%d0%bf%d0%b8%d1%81%d1%8c%d0%bc%d1%83-e-mail","status":"publish","type":"post","link":"https:\/\/geckich.space\/?p=1188","title":{"rendered":"Python, \u043a\u0430\u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0438\u0442\u044c .xlsx \u0444\u0430\u0439\u043b \u043a \u043f\u0438\u0441\u044c\u043c\u0443 e-mail."},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0442\u0430\u043a\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0438\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0444\u0430\u0439\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u043e\u0442\u0441\u044b\u043b\u0430\u044e\u0449\u0435\u0433\u043e .xlsx \u0444\u0430\u0439\u043b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import smtplib\nfrom email.utils import formatdate\nfrom email.mime.multipart import MIMEMultipart\nfrom email.mime.text import MIMEText\nfrom email.mime.base import MIMEBase\nfrom email import encoders\n\ndst_emails = 'recipient1@example.com, recipient2@example.com'\nxlsxfile = '\/home\/user\/project1\/example.xlsx'\n\n# E-mail block\nme = \"sender@example.com\"\n# Filling e-mail info\nmsg = MIMEMultipart()\nmsg['Subject'] = \"Xlsx file\"\nmsg['From'] = me\nmsg['To'] = dst_emails\nto = msg['To'].split()\nmsg['Date'] = formatdate(localtime=True)\nhtml = \"\"\"\\\n        &lt;html&gt;\n          &lt;head&gt;&lt;\/head&gt;\n          &lt;body&gt;\n            &lt;p&gt;&lt;br&gt;\n                &lt;strong&gt;This is a test e-mail&lt;\/strong&gt;&lt;br&gt;\n                &lt;br&gt;\n                Place body text here\n\n                Best regards,&lt;br&gt;\n                Example user&lt;br&gt;\n            &lt;\/p&gt;\n          &lt;\/body&gt;\n        &lt;\/html&gt;\n        \"\"\"\n# Record the MIME type\nletter = MIMEText(html, 'html')\n# Attach letter into message container.\nmsg.attach(letter)\n# Attach xlsx file\npart = MIMEBase('application', \"octet-stream\")\npart.set_payload(open(xlsxfile, \"rb\").read())\nencoders.encode_base64(part)\npart.add_header('Content-Disposition', 'attachment; filename=\"%s\"'%(xlsxfile))\nmsg.attach(part)\n# Send the message via example.com SMTP server.\ns = smtplib.SMTP('mail.example.com')\n# sendmail function takes 3 arguments: sender's address, recipient's address\n# and message to send - here it is sent as one string.\ns.sendmail(me, to, msg.as_string())\ns.quit()\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u0441\u044b\u043b\u043a\u0438:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>https:\/\/stackoverflow.com\/questions\/25346001\/add-excel-file-attachment-when-sending-python-email<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0442\u0430\u043a\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0438\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0444\u0430\u0439\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043a\u0440\u0438\u043f\u0442\u0430, \u043e\u0442\u0441\u044b\u043b\u0430\u044e\u0449\u0435\u0433\u043e .xlsx \u0444\u0430\u0439\u043b: import smtplib from email.utils import formatdate from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders dst_emails = &#8216;recipient1@example.com, recipient2@example.com&#8217; xlsxfile = &#8216;\/home\/user\/project1\/example.xlsx&#8217; # E-mail block me = &#8220;sender@example.com&#8221; &hellip; <a href=\"https:\/\/geckich.space\/?p=1188\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python, \u043a\u0430\u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0438\u0442\u044c .xlsx \u0444\u0430\u0439\u043b \u043a \u043f\u0438\u0441\u044c\u043c\u0443 e-mail.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[164,156,48,116,165],"class_list":["post-1188","post","type-post","status-publish","format-standard","hentry","category-programming","tag-e-mail","tag-excel","tag-linux","tag-python","tag-xlsx"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts\/1188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1188"}],"version-history":[{"count":5,"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1300,"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts\/1188\/revisions\/1300"}],"wp:attachment":[{"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}