码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3.4 send mail

时间:2016-12-09 12:14:31      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:chm   ifile   asp   amp   base64   config   imei   type   osi   

#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("发送邮件成功");

  

Python 3.4 send mail

标签:chm   ifile   asp   amp   base64   config   imei   type   osi   

原文地址:http://www.cnblogs.com/geovindu/p/6148755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!