python邮件服务

文件形式的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.text import MIMEText
  5. from email.header import Header
  6. sender = ‘***‘
  7. receiver = ‘***‘
  8. subject = ‘python email test‘
  9. smtpserver = ‘smtp.163.com‘
  10. username = ‘***‘
  11. password = ‘***‘
  12. msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
  13. msg[‘Subject‘] = Header(subject, ‘utf-8‘)
  14. smtp = smtplib.SMTP()
  15. smtp.connect(‘smtp.163.com‘)
  16. smtp.login(username, password)
  17. smtp.sendmail(sender, receiver, msg.as_string())
  18. smtp.quit()

HTML形式的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.text import MIMEText
  5. sender = ‘***‘
  6. receiver = ‘***‘
  7. subject = ‘python email test‘
  8. smtpserver = ‘smtp.163.com‘
  9. username = ‘***‘
  10. password = ‘***‘
  11. msg = MIMEText(‘<html><h1>你好</h1></html>‘,‘html‘,‘utf-8‘)
  12. msg[‘Subject‘] = subject
  13. smtp = smtplib.SMTP()
  14. smtp.connect(‘smtp.163.com‘)
  15. smtp.login(username, password)
  16. smtp.sendmail(sender, receiver, msg.as_string())
  17. smtp.quit()

带图片的HTML邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.mime.image import MIMEImage
  7. sender = ‘***‘
  8. receiver = ‘***‘
  9. subject = ‘python email test‘
  10. smtpserver = ‘smtp.163.com‘
  11. username = ‘***‘
  12. password = ‘***‘
  13. msgRoot = MIMEMultipart(‘related‘)
  14. msgRoot[‘Subject‘] = ‘test message‘
  15. msgText = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!‘,‘html‘,‘utf-8‘)
  16. msgRoot.attach(msgText)
  17. fp = open(‘h:\\python\\1.jpg‘, ‘rb‘)
  18. msgImage = MIMEImage(fp.read())
  19. fp.close()
  20. msgImage.add_header(‘Content-ID‘, ‘<image1>‘)
  21. msgRoot.attach(msgImage)
  22. smtp = smtplib.SMTP()
  23. smtp.connect(‘smtp.163.com‘)
  24. smtp.login(username, password)
  25. smtp.sendmail(sender, receiver, msgRoot.as_string())
  26. smtp.quit()

带附件的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.mime.image import MIMEImage
  7. sender = ‘***‘
  8. receiver = ‘***‘
  9. subject = ‘python email test‘
  10. smtpserver = ‘smtp.163.com‘
  11. username = ‘***‘
  12. password = ‘***‘
  13. msgRoot = MIMEMultipart(‘related‘)
  14. msgRoot[‘Subject‘] = ‘test message‘
  15. #构造附件
  16. att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
  17. att["Content-Type"] = ‘application/octet-stream‘
  18. att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
  19. msgRoot.attach(att)
  20. smtp = smtplib.SMTP()
  21. smtp.connect(‘smtp.163.com‘)
  22. smtp.login(username, password)
  23. smtp.sendmail(sender, receiver, msgRoot.as_string())
  24. smtp.quit()

群邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.text import MIMEText
  5. sender = ‘***‘
  6. receiver = [‘***‘,‘****‘,……]
  7. subject = ‘python email test‘
  8. smtpserver = ‘smtp.163.com‘
  9. username = ‘***‘
  10. password = ‘***‘
  11. msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)
  12. msg[‘Subject‘] = subject
  13. smtp = smtplib.SMTP()
  14. smtp.connect(‘smtp.163.com‘)
  15. smtp.login(username, password)
  16. smtp.sendmail(sender, receiver, msg.as_string())
  17. smtp.quit()

