忘れてしまわないようにメモ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ https://stackoverflow.com/questions/39457380/retrieve-email-subject-from-file-via-bash https://stackoverflow.com/questions/4067937/getting-mail-attachment-to-python-file-object https://stackoverflow.com/questions/17874360/python-how-to-parse-the-body-from-a-raw-email-given-that-raw-email-does-not """ import time, os, base64, re, html from sys import argv import email from email.parser import Parser from email.header import Header, decode_header # from django.utils.html import strip_tags scriptPath = os.path.dirname(os.path.abspath(__file__)) # get header and decode def decodeHeader(key, message): lines = decode_header(message[key]) # print(lines) s = "" for line in lines: if line[1]: if isinstance(line[0], str): s += line[0] else: s += line[0].decode(line[1], errors="ignore") else: if isinstance(line[0], str): s += line[0] else: s += line[0].decode(errors="ignore") return s for filename in argv[1:]: # print("filename: %s" % filename) with open(filename, mode="rt", errors="ignore") as handle: # handle file not found etc? message = Parser().parse(handle) # print(message) subject = decodeHeader("subject", message) fromAddress = decodeHeader("From", message) # print(fromAddress) fromEmailAddress = email.utils.parseaddr(fromAddress)[1] dateString = message.get("Date") # "Mon, 16 Nov 2009 13:32:02 +0100" # print(dateString) dateObject = email.utils.parsedate(dateString) # print(dateObject) body = "" if message.is_multipart(): # print("* Multipart") i = 0 for payload in message.get_payload(decode=False): contentType = payload.get_content_type() # print("content type: %s" % contentType) contentTransferEncoding = payload["Content-Transfer-Encoding"] # print("Content-Transfer-Encoding: %s" % contentTransferEncoding) charset = payload.get_content_charset() # print("charset = %s" % charset) c = contentType.split("/") if c[0] == "text": attachment = payload.get_payload(decode=True) if charset == None: body += attachment.decode(errors="ignore") + "\n\n" elif contentTransferEncoding == "8bit": body += attachment.decode(charset, errors="ignore").encode("utf-8").decode("unicode-escape") + "\n\n" else: body += attachment.decode(charset, errors="ignore") + "\n\n" """ if c[1] == "html": body = html.unescape(body) # decode html entity "�" body = strip_tags(body) """ elif c[0] == "image": continue filename = payload.get_filename() # print("filename: %s" % filename) i += 1 open("%s/attachment/%s-%02d(%s).%s" % (scriptPath, time.strftime("%Y%m%d_%H%M%S", dateObject), i, filename, c[1]), "wb").write(payload.get_payload(decode=True)) else: continue else: # print("* Single part") charset = message.get_content_charset() # print("charset = %s" % charset) contentTransferEncoding = message["Content-Transfer-Encoding"] # print("Content-Transfer-Encoding: %s" % contentTransferEncoding) attachment = message.get_payload(decode=True) if charset == None: body += attachment + "\n\n" elif contentTransferEncoding == "8bit": body += attachment.decode(charset, errors="ignore").encode("utf-8").decode("unicode-escape") + "\n\n" else: body += attachment.decode(charset, errors="ignore") + "\n\n" body = html.unescape(body) # decode html entity "�" """ print("-------------------") if dateObject: print(time.strftime("%Y-%m-%d %H:%M:%S", dateObject)) print("From: %s" % fromEmailAddress) print("Subject: %s" % (subject)) print(body) # exit(0) """ # https://qiita.com/yasunori/items/265d8db746742bb967c4 import smtplib from email.mime.text import MIMEText from email.header import Header from email import charset con = smtplib.SMTP("localhost") # con.set_debuglevel(True) con.set_debuglevel(False) cset = "utf-8" fromAddress = "user@example.com" toAddress = "user@example.com" message = MIMEText(body, "plain", cset) message["Subject"] = Header(subject, cset) message["From"] = fromAddress message["To"] = toAddress message["Reply-To"] = fromEmailAddress con.sendmail(fromAddress, [toAddress], message.as_string()) con.close() |