def get_plain(part): this_charset = part.get_content_charset() this_sub_part = part.get_payload(decode=True) if this_charset: if part.get_content_subtype() == 'html': this_sub_part = html2plaintext(smart_text(quopri.decodestring(this_sub_part), encoding=this_charset, errors='ignore'), encoding='utf-8') elif part.get_content_maintype() == 'text': this_sub_part = smart_text(quopri.decodestring(this_sub_part), encoding=this_charset, errors='ignore') else: if part.get_content_subtype() == 'html': this_sub_part = html2plaintext(smart_text(quopri.decodestring(this_sub_part), errors='ignore'), encoding='utf-8') elif part.get_content_maintype() == 'text': this_sub_part = smart_text(quopri.decodestring(this_sub_part), errors='ignore') return this_sub_part.replace("\n\n", "\n").replace("\r\n\r\n", "\r\n")
def form_valid(self, form): from django.core.mail import EmailMultiAlternatives from fetchmyguest.utils.html2plaintext import html2plaintext import HTMLParser h = HTMLParser.HTMLParser() message = self.get_object() subject, to = 'Re: ' + message.subject, message.lead.customer.email html_content = h.unescape(form.cleaned_data['message']) text_content = html2plaintext(html_content) msg = EmailMultiAlternatives(subject, text_content, [to]) msg.attach_alternative(html_content, "text/html") sent_message = message.reply(msg) sent_message.lead = message.lead sent_message.save() return super(ReplyEmail,self).form_valid(form)
def post(self, request, *args, **kwargs): serializer = ReplySerializer(data=request.DATA) def record_send_message(email_msg, related_lead, sending_mailbox): """ Records and sends a message with metadata for Mandrill. :param email_msg: :param related_lead: :param sending_mailbox: :return: """ out_msg = sending_mailbox.record_outgoing_message( email.message_from_string( email_msg.message().as_string() ) ) out_msg.lead = related_lead out_msg.save() email_msg = set_metadata(email_msg, out_msg) email_msg.send() return out_msg if serializer.is_valid(): from fetchmyguest.utils.html2plaintext import html2plaintext import HTMLParser h = HTMLParser.HTMLParser() html_content = h.unescape(serializer.data['message']) text_content = html2plaintext(html_content, encoding='utf-8') if int(kwargs['msg_pk']) != 0: message = get_object_or_404(Message, pk=int(kwargs['msg_pk'])) cc = [] if serializer.data['cc']: for address in serializer.data['cc'].split(','): cc.append( rfc822.parseaddr( address )[1] ) # Forwarded message if serializer.data['fwd']: to = [] for address in serializer.data['fwd'].split(','): to.append( rfc822.parseaddr( address )[1] ) subject = 'Fwd: ' + message.subject msg = EmailMessage(subject=subject, body=message.get_text_body(), to=to, cc=cc) if message.mailbox.from_email: msg.from_email = message.mailbox.from_email else: msg.from_email = settings.DEFAULT_FROM_EMAIL record_send_message(msg, message.lead, message.mailbox) # Reply message else: subject, to = 'Re: ' + ' '.join(message.subject.split()), message.lead.customer.email msg = EmailMultiAlternatives(subject=subject, body=text_content, to=[to], cc=cc) msg.attach_alternative(html_content, "text/html") new_msg = message.prepare_reply(msg) record_send_message(new_msg, message.lead, message.mailbox) else: lead = get_object_or_404(Lead, pk=int(kwargs['lead_pk'])) if serializer.data['subject']: subject = serializer.data['subject'] else: try: lp = lead.leadproperty_set.filter( Q(status=LeadProperty.REQUESTED) | Q(status=LeadProperty.NOTAVAILABLE) )[0] subject = 'Property Request for {0}'.format(lp.property.title) except: subject = 'Property Request' msg = EmailMultiAlternatives( subject=subject, body=text_content, to=[lead.customer.email], bcc=[lead.agency.email] ) msg.attach_alternative(html_content, "text/html") record_send_message(msg, lead, lead.agency.mailbox) return Response(serializer.data, status=201) else: return Response(serializer.errors, status=400)