From f73b8a1b78f16766e10f45ea31120bf8f1420dda Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Fri, 23 Oct 2020 09:30:52 +0200
Subject: [PATCH] Add extra MIME headers to ComposedMessage

---
 apimail/api/serializers.py                    |  4 +++-
 .../commands/mailgun_send_messages.py         |  5 +++++
 .../0025_composedmessage_headers_added.py     | 19 +++++++++++++++++++
 apimail/models/composed_message.py            |  2 ++
 .../assets/vue/components/MessageComposer.vue | 12 ++++++++++++
 5 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 apimail/migrations/0025_composedmessage_headers_added.py

diff --git a/apimail/api/serializers.py b/apimail/api/serializers.py
index 0d186486d..2bdfd8693 100644
--- a/apimail/api/serializers.py
+++ b/apimail/api/serializers.py
@@ -59,8 +59,10 @@ class ComposedMessageSerializer(serializers.ModelSerializer):
         model = ComposedMessage
         fields = [
             'uuid', 'author', 'created_on', 'status',
-            'from_account', 'from_email', 'to_recipient', 'cc_recipients', 'bcc_recipients',
+            'from_account', 'from_email',
+            'to_recipient', 'cc_recipients', 'bcc_recipients',
             'subject', 'body_text', 'body_html',
+            'headers_added',
             'attachment_files', 'api_responses'
         ]
 
diff --git a/apimail/management/commands/mailgun_send_messages.py b/apimail/management/commands/mailgun_send_messages.py
index 790fffcf6..9aef99c15 100644
--- a/apimail/management/commands/mailgun_send_messages.py
+++ b/apimail/management/commands/mailgun_send_messages.py
@@ -28,6 +28,11 @@ class Command(BaseCommand):
             if msg.bcc_recipients:
                 data['bcc'] = msg.bcc_recipients
 
+            # RFC 2822 MIME headers:
+            for key, val in msg.headers_added.items():
+                h_key = "h:%s" % key
+                data[h_key] = val
+
             files = [('attachment', (att.data['name'], att.file.read()))
                      for att in msg.attachment_files.all()]
 
diff --git a/apimail/migrations/0025_composedmessage_headers_added.py b/apimail/migrations/0025_composedmessage_headers_added.py
new file mode 100644
index 000000000..2d8836149
--- /dev/null
+++ b/apimail/migrations/0025_composedmessage_headers_added.py
@@ -0,0 +1,19 @@
+# Generated by Django 2.2.16 on 2020-10-23 06:15
+
+import django.contrib.postgres.fields.jsonb
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('apimail', '0024_auto_20201017_1658'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='composedmessage',
+            name='headers_added',
+            field=django.contrib.postgres.fields.jsonb.JSONField(default=dict),
+        ),
+    ]
diff --git a/apimail/models/composed_message.py b/apimail/models/composed_message.py
index 491d6ea7a..654f911d2 100644
--- a/apimail/models/composed_message.py
+++ b/apimail/models/composed_message.py
@@ -64,6 +64,8 @@ class ComposedMessage(models.Model):
     body_text = models.TextField(blank=True)
     body_html = models.TextField(blank=True)
 
+    headers_added = JSONField(default=dict)
+
     attachment_files = models.ManyToManyField(
         'apimail.AttachmentFile',
         blank=True)
diff --git a/apimail/static/apimail/assets/vue/components/MessageComposer.vue b/apimail/static/apimail/assets/vue/components/MessageComposer.vue
index bf1442d5e..cdfdbda6c 100644
--- a/apimail/static/apimail/assets/vue/components/MessageComposer.vue
+++ b/apimail/static/apimail/assets/vue/components/MessageComposer.vue
@@ -330,6 +330,7 @@ export default {
 		bcc_recipients: [],
 		subject: '',
 		body_html: '',
+		headers_added: {},
 		attachments: [],
 	    },
 	    from_account_accesses: [],
@@ -404,6 +405,7 @@ export default {
 			  'subject': this.form.subject,
 			  'body_text': this.form.body_html, // TODO: remove; only html emails
 			  'body_html': this.sanitized_body_html,
+			  'headers_added': this.form.headers_added,
 			  'attachment_uuids': attachment_uuids
 	    	      })
 	    	  })
@@ -450,6 +452,16 @@ export default {
       	else if (this.originalmessage) {
 	    this.form.from_account = this.accountSelected.pk
       	    this.form.body_html = ('<br><br><blockquote>')
+	    this.form.headers_added['Reply-To'] = this.accountSelected.email
+	    this.form.headers_added['In-Reply-To'] = this.originalmessage.data['Message-Id']
+	    if (this.originalmessage.data['References']) {
+		this.form.headers_added['References'] = (
+		    this.originalmessage.data['References'] + " "
+			+ this.originalmessage.data['Message-Id'])
+	    }
+	    else {
+		this.form.headers_added['References'] = this.originalmessage.data['Message-Id']
+	    }
 	    if (this.action == 'reply') {
       		this.form.to_recipient = this.originalmessage.data.sender
 		this.form.cc_recipients = this.originalmessage.data.recipients.split(',')
-- 
GitLab