def _get_active_receiver_list(data, date, user=''): mail_model = get_mail_model(date) if user: mails = mail_model.objects.filter(customer=user) else: mails = mail_model.objects.all() mails = mails.values("mail_to").annotate( total_all=Count("id"), size=Sum("size"), total=Sum( Case(When(mail_id=0, then=Value(1)), default=Value(0), output_field=IntegerField())), total_success=Sum( Case(When(state='finished', then=Value(1)), default=Value(0), output_field=IntegerField())), size_success=Sum( Case(When(state='finished', then='size'), default=Value(0), output_field=IntegerField())), total_reject=Sum( Case(When(Q(state='reject') | Q(state='c_reject'), then=Value(1)), default=Value(0), output_field=IntegerField())), size_reject=Sum( Case(When(Q(state='reject') | Q(state='c_reject'), then='size'), default=Value(0), output_field=IntegerField())), total_fail=Sum( Case(When(Q(state='fail_finished') | Q(state='bounce'), then=Value(1)), default=Value(0), output_field=IntegerField())), size_fail=Sum( Case(When(Q(state='fail_finished') | Q(state='bounce'), then='size'), default=Value(0), output_field=IntegerField())), total_out=Sum( Case(When(Q(state='finished') | Q(state='fail_finished') | Q(state='bounce'), then=Value(1)), default=Value(0), output_field=IntegerField())), size_out=Sum( Case(When(Q(state='finished') | Q(state='fail_finished') | Q(state='bounce'), then='size'), default=Value(0), output_field=IntegerField())), ) try: mails.exists() except: mails = [] return mails
def deliver_logs(request, id): date, mail_id = id.split('_') logs = DeliverLog.objects.filter(date=date, mail_id=mail_id) m = get_mail_model(date).objects.get(id=mail_id) return render_to_response("collect_mail/deliver_logs.html", { 'logs': logs, 'm': m, }, context_instance=RequestContext(request))
def customer_report(request): id = request.GET.get('id', '') if id: date, id = id.split('_', 1) m = get_mail_model(date).objects.get(customer=request.user, id=id) if m.customer_report == 0: m.customer_report = 1 m.save() return HttpResponse(json.dumps({'msg': _(u'已举报垃圾邮件') % {}}), content_type="application/json")
def add_sender_blacklist(mail_id, is_global=False, is_domain=False): date, id = mail_id.split('_') mail_obj = get_mail_model(date).objects.get(id=id) mail_from = mail_obj.mail_from if '=' in mail_from: mail_from = mail_from.split('=')[-1] if is_domain: mail_from = mail_from.split('@')[-1] customer = None if is_global else mail_obj.customer CustomerSenderBlacklist.objects.get_or_create(sender=mail_from, is_global=is_global, is_domain=is_domain, customer=customer)
def resent(request): ids = request.GET.get('id', '') date, id = ids.split('_') msg = _(u'发送失败,只有发送完成/失败的邮件可以再次发送') % {} m = get_mail_model(date).objects.get(id=id) if m.state in ['finished', 'fail_finished']: try: redis = get_redis_connection2(m.server_id) except: redis = get_redis_connection2('default') redis.lpush('collect_send', m.get_mail_filename()) m.state = 'retry' m.save() msg = _(u'已再次发送邮件') % {} return HttpResponse(json.dumps({'msg': msg}), content_type="application/json")
def mail_review(request): id = request.GET.get('id', '') result = request.GET.get('result', '') # date = get_date(request.GET) # 单个ajax审核 if id and result: date, id = id.split('_', 1) mails = get_mail_model(date).objects.filter(customer=request.user, id=id) if mails: m = mails[0] try: redis = get_redis_connection2(m.server_id) except: redis = get_redis_connection2('default') if result in ['customer_pass', 'customer_domain_pass']: is_domain = True if result.find('domain') != -1 else False add_sender_whitelist(m.date_id(), is_global=False, is_domain=is_domain) result = 'c_pass' if result in ['customer_reject', 'customer_domain_reject']: is_domain = True if result.find('domain') != -1 else False add_sender_whitelist(m.date_id(), is_global=False, is_domain=is_domain) result = 'c_reject' m.review_result = result if result in ['c_pass', 'c_pass_undo']: m.state = 'send' msg = _(u'已放行') % {} else: m.state = 'c_reject' msg = _(u'已拒绝') % {} m.save(update_fields=['review_result', 'state']) if result in ['c_pass', 'c_pass_undo']: redis.lpush('collect_send', m.get_mail_filename()) return HttpResponse(json.dumps({'msg': msg}), content_type="application/json")
def mail_read(request): date_id = request.GET.get('id', '') date, id = date_id.split('_') cid = request.GET.get('cid', '') aid = request.GET.get('aid', '') download = request.GET.get('download', '') view_body = request.GET.get('view_body', '') view_source = request.GET.get('view_source', '') export = request.GET.get('export', '') can_view_mail = request.user.customer_setting.can_view_mail # if not can_view_mail: # return HttpResponse(u'无法查看邮件') if id and date: mail_obj = get_mail_model(date).objects.get(id=id) content = mail_obj.get_mail_content() parse_obj = ParseEmail(content) m = parse_obj.parseMailTemplate() if cid or aid: attachments = m['attachments'] if aid: attach = attachments[int(aid)] response = HttpResponse(attach.get('data', ''), content_type=attach.get( 'content_type', '').split(';')[0]) else: for one in attachments: if one.get('content_id') == cid: attach = one response = HttpResponse(attach.get('data', ''), content_type=attach.get( 'content_type', '').split(';')[0]) break if download: response[ 'Content-Disposition'] = 'attachment; filename="{}"'.format( attach.get('decode_name', '').encode('utf-8')) return response if view_body: if not can_view_mail: return HttpResponse() text = m.get('html_text', '') charset = m.get('html_charset', '') if not text: text = m.get('plain_text', '') charset = m.get('plain_charset', '') link = '{}?id={}&cid=\g<cid>'.format(reverse('mail_read'), date_id) text = re.sub('"cid:(?P<cid>.*?)"', link, text) if mail_obj.check_result in [ 'keyword_blacklist', 'custom_blacklist' ] and text and charset: s = mail_obj.check_message.split('----', 1)[-1] re_s = u'<span style="background-color: yellow"><b style="color:#A94442;">{}</b></span>'.format( s).encode(charset) s = s.encode(charset) text = text.replace(s, re_s) return HttpResponse(text, charset=charset) if view_source: if not can_view_mail: return HttpResponse() return render_to_response( "collect_mail/txt.html", { 'content': content.decode('gbk', 'ignore'), }, context_instance=RequestContext(request)) if export: if not can_view_mail: return HttpResponse() response = HttpResponse(content, content_type='text/html') response['Content-Disposition'] = 'attachment; filename="eml.eml"' return response subject = mail_obj.subject return render_to_response("collect_mail/mail_read.html", { 'm': m, 'id': id, 'date': date, 'mail_obj': mail_obj, 'subject': subject, }, context_instance=RequestContext(request)) return HttpResponse(u'no email')
def get_search_data(data, date='', user=''): if not date: date = data.get('date', '') mail_from = data.get('mail_from', '') mail_to = data.get('mail_to', '') state = data.get('state', '') send = data.get('send', '') bounce = data.get('bounce', '') mail_id = data.get('mail_id', '') error_type = data.get('error_type', '') ip = data.get('ip', '') subject = data.get('subject', '') search = data.get('search[value]', '') order_column = data.get('order[0][column]', '') order_dir = data.get('order[0][dir]', '') if not date: date = time.strftime('%Y-%m-%d') return_code = data.get('return_code', '') if not date: date = time.strftime('%Y%m%d') else: date = date.replace('-', '') mail_model = get_mail_model(date) if user: mails = mail_model.objects.filter(customer=user) else: mails = mail_model.objects.all() if mail_from: mails = mails.filter(mail_from__icontains=mail_from) if mail_to: mails = mails.filter(mail_to__icontains=mail_to) if error_type: mails = mails.filter(error_type=error_type) if ip: mails = mails.filter(client_ip=ip) if state: if state == 'analysis': mails = mails.filter(state__in=['check', 'review', 'dispatch']) elif state == 'out_all': mails = mails.filter( state__in=['finished', 'fail_finished', 'bounce']) elif state == 'fail_finished': mails = mails.filter(state__in=['fail_finished', 'bounce']) elif state == 'rejects': mails = mails.filter(state__in=['reject', 'c_reject']) else: mails = mails.filter(state=state) if send == 'success': return_code = '250' elif send == 'fail': return_code = '0' if bounce: mails = mails.filter(bounce_result=bounce) if mail_id: if mail_id == '0': mails = mails.filter(mail_id=0) else: mails = mails.filter(Q(mail_id=mail_id) | Q(id=mail_id)) if subject: mails = mails.filter(subject__icontains=subject) if search: mails = mails.filter( Q(mail_from__icontains=search) | Q(mail_to__icontains=search) | Q(subject__icontains=search)) if return_code: if return_code == '0': mails = mails.exclude(return_code=250) else: mails = mails.filter(return_code=return_code) try: mails.exists() except: mails = [] colums = [ 'mail_from', 'size', 'mail_to', 'id', 'client_ip', 'created', 'subject', 'state' ] if isinstance(mails, query.QuerySet ) and order_column and int(order_column) < len(colums): if order_dir == 'desc': mails = mails.order_by('-%s' % colums[int(order_column)]) else: mails = mails.order_by('%s' % colums[int(order_column)]) return mails