django发送邮件,当附件是js文件时,收到的邮件是js.txt。当压缩js文件时,rar可以的,zip会解析成zip.txt。
各位大神有没有类似的经历,或者思路
补充代码:
import os,json,threading,datetime
os.environ['DJANGO_SETTINGS_MODULE'] = '####.settings'
from django.core.mail import EmailMultiAlternatives
from django.core.mail import send_mail
from django.conf import settings
class MyThread(threading.Thread):
def __init__(self,from_email,to_email,cc_email,subject,html_content,attach_files):
threading.Thread.__init__(self)
self.from_email = from_email
self.to_email = to_email
self.cc_email = cc_email
self.subject = subject
self.html_content = html_content
self.attach_files=attach_files
def run(self):
sendemail(self.from_email,self.to_email,self.cc_email,self.subject,self.html_content,self.attach_files)
def sendemail(from_email,to_email,cc_email,subject,html_content,attach_files):
try:
msg = EmailMultiAlternatives(subject=subject, body=html_content, from_email=from_email, to=to_email, cc=cc_email)
msg.content_subtype = 'html'
for attach_file in attach_files:
msg.attach_file(attach_file, 'image/gif')
msg.send()
except Exception as e:
print(e)
def faultmail(request, prjname,ip,warinfo,trouble_time,faultpath,messageinfo):
try:
cc_email=['*****@***.com']
content = "<font >信息:</font><br></br><table style='border-collapse:collapse;table-layout:" \
"<br>-----------------------------------------------------------</br><br></br>" \
"fixed;border:1px #333 solid;'>tr_content<tbody></tbody></table>"\
tr_format = "<tr ><td class='title' style='word-wrap: break-word;width:120px;border:1px #333 solid;background-color:#ccc;'>" \
"<font color ='red'>*</font>title_name</td><td style='word-wrap: " \
"break-word;width:20em;text-align:center;border:1px #333 solid;padding: 5px;'>title_value</td></tr>"
td_redfont = "<font color ='red'>*</font>"
title_valuefont = "<font color ='red'>title_value</font>"
common_content = set_common_content(tr_format, prjname, ip, warinfo, trouble_time,messageinfo)
email_theme = 'message'
tr_sign = "<br></br>-----------------------------------------------------------<br>" + request.user.UserName + ' | ' + request.user.Dept + \
'<br>Tel:' + request.user.Telephone + '<br>Mail:' + request.user.Email
tr_content = common_content
tr_addr = "<br>访问地址:<a href='****.com'>****.com</a><br>"
html_content = content.replace('tr_content', tr_content) + tr_addr + tr_sign
#from_email = request.user.UserName
from_email = request.user.UserName + ' <' + request.user.Email + '>'
to_email=['###@###.com']
subject = settings.EMAIL_SUBJECT_PREFIX + prjname+ ip+email_theme
filelist = faultpath.split('|')
attach_files = add_str_list(filelist, settings.MEDIA_ROOT)
thread = MyThread(from_email, to_email,cc_email, subject, html_content,attach_files)
thread.start()
except Exception as e:
print(e)
def set_common_content(tr_format ,prjname,ip,warinfo,trouble_time,messageinfo):
prjname = tr_format.replace('title_name', '项目:').replace('title_value',prjname)
ip = tr_format.replace('title_name', 'IP:').replace('title_value',ip )
warinfo = tr_format.replace('title_name', '信息:').replace('title_value',warinfo)
trouble_time = tr_format.replace('title_name', '时间:').replace('title_value', trouble_time)
messageinfo = tr_format.replace('title_name', '说明:').replace('title_value', messageinfo)
tr_common_content =prjname+ip+warinfo+trouble_time+messageinfo
return tr_common_content
def add_str_list(list,s):
return [s + str(i) for i in list]
邮件图片:
这么奇怪的问题,方便的话把相关代码贴出来看看。