博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过 python 处理 email - Email via Python
阅读量:4991 次
发布时间:2019-06-12

本文共 9706 字,大约阅读时间需要 32 分钟。

Email via Python   1 MIME - Multipurpose Internet Mail Extensions  2 SMTP - Simple Message Transport Protocol  3   4 邮件的发送,  5     例子 - send email  6         import smtplib  7         from email.mime.text import MIMEText  8         from email.utils import formatdate, make_msgid  9         mess = 'hello there' 10         mesgobj = MIMEText(mess) 11         mesgobj['To'] = 'testrecevier@example.com' 12         mesgobj['From'] = 'testsender@example.com' 13         mesgobj['Subject'] = 'Greeting' 14         mesgobj['Date'] = formatdate(localtime=1)     # Date header, formatdate() 生成 email 专门日期格式 15         mesgobj['Message-ID'] = make_msgid()          # make_msgid() 方法生成唯一的 Message-ID, 16         print(mesgobj.as_string()) 17         print(mesgobj.as_bytes()) 18  19         s = smtplib.SMTP('SMTP-SERVER-ADDR') 20         s.send_message(mesgobj) 21         s.quit() 22  23 邮件的解析, 24     例子 - parsing email, 25     from email.parser import Parser 26     headers = Parser().parsestr(mesgobj.as_string())  # mesgobj 为上例中对象. 27     for h,v in headers.items(): 28         print("Header[%s] - " % h,v) 29     print('Emial content - ', mesgobj.get_payload())  # 打印邮件正文. 30  31 带有附件的邮件发送处理 - Packing MIME, 32     邮件的附件有 音频, 图像, 压缩文件等等, 通过 python doc 的上的例子看一下儿 33     对邮件的附件的处理方式, 34     例子 - send email with attachments, 35         * an example of how to send the entire contents of a directory as an email message 36  37         import os 38         import sys 39         import smtplib 40         # For guessing MIME type based on file name extension 41         import mimetypes 42         from optparse import OptionParser 43         from email import encoders 44         from email.message import Message 45         from email.mime.audio import MIMEAudio 46         from email.mime.base import MIMEBase 47         from email.mime.image import MIMEImage 48         from email.mime.multipart import MIMEMultipart 49         from email.mime.text import MIMEText 50  51         COMMASPACE = ', ' 52  53         def main(): 54             parser = OptionParser(usage='''\ 55         Send the contents of a directory as a MIME message. 56  57         Usage: %prog [options] 58  59         Unless the -o option is given, the email is sent by forwarding to your local 60         SMTP server, which then does the normal delivery process.  Your local machine 61         must be running an SMTP server. 62         ''') 63             parser.add_option('-d', '--directory', 64                               type='string', action='store', 65                               help='''Mail the contents of the specified directory, 66                               otherwise use the current directory.  Only the regular 67                               files in the directory are sent, and we don't recurse to 68                               subdirectories.''') 69             parser.add_option('-o', '--output', 70                               type='string', action='store', metavar='FILE', 71                               help='''Print the composed message to FILE instead of 72                               sending the message to the SMTP server.''') 73             parser.add_option('-s', '--sender', 74                               type='string', action='store', metavar='SENDER', 75                               help='The value of the From: header (required)') 76             parser.add_option('-r', '--recipient', 77                               type='string', action='append', metavar='RECIPIENT', 78                               default=[], dest='recipients', 79                               help='A To: header value (at least one required)') 80             opts, args = parser.parse_args() 81             if not opts.sender or not opts.recipients: 82                 parser.print_help()    # SMTP server 只处理地址有效的 sender 跟 recipients 83                 sys.exit(1) 84             directory = opts.directory 85             if not directory: 86                 directory = '.'        # 之前的代码的功能是通过 OptionParser 没款完成对命令行参数的解析 87             # Create the enclosing (outer) message 88             outer = MIMEMultipart()    # 构建一个带附件的 email 对象 89             outer['Subject'] = 'Contents of directory %s' % os.path.abspath(directory) 90             outer['To'] = COMMASPACE.join(opts.recipients) 91             outer['From'] = opts.sender 92             outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' 93  94             for filename in os.listdir(directory):    # 按附件类型做相应的处理 95                 path = os.path.join(directory, filename) 96                 if not os.path.isfile(path): 97                     continue 98                 # Guess the content type based on the file's extension.  Encoding 99                 # will be ignored, although we should check for simple things like100                 # gzip'd or compressed files.101                 ctype, encoding = mimetypes.guess_type(path) # 附件类型的判断102                 if ctype is None or encoding is not None:103                     # No guess could be made, or the file is encoded (compressed), so104                     # use a generic bag-of-bits type.105                     ctype = 'application/octet-stream'       # zip, pdf 等附件的类型106                 maintype, subtype = ctype.split('/', 1)107                 if maintype == 'text':             # 处理的 文本text 类型的附件108                     fp = open(path)109                     # Note: we should handle calculating the charset110                     msg = MIMEText(fp.read(), _subtype=subtype)111                     fp.close()112                 elif maintype == 'image':          # 处理的 图像image 类型的附件113                     fp = open(path, 'rb')114                     msg = MIMEImage(fp.read(), _subtype=subtype)115                     fp.close()116                 elif maintype == 'audio':          # 处理的 音频audio 类型的附件117                     fp = open(path, 'rb')118                     msg = MIMEAudio(fp.read(), _subtype=subtype)119                     fp.close()120                 else:121                     fp = open(path, 'rb')          # 处理的其他类型的附件:如 zip, pdf 等122                     msg = MIMEBase(maintype, subtype)123                     msg.set_payload(fp.read())124                     fp.close()125                     # Encode the payload using Base64126                     encoders.encode_base64(msg)127                 # Set the filename parameter128                 msg.add_header('Content-Disposition', 'attachment', filename=filename)129                 outer.attach(msg)130             # Now send or store the message131             composed = outer.as_string()132             if opts.output:                        # 保存到文件 store133                 fp = open(opts.output, 'w')134                 fp.write(composed)135                 fp.close()136             else:137                 s = smtplib.SMTP('localhost')      # SMTP server 发送处理138                 s.sendmail(opts.sender, opts.recipients, composed)139                 s.quit()140 141         if __name__ == '__main__':142             main()143 144 MIME 的解包 - Unpacking MIME145 146     接收方对收到的 MIMEMultipart 的处理,147         '''Unpack a MIME message into a directory of files.'''148 149         import os150         import sys151         import email152         import errno153         import mimetypes154         from optparse import OptionParser155         def main():156             parser = OptionParser(usage='''\157             Unpack a MIME message into a directory of files.158 159             Usage: %prog [options] msgfile160             ''')161             parser.add_option('-d', '--directory',162                               type='string', action='store',163                               help='''Unpack the MIME message into the named164                               directory, which will be created if it doesn't already165                               exist.''')166             opts, args = parser.parse_args()167             if not opts.directory:168                 parser.print_help()169                 sys.exit(1)170 171             try:172                 msgfile = args[0]173             except IndexError:174                 parser.print_help()175                 sys.exit(1)176 177             try:178                 os.mkdir(opts.directory)179             except OSError as e:180                 # Ignore directory exists error181                 if e.errno != errno.EEXIST:182                     raise183 184             fp = open(msgfile)185             msg = email.message_from_file(fp)186             fp.close()187 188             counter = 1189             for part in msg.walk():190                 # multipart/* are just containers191                 if part.get_content_maintype() == 'multipart':192                     continue193                 # Applications should really sanitize the given filename so that an194                 # email message can't be used to overwrite important files195                 filename = part.get_filename()196                 if not filename:197                     ext = mimetypes.guess_extension(part.get_content_type())198                     if not ext:199                         # Use a generic bag-of-bits extension200                         ext = '.bin'201                     filename = 'part-%03d%s' % (counter, ext)202                 counter += 1203                 fp = open(os.path.join(opts.directory, filename), 'wb')204                 fp.write(part.get_payload(decode=True))205                 fp.close()206 207         if __name__ == '__main__':208             main()209 210 Reference,211     https://docs.python.org/3/library/email.html

 

转载于:https://www.cnblogs.com/zzyzz/p/8057918.html

你可能感兴趣的文章
随笔:技术流可以这样写博客
查看>>
[优化]JavaScript 格式化带有占位符字符串
查看>>
打JAR包
查看>>
大图轮播
查看>>
UNIX环境高级编程读书笔记
查看>>
java awt 乱码问题
查看>>
矩阵中的路径
查看>>
unity回调函数范例
查看>>
linux下给php安装curl、gd(ubuntu)
查看>>
Java自带的Logger使用-代码摘要
查看>>
Java设计模式系列 — 构造器模式
查看>>
MySQL执行计划explain的key_len解析
查看>>
Windows Phone开发(9):关于页面状态 转:http://blog.csdn.net/tcjiaan/article/details/7292160...
查看>>
android 通过数组,流播放声音的方法
查看>>
Spring入门篇
查看>>
JAVA遇见HTML——JSP篇(JSP状态管理)
查看>>
启动eclipse出现错误Java was started but returned exit =一个数字
查看>>
myBatis模糊查找
查看>>
数据结构与算法之五 链接列表
查看>>
java 对象数组
查看>>