{"id":949,"date":"2024-08-07T17:06:36","date_gmt":"2024-08-07T09:06:36","guid":{"rendered":"http:\/\/www.madbull.site\/?p=949"},"modified":"2024-08-30T00:14:14","modified_gmt":"2024-08-29T16:14:14","slug":"%e7%94%a8python%e7%94%9f%e6%88%90%e9%82%ae%e4%bb%b6%e6%96%87%e4%bb%b6-eml%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=949","title":{"rendered":"\u7528python\u751f\u6210\u90ae\u4ef6\u6587\u4ef6&#8211;eml\u6587\u4ef6"},"content":{"rendered":"\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"has-large-font-size\"><strong>\u521b\u5efatceml.py\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n#!\/usr\/bin\/env python3.9\n\n\nfrom email.mime.multipart import MIMEMultipart\nfrom email.mime.text import MIMEText\nfrom email.mime.application import MIMEApplication\n\nimport optparse\nimport sys\n\ndef create_eml(text_content = \"this is a test mail !\", url_lst = None, athm_lst = None, **headers) :\n\n    # 1\u3001\u521b\u5efa\u4e00\u4e2a\u6df7\u5408\u578b\u7684 MIME\u591a\u90e8\u5206\u6570\u636e\n    eml_mixed = MIMEMultipart('mixed')\n    # 1.1\u3001header\u5904\u7406\n    for key, value in headers.items() :\n        eml_mixed&#91;key] = value\n\n    # 2\u3001\u521b\u5efa\u4e00\u4e2a\u53ef\u66ff\u6362\u7684 MIME\u591a\u90e8\u5206\u6570\u636e\n    eml_cnt = MIMEMultipart('alternative')\n\n    # 2.1\u3001\u6587\u672c\u6570\u636e\u5904\u7406\n    part1 = MIMEText(text_content, _subtype='plain', _charset=\"utf-8\")\n    eml_cnt.attach(part1)\n\n    # 2.2 \u521b\u5efa\u4e00\u4e2ahtml\u683c\u5f0f\u7684\u5185\u5bb9\u7ed9 eml_cnt\uff0c\u6709URL,\u6253\u4e0aURL\u94fe\u63a5\n    if url_lst is not None and len(url_lst) &gt;= 1 :\n        i = 0 ;\n        html_content=\"&lt;html&gt;&lt;body&gt;&lt;p&gt;URL\u6d4b\u8bd5\u90ae\u4ef6&lt;\/p&gt;\"\n        for url in url_lst :\n            i+=1\n            a_tag = \"&lt;p&gt;&lt;a href=\\\"\"+ url +\"\\\"&gt;\u6d4b\u8bd5\u94fe\u63a5\"+ str(i) +\"&lt;\/a&gt;&lt;\/p&gt;\"\n            html_content += a_tag\n        html_content += \"&lt;\/body&gt;&lt;\/html&gt;\"\n        part2 = MIMEText(html_content, _subtype='html', _charset=\"utf-8\")\n        eml_cnt.attach(part2)\n\n    eml_mixed.attach(eml_cnt)\n\n    # 3\u3001\u9010\u4e2a\u6dfb\u52a0\u9644\u4ef6\n    for athm in athm_lst :\n        with open(athm, \"rb\") as fd :\n            # 3.1\u3001\u6dfb\u52a0\u9644\u4ef6\u6570\u636e\n            athm_part = MIMEApplication(fd.read(), _subtype=\"plain\")\n            # 3.2\u3001\u6dfb\u52a0\u9644\u4ef6\u6570\u636e\u7684\u5934\u90e8\u8bf4\u660e\n            filename = athm.split(\"\/\")&#91;-1]\n            athm_part.add_header('content-disposition', 'attachment', filename=filename )\n            # 3\u3001\u9010\u4e2a\u6dfb\u52a0\u5230 eml_mixed \u4e2d\n            eml_mixed.attach(athm_part)\n\n    # \u8f6c\u6362\u6210string\u8fd4\u56de\n    return eml_mixed.as_string()\n\n\n# input : aaaa@aaa.com,bbbb@bbb.com,ccc@ccc.com\n# output : aaaa&lt;aaaa@aaa.com&gt;,bbbb&lt;bbbb@bbb.com&gt;,ccc&lt;ccc@ccc.com&gt;\n\ndef mail_account_create(account_str) :\n    if account_str == None or len(account_str) == 0 :\n        return None\n    dst_lst = &#91;]\n    acc_lst = account_str.split(\",\")\n    for account in acc_lst :\n        nick_name = account.split(\"@\")&#91;0]\n        dst_lst.append(nick_name+\"&lt;\"+account+\"&gt;\")\n    return \",\".join(dst_lst)\n\n\nif __name__ == \"__main__\":\n    argsParser = optparse.OptionParser( usage='Usage: createEml.py &#91;options] -t type -f file -i project-id', prog='PROG', conflict_handler='resolve' )\n    argsParser.add_option( '', '--help', action='store_true', dest=\"help\", default=False, help='dispaly help usage' ) \n    argsParser.add_option( '-t', '--type', dest=\"type\", default=-1, help='type : athm=1 url=2 text=3' ) \n    argsParser.add_option( '-e', '--element', action='append', dest=\"ele\", default=None, help=\"the data element\" )\n    argsParser.add_option( '-o', '--output', dest=\"output\", default=None, help=\"the eml write to the file\" )\n    argsParser.add_option( '', '--from', dest=\"from_\", default=\"from@from.com\", help=\"the eml file 'from' data\" )\n    argsParser.add_option( '', '--sender', dest=\"sender\", default=\"sender@sender.com\", help=\"the eml file 'sender' data\" )\n    argsParser.add_option( '', '--subject', dest=\"subject\", default=\"\u8fd9\u662f\u6d4b\u8bd5\u90ae\u4ef6\u4e3b\u9898\", help=\"the eml file 'subject' data\" )\n    argsParser.add_option( '', '--to', dest=\"to\", default=\"to@to.com\", help=\"the eml file 'to' data\" )\n    argsParser.add_option( '', '--cc', dest=\"cc\", default=\"cc@cc.com\", help=\"the eml file 'cc' data\" )\n\n    (options, args) = argsParser.parse_args(list(sys.argv)) \n\n    if options.help == True or options.type == -1 or options.ele == None or options.output == None :\n        argsParser.print_help()\n        sys.exit()\n\n    athm_lst = &#91;]\n    url_lst = &#91;]\n    text_content = \"this is a test mail !!!\"\n\n    from_ = mail_account_create(options.from_)\n    sender = mail_account_create(options.sender)\n    to = mail_account_create(options.to)\n    cc = mail_account_create(options.cc)\n\n\n    if options.type == '1' :\n        athm_lst = options.ele\n    elif options.type == '2' :\n        url_lst = options.ele\n    elif options.type == '3' :\n        text_content = '\\n'.join(options.ele)\n\n    data = create_eml( text_content, url_lst, athm_lst, Subject=options.subject, From=from_, Sender=sender, To=to, Cc=cc, X_ddd=\"\u81ea\u5b9a\u4e49\u81ea\u5b9a\u4e49\" )\n    with open(options.output, 'w') as fd:\n        fd.write(data)\n\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6267\u884c\u811a\u672c\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>python3.9 crteml.py -t 1 -e \/etc\/os-release -e \/etc\/services -o 123.eml &#8211;from ff@ff.com &#8211;to to@to.com,tt@tt.com &#8211;sender ss@ss.com,sd@sd.com &#8211;subject &#8220;ssssssssssss&#8221;<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u751f\u6210\u4e86 123.eml \u6587\u4ef6\uff0c\u53ef\u4ee5\u7528 foxmail \u6216\u8005\u5176\u4ed6\u67e5\u770b\u90ae\u4ef6\u7684\u5de5\u5177\u6253\u5f00\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:21px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"349\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-8.png\" alt=\"\" class=\"wp-image-1013\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-8.png 943w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-8-300x111.png 300w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/11-8-768x284.png 768w\" sizes=\"auto, (max-width: 943px) 100vw, 943px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"871\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-4.png\" alt=\"\" class=\"wp-image-1014\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-4.png 621w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-4-214x300.png 214w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7528python\u7f16\u5199\u4e00\u4e2a\u811a\u672c\uff0c\u751f\u6210\u90ae\u4ef6\u6587\u4ef6&#8211;eml\u6587\u4ef6\u3002\u521b\u5efatceml.py\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a  # 1\u3001\u521b\u5efa\u4e00\u4e2a\u6df7\u5408\u578b\u7684 MIME\u591a\u90e8\u5206\u6570\u636e # 2\u3001\u521b\u5efa\u4e00\u4e2a\u53ef\u66ff\u6362\u7684 MIME\u591a\u90e8\u5206\u6570\u636e # 3\u3001\u9010\u4e2a\u6dfb\u52a0\u9644\u4ef6 # \u8f6c\u6362\u6210string\u8fd4\u56de<\/p>\n","protected":false},"author":1,"featured_media":548,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[155,154],"tags":[380,383,385,384,142,328,382,381],"class_list":["post-949","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-154","tag-eml","tag-mime","tag-mimeapplication","tag-mimemultipart","tag-python","tag-328","tag-382","tag-381"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=949"}],"version-history":[{"count":5,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/949\/revisions"}],"predecessor-version":[{"id":1051,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/949\/revisions\/1051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/548"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}