各种元素都包含的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.mime.image import MIMEImage
  7. sender = ‘***‘
  8. receiver = ‘***‘
  9. subject = ‘python email test‘
  10. smtpserver = ‘smtp.163.com‘
  11. username = ‘***‘
  12. password = ‘***‘
  13. # Create message container - the correct MIME type is multipart/alternative.
  14. msg = MIMEMultipart(‘alternative‘)
  15. msg[‘Subject‘] = "Link"
  16. # Create the body of the message (a plain-text and an HTML version).
  17. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
  18. html = """\
  19. <html>
  20. <head></head>
  21. <body>
  22. <p>Hi!<br>
  23. How are you?<br>
  24. Here is the <a href="http://www.python.org">link</a> you wanted.
  25. </p>
  26. </body>
  27. </html>
  28. """
  29. # Record the MIME types of both parts - text/plain and text/html.
  30. part1 = MIMEText(text, ‘plain‘)
  31. part2 = MIMEText(html, ‘html‘)
  32. # Attach parts into message container.
  33. # According to RFC 2046, the last part of a multipart message, in this case
  34. # the HTML message, is best and preferred.
  35. msg.attach(part1)
  36. msg.attach(part2)
  37. #构造附件
  38. att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
  39. att["Content-Type"] = ‘application/octet-stream‘
  40. att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
  41. msg.attach(att)
  42. smtp = smtplib.SMTP()
  43. smtp.connect(‘smtp.163.com‘)
  44. smtp.login(username, password)
  45. smtp.sendmail(sender, receiver, msg.as_string())
  46. smtp.quit()

基于SSL的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.text import MIMEText
  5. from email.header import Header
  6. sender = ‘***‘
  7. receiver = ‘***‘
  8. subject = ‘python email test‘
  9. smtpserver = ‘smtp.163.com‘
  10. username = ‘***‘
  11. password = ‘***‘
  12. msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
  13. msg[‘Subject‘] = Header(subject, ‘utf-8‘)
  14. smtp = smtplib.SMTP()
  15. smtp.connect(‘smtp.163.com‘)
  16. smtp.ehlo()
  17. smtp.starttls()
  18. smtp.ehlo()
  19. smtp.set_debuglevel(1)
  20. smtp.login(username, password)
  21. smtp.sendmail(sender, receiver, msg.as_string())
  22. smtp.quit()  

    文件形式的邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.text import MIMEText
    5. from email.header import Header
    6. sender = ‘***‘
    7. receiver = ‘***‘
    8. subject = ‘python email test‘
    9. smtpserver = ‘smtp.163.com‘
    10. username = ‘***‘
    11. password = ‘***‘
    12. msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
    13. msg[‘Subject‘] = Header(subject, ‘utf-8‘)
    14. smtp = smtplib.SMTP()
    15. smtp.connect(‘smtp.163.com‘)
    16. smtp.login(username, password)
    17. smtp.sendmail(sender, receiver, msg.as_string())
    18. smtp.quit()

    HTML形式的邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.text import MIMEText
    5. sender = ‘***‘
    6. receiver = ‘***‘
    7. subject = ‘python email test‘
    8. smtpserver = ‘smtp.163.com‘
    9. username = ‘***‘
    10. password = ‘***‘
    11. msg = MIMEText(‘<html><h1>你好</h1></html>‘,‘html‘,‘utf-8‘)
    12. msg[‘Subject‘] = subject
    13. smtp = smtplib.SMTP()
    14. smtp.connect(‘smtp.163.com‘)
    15. smtp.login(username, password)
    16. smtp.sendmail(sender, receiver, msg.as_string())
    17. smtp.quit()

    带图片的HTML邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.multipart import MIMEMultipart
    5. from email.mime.text import MIMEText
    6. from email.mime.image import MIMEImage
    7. sender = ‘***‘
    8. receiver = ‘***‘
    9. subject = ‘python email test‘
    10. smtpserver = ‘smtp.163.com‘
    11. username = ‘***‘
    12. password = ‘***‘
    13. msgRoot = MIMEMultipart(‘related‘)
    14. msgRoot[‘Subject‘] = ‘test message‘
    15. msgText = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!‘,‘html‘,‘utf-8‘)
    16. msgRoot.attach(msgText)
    17. fp = open(‘h:\\python\\1.jpg‘, ‘rb‘)
    18. msgImage = MIMEImage(fp.read())
    19. fp.close()
    20. msgImage.add_header(‘Content-ID‘, ‘<image1>‘)
    21. msgRoot.attach(msgImage)
    22. smtp = smtplib.SMTP()
    23. smtp.connect(‘smtp.163.com‘)
    24. smtp.login(username, password)
    25. smtp.sendmail(sender, receiver, msgRoot.as_string())
    26. smtp.quit()

    带附件的邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.multipart import MIMEMultipart
    5. from email.mime.text import MIMEText
    6. from email.mime.image import MIMEImage
    7. sender = ‘***‘
    8. receiver = ‘***‘
    9. subject = ‘python email test‘
    10. smtpserver = ‘smtp.163.com‘
    11. username = ‘***‘
    12. password = ‘***‘
    13. msgRoot = MIMEMultipart(‘related‘)
    14. msgRoot[‘Subject‘] = ‘test message‘
    15. #构造附件
    16. att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
    17. att["Content-Type"] = ‘application/octet-stream‘
    18. att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
    19. msgRoot.attach(att)
    20. smtp = smtplib.SMTP()
    21. smtp.connect(‘smtp.163.com‘)
    22. smtp.login(username, password)
    23. smtp.sendmail(sender, receiver, msgRoot.as_string())
    24. smtp.quit()

    群邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.text import MIMEText
    5. sender = ‘***‘
    6. receiver = [‘***‘,‘****‘,……]
    7. subject = ‘python email test‘
    8. smtpserver = ‘smtp.163.com‘
    9. username = ‘***‘
    10. password = ‘***‘
    11. msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)
    12. msg[‘Subject‘] = subject
    13. smtp = smtplib.SMTP()
    14. smtp.connect(‘smtp.163.com‘)
    15. smtp.login(username, password)
    16. smtp.sendmail(sender, receiver, msg.as_string())
    17. smtp.quit()

    各种元素都包含的邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.multipart import MIMEMultipart
    5. from email.mime.text import MIMEText
    6. from email.mime.image import MIMEImage
    7. sender = ‘***‘
    8. receiver = ‘***‘
    9. subject = ‘python email test‘
    10. smtpserver = ‘smtp.163.com‘
    11. username = ‘***‘
    12. password = ‘***‘
    13. # Create message container - the correct MIME type is multipart/alternative.
    14. msg = MIMEMultipart(‘alternative‘)
    15. msg[‘Subject‘] = "Link"
    16. # Create the body of the message (a plain-text and an HTML version).
    17. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
    18. html = """\
    19. <html>
    20. <head></head>
    21. <body>
    22. <p>Hi!<br>
    23. How are you?<br>
    24. Here is the <a href="http://www.python.org">link</a> you wanted.
    25. </p>
    26. </body>
    27. </html>
    28. """
    29. # Record the MIME types of both parts - text/plain and text/html.
    30. part1 = MIMEText(text, ‘plain‘)
    31. part2 = MIMEText(html, ‘html‘)
    32. # Attach parts into message container.
    33. # According to RFC 2046, the last part of a multipart message, in this case
    34. # the HTML message, is best and preferred.
    35. msg.attach(part1)
    36. msg.attach(part2)
    37. #构造附件
    38. att = MIMEText(open(‘h:\\python\\1.jpg‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
    39. att["Content-Type"] = ‘application/octet-stream‘
    40. att["Content-Disposition"] = ‘attachment; filename="1.jpg"‘
    41. msg.attach(att)
    42. smtp = smtplib.SMTP()
    43. smtp.connect(‘smtp.163.com‘)
    44. smtp.login(username, password)
    45. smtp.sendmail(sender, receiver, msg.as_string())
    46. smtp.quit()

    基于SSL的邮件

    [python] view plaincopy

    1. #!/usr/bin/env python3
    2. #coding: utf-8
    3. import smtplib
    4. from email.mime.text import MIMEText
    5. from email.header import Header
    6. sender = ‘***‘
    7. receiver = ‘***‘
    8. subject = ‘python email test‘
    9. smtpserver = ‘smtp.163.com‘
    10. username = ‘***‘
    11. password = ‘***‘
    12. msg = MIMEText(‘你好‘,‘text‘,‘utf-8‘)#中文需参数‘utf-8’,单字节字符不需要
    13. msg[‘Subject‘] = Header(subject, ‘utf-8‘)
    14. smtp = smtplib.SMTP()
    15. smtp.connect(‘smtp.163.com‘)
    16. smtp.ehlo()
    17. smtp.starttls()
    18. smtp.ehlo()
    19. smtp.set_debuglevel(1)
    20. smtp.login(username, password)
    21. smtp.sendmail(sender, receiver, msg.as_string())
    22. smtp.quit()

转载:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html

时间: 2024-07-28 19:59:03

python邮件服务的相关文章

【python邮件服务】每天早上定时定时发送天气给邮箱

壹:获取天气api 打开和风天气:https://console.heweather.com/,在注册和登陆之后,点击应用管理新建应用,创建key就会有下列实例. 一:api: 打开https://dev.heweather.com/docs/api/weather开发文档查看调用api 注意:分为免费版与商业版,一般我们个人使用只要用免费版就行了. api: 1.现在:now(实况天气) https://free-api.heweather.net/s6/weather/now?locatio

邮件服务

邮件语言:M4 SASL: v2 cyrus-sasl 认证框架 courier-authlib MTA: 邮件传输代理,SMTP服务器 sendmail,  UUCP qmail 数学家当程序员,他写的算法绝对是一流的 postfix 安全.兼容.效率高 exim Exchange (异步消息协作平台) 重量级 MDA: 邮件投递代理 procmail maildrop MRA: 邮件检索代理(pop3, imap4) cyrus-imap dovecot MUA: 邮件用户代理 Outloo

电商邮件服务平台性能优化谈

从今年一月份开始,团队陆续完成了邮件服务的架构升级.新平台上线运行的过程中也发生了一系列的性能问题,即使很多看起来微不足道的点也会让整个系统运行得不是那么平稳,今天就将这段时间的问题以及解决方案统一整理下,希望能起到抛砖的作用,让读者在遇到类似问题的时候能多一个解决方案. 新平台上线后第一版架构如下: 这版架构上线后,我们遇到的第一个问题:数据库读写压力过大后影响整体服务稳定. 表现为: 1.数据库主库压力高,同时伴有大量的读,写操作. 2.远程服务接口性能不稳定,业务繁忙时数据库的插入操作延迟

写一个python的服务监控程序

写一个python的服务监控程序 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python站点下载源码.解压到Redhat上.然后执行以下的命令: # ./configure --prefix=/usr/local/python27 # make # make install 这样安装之后默认不会启用Python2.7.须要使用/usr/local/python27/bin/python2.7调用新版本号的python. 而

使用mail.rc快速配置linux发邮件服务

1.系统环境. [[email protected] ~]# cat /etc/redhat-release  CentOS release 6.8 (Final) [[email protected] ~]# uname -r 2.6.32-642.el6.x86_64 [[email protected] ~]# uname -m x86_64 2.通过修改配置文件/etc/mail.rc可以使用外部SMTP服务器,轻松实现linux发邮件功能. [[email protected] ~]#

【初学菜鸟作--邮件服务的简单配置案例】

邮件服务器的配置以及使用 实验一:                    实验目的:简单搭建出邮件服务器并测试其可用性                    实验环境:DNS服务器一台,安装有Portfix的邮件服务器一台 实验步骤: 一.邮件的发送(SMTP) 1.在邮件服务器配置主机名,ip,并安装portfix并启动 [[email protected]~]# tail -2 /etc/sysconfig/network HOSTNAME=mail.tarena.com   [[email

谢烟客---------Linux之邮件服务及任务计划执行

任务计划命令 mail,at,batch,crond,sleep 邮件服务工作模式: 发 代理-> stmp --> smtp --> 投递代理 --> 邮筒 --> pop3,imap4 <-- 代理 <-- 收 at,mail,batch,cron命令均建议用完整路径或在脚本中定义PATH 特点: mail -s '主题' [email protected] 发邮件 batch 根据负载选定命令执行时间 at HH:MM am|pm [YYYY-MM-DD]

邮件服务系列之五安装Extman,maildrop以及clamav-0.97.7的安装

前面我们已经完成了基础的邮件系统所需的组建的安装以及配置,下面我们进行Extman-1.1,maildrop以及clamav-0.97.7的安装以及配置,来完成整个邮件服务系统的搭建. 一.安装Extman-1.1 1.安装及基本配置 # tar zxvf  extman-1.1.tar.gz # mv extman-1.1 /var/www/extsuite/extman 修改配置文件以符合本例的需要: # cp /var/www/extsuite/extman/webman.cf.defau

邮件服务系列之三实现postfix+dovecot+sasl

MRA :cyrus-imap,dovecot dovecot 依赖MySQL客户端 pop3协议监听tcp110 imap4协议监听tcp143端口 以明文方式工作需结合sasl来实现邮件传输加密 dovecot支持四种协议: pop3 imap4 pop3s imaps 配置文件位于:/etc/dovecot.conf 带有sasl认证能力 支持两种邮箱格式: mbox一个文件存储所有邮件 maildir:一个文件存储一封邮件,所有邮件存储在一个目录中 安装: yum install dov