def compose(request): character = request.user.character_set.get(alive=True) new_mail = MailForm() if request.POST: new_mail = MailForm(request.POST) if new_mail.is_valid(): send_mail(request.POST, character) url = reverse('mail folder', kwargs={"folder_name": "inbox"}) return HttpResponseRedirect(url) return render(request, "compose.html", {"new_mail": new_mail})
def testSendAndReply(self): user = CustomUser.objects.create(email="*****@*****.**", username="******", needs_moderation=False) data = QueryDict("").copy() SUBJECT = "Test Send And Reply" data.update({'mto':'*****@*****.**', 'mfrom':user.email, 'subject':SUBJECT, 'message':'Hi'}) form = MailForm(None, data) self.assertFalse(form.is_valid()) data.update({'mto':'*****@*****.**', 'mfrom':user.email, 'subject':SUBJECT, 'name': 'One Baz', 'message':'Hi'}) form = MailForm(None, data) self.assertTrue(form.is_valid()) mail_obj = form.save() c = Client() response = c.get('/process', follow=True) self.assertContains(response, SUBJECT) self.assertEquals(mail.outbox[0].subject, SUBJECT) message_id = mail.outbox[0].extra_headers['Message-ID'] REPLY_SUBJECT = "Re: %s" % SUBJECT response = EmailMessage(REPLY_SUBJECT, "Hello back", "*****@*****.**", [user.proxy_email], headers={'In-Reply-To':'<%s>' % message_id}) response.send() self.assertEquals(mail.outbox[-1].subject, REPLY_SUBJECT) reply_fp = StringIO(mail.outbox[-1].message().as_string()) response = make_response_from_file(reply_fp) self.assertTrue(response) # this was like a different person at the same company # replying to the message -- let's check that while we're here couser1 = CustomUser.objects.get(email='*****@*****.**') couser2 = CustomUser.objects.get(email='*****@*****.**') self.assertEqual(couser1.organisation, couser2.organisation) # two emails should have been created, threaded together mails = Mail.objects.order_by('created') self.assertEqual(mails[0].childwalk().next(), mails[1]) # and an email should have been sent to the original sender, # with a proxied reply-to address self.assertEqual(mail.outbox[-1].to[0], user.email) self.assertEqual(mail.outbox[-1].from_email, couser2.email) self.assertEqual(mail.outbox[-1].extra_headers['Reply-To'], couser2.proxy_email)
def view_mail(request, mail_id): mail = get_object_or_404(Mail, pk=mail_id) #change mail to read if mail.read == False: mail.read = True mail.save() #mailoptions actions = [["reply", "reply"], ["forward", "forward"], ["archive", "move to archive"], ["trash", "move to trash"]] if request.POST: result = mail_options(request.POST, mail) if result: if type(result) is type({}): new_mail = MailForm(result) return render(request, "compose.html", {"new_mail": new_mail}) else: url = reverse('mail folder', kwargs={"folder_name": "inbox"}) return HttpResponseRedirect(url) else: return render(request, "mail.html", {"mail": mail, "actions": actions}) return render(request, "mail.html", {"mail": mail, "actions": actions})
def testSimpleMailForm(self): data = QueryDict("").copy() data.update({'mto':'*****@*****.**', 'mfrom':'*****@*****.**', 'subject':'Hello', 'name':'Two Baz', 'message':'Hi'}) form = MailForm(None, data) self.assertTrue(form.is_valid()) mail = form.save() self.assertEquals(mail.mto.email, data['mto']) self.assertEquals(mail.mfrom.email, data['mfrom']) # sending the same message twice is OK data.update({'mto':'*****@*****.**', 'mfrom':'*****@*****.**', 'subject':'Hello', 'message':'Hi'}) form = MailForm(None, data) self.assertTrue(form.is_valid()) mail = form.save() self.assertFalse(mail.approved) # now try with a userfo who's unmoderated user = CustomUser.objects.create(email="*****@*****.**", username="******", needs_moderation=False) data.update({'mto':'*****@*****.**', 'mfrom':'*****@*****.**', 'subject':'Hello', 'message':'Hi'}) form = MailForm(None, data) self.assertTrue(form.is_valid()) mail = form.save() self.assertTrue(mail.approved)