{"id":1464,"date":"2020-12-27T12:08:46","date_gmt":"2020-12-27T04:08:46","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1464"},"modified":"2020-12-27T12:08:46","modified_gmt":"2020-12-27T04:08:46","slug":"python-sent-emails-with-embedded-images-%e5%8f%91%e9%82%ae%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=1464","title":{"rendered":"Python: sent emails with embedded images \u53d1\u90ae\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\nto send emails with images you need to use MIMEMultipart, but the basic approach:\r\r\nimport smtplib\r\n\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.mime.image import MIMEImage\r\n\r\nmsg = MIMEMultipart('alternative')\r\nmsg&#91;'Subject'] = \"subject\"\r\nmsg&#91;'From'] = from_addr\r\nmsg&#91;'To'] = to_addr\r\n\r\npart = MIMEImage(open('\/path\/to\/image', 'rb').read())\r\n\r\ns = smtplib.SMTP('localhost')\r\ns.sendmail(from_addr, to_addr, msg.as_string())\r\ns.quit()\r\n\r\nwill produce an email with empty body and the image as an attachment.\r\n\r\nThe better way, ie to have the image as part of the body of the email, requires to write an HTML body that refers to that image:\r\n\r\nimport smtplib\r\n\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.mime.text import MIMEText\r\nfrom email.mime.image import MIMEImage\r\n\r\nmsg = MIMEMultipart('alternative')\r\nmsg&#91;'Subject'] = \"subject\r\nmsg&#91;'From'] = from_addr\r\nmsg&#91;'To'] = to_addr\r\n\r\ntext = MIMEText('&lt;img src=\"cid:image1\">', 'html')\r\nmsg.attach(text)\r\n\r\nimage = MIMEImage(open('\/path\/to\/image', 'rb').read())\r\n\r\n# Define the image's ID as referenced in the HTML body above\r\nimage.add_header('Content-ID', '&lt;image1>')\r\nmsg.attach(image)\r\n\r\ns = smtplib.SMTP('localhost')\r\ns.sendmail(from_addr, to_addr, msg.as_string())\r\ns.quit()\r\n\r\nThe trick is to define an image with a specific Content-ID and make that the only item in an HTML body: now you have an email with contains that specific image as the only content of the body, embedded in it.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1464","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1464"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1465,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1464\/revisions\/1465"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}