Python 3.4 send mail

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
#coding=utf-8
#Python 3.4  https://docs.python.org/3.4/library/
#IDE:Visual Studio 2015  Window10
import atexit
import os
import unicodedata
import sys
import time
import unicodedata
import winsound
import code
import codecs
import math
import csv
import base64
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header 
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase 
from email.utils import COMMASPACE, formatdate 
import email
import os.path 
import sys  
import mimetypes  
import configparser
import string 
 
#https://docs.python.org/3.4/library/email.html
#https://docs.python.org/3/library/email-examples.html
 
inifile='F:/Python/055.JPG' 
#config=ConfigParser.ConfigParser() 
#config.read(inifile) 
#os.remove(inifile) #移除文件
subject=Header("缔友计算机有限公司销售报告","utf-8"#邮件标题
ReplyToName="geovindu@dusystem.com" 
ReplyToMail="geovindu@dusystem.com" 
To="463588883@qq.com"
bcc="geovindu@163.com";
cc="geovindu@jw28.com";
COMMASPACE = ', ';
file_name=inifile #"file_name" 
From = "%s<geovindu@dusystem.com>" % Header("缔友计算机信息有公司涂聚文","utf-8"#发件人和姓名
server = smtplib.SMTP("mail.dusystem.com",25
server.login("geovindu@dusystem.com","888") #仅smtp服务器需要验证时 
   
# 构造MIMEMultipart对象做为根容器 
main_msg =  MIMEMultipart("alternative");  #alternative   related  2.x版本的email.MIMEMultipart.MIMEMultipart()
# 构造MIMEText对象做为邮件显示内容并附加到根容器 
text_msg =MIMEText("xxx帮你转发的邮件", _subtype='html',_charset="utf-8"#邮件内容  2.x版本的 email.MIMEText.MIMEText
main_msg.attach(text_msg) 
 
# 构造MIMEBase对象做为文件附件内容并附加到根容器 
 
#图片未显示,附件不同
#fp = open(file_name, 'rb')   
#msgImage = MIMEImage(fp.read())   
#fp.close()       
#msgImage.add_header('Content-ID', '<image1>')   
#main_msg.attach(msgImage) 
 
    
## 设置附件头 
#basename = os.path.basename(file_name) 
#file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头 
file_msg = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')    #发文件
file_msg["Content-Type"] = 'application/octet-stream'   
file_msg["Content-Disposition"] = 'attachment; filename="055.JPG"'
 
main_msg.attach(file_msg) 
# 设置根容器属性 
main_msg['From'] = From 
if ReplyToMail!='none'
    main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail) 
main_msg['To'] = To; 
main_msg['Subject'] = subject; 
main_msg['Cc']=cc;
main_msg['Bcc'] = bcc;  #这无效
main_msg['Date'] =formatdate(localtime=True)
# 得到格式化后的完整文本 
fullText = main_msg.as_string() 
# 用smtp发送邮件 
try
    server.sendmail(From, To.split(';'), fullText) 
finally
    server.quit() 
    #os.remove(file_name) 
print("发送邮件成功");

  上发图片

下为发文件

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
#coding=utf-8
#Python 3.4  https://docs.python.org/3.4/library/
#IDE:Visual Studio 2015  Window10
import atexit #类库
import os
import unicodedata
import sys
import time
import unicodedata
import winsound
import code
import codecs
import math
import csv
import base64
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header 
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase 
from email.encoders import encode_base64
from email.mime.application import MIMEApplication
from email.utils import COMMASPACE, formatdate 
import email
import os.path 
import sys  
import mimetypes  
import configparser
import string 
import mimetypes
import base64;
 
#https://docs.python.org/3.4/library/email.html
#https://docs.python.org/3/library/email-examples.html
 
inifile=u'F:/Python/工应用文写作.doc' 
#config=ConfigParser.ConfigParser() 
#config.read(inifile) 
#os.remove(inifile) #移除文件
subject=Header("缔友计算机有限公司销售报告","utf-8"#邮件标题
ReplyToName="geovindu@dusystem.com" 
ReplyToMail="geovindu@dusystem.com" 
To="463588883@qq.com"
bcc="geovindu@163.com";
cc="geovindu@jw28.com";
COMMASPACE = ', ';
file_name=inifile #"file_name" 
From = "%s<geovindu@dusystem.com>" % Header("缔友计算机信息有公司涂聚文","utf-8"#发件人和姓名
server = smtplib.SMTP("mail.dusystem.com",25
server.login("geovindu@dusystem.com","888") #仅smtp服务器需要验证时 
   
# 构造MIMEMultipart对象做为根容器 
main_msg =  MIMEMultipart("alternative");  #alternative   related  2.x版本的email.MIMEMultipart.MIMEMultipart()
# 构造MIMEText对象做为邮件显示内容并附加到根容器 
text_msg =MIMEText("请你及阅完文档,提出建议", _subtype='html',_charset="utf-8"#邮件内容  2.x版本的 email.MIMEText.MIMEText
main_msg.attach(text_msg) 
 
# 构造MIMEBase对象做为文件附件内容并附加到根容器 
 
#图片未显示,附件不同
#fp = open(file_name, 'rb')   
#msgImage = MIMEImage(fp.read())   
#fp.close()       
#msgImage.add_header('Content-ID', '<image1>')   
#main_msg.attach(msgImage) 
 
    
## 设置附件头 
#basename = os.path.basename(file_name) 
#file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头 
#图片
#file_image="F:/Python/055.jpg";
#file_msg = MIMEText(open(file_image, 'rb').read(), 'base64', 'utf-8')    #发完文件不见了
#file_msg["Content-Type"] = 'application/octet-stream'     # 图片(application/octet-stream)可以,但WORD不行
#file_msg["Content-Disposition"] = 'attachment; filename="055.jpg"'
#1 可以文件名乱码
#contentType, encoding = mimetypes.guess_type(file_name)
#mainType, subType = contentType.split('/', 1) 
#file = open(file_name, 'rb')
#attachment = MIMEBase(mainType, subType)
#attachment.set_payload(file.read()) 
#encode_base64(attachment) 
#file.close()  
#basename = os.path.basename(file_name);
#attachment.add_header('Content-Disposition', 'attachment',filename=basename)
#attachment.add_header('Content-Disposition', 'attachment',filename=os.path.basename(file_name))  #file_name
 
#2中文件名乱码
#file_msg = MIMEApplication(open(file_name, 'rb').read())    #发文件 
#file_msg.add_header('Content-Disposition', 'attachment', filename="应用文写作.doc") 
#main_msg.attach(file_msg)  #file_msg
 
#3注意:指定att的编码
basename = os.path.basename(file_name)
att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8'
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename=%s' % basename.encode("utf-8")
main_msg.attach(att) 
 
# 设置根容器属性 
main_msg['From'] = From 
if ReplyToMail!='none'
    main_msg['Reply-to'] = "%s<%s>" % (Header(ReplyToName,"utf-8"),ReplyToMail) 
main_msg['To'] = To; 
main_msg['Subject'] = subject; 
main_msg['Cc']=cc;
main_msg['Bcc'] = bcc;  #这无效
main_msg['Date'] =formatdate(localtime=True)
# 得到格式化后的完整文本 
fullText = main_msg.as_string() 
# 用smtp发送邮件 
try
    server.sendmail(From, To.split(';'), fullText) 
finally
    server.quit() 
    #os.remove(file_name)  移除文件
print("发送邮件成功");

  

1
2
3
4
5
6
7
8
#4.中文无乱码
file_name=u'F:/Python/应用文写作.doc'
urlfilename="应用文写作.doc"
att = MIMEBase('application', 'octet-stream'
att.set_payload(open(file_name, 'rb').read()) 
att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', urlfilename) )  #gbk 收件时,名称不乱码。
encode_base64(att)
main_msg.attach(att)

  

posted @   ®Geovin Du Dream Park™  阅读(417)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-12-09 css:条件注释判断浏览器
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示