Repository: translate/pootle

Issue:
Modify outbound email headers to prevent mail from being bounced
We currently use `From: "{{user.display_name}}" <{{user.email}}>` header in the emails we send from Pootle. This doesn't work with all servers. For example, it doesn't work with Yahoo emails as their server bounces our emails with the following error message: `Unauthenticated email from yahoo.com is not accepted due to domain's 550-5.7.1 DMARC policy.`

So let's make sure all outbound emails have the following `From:` and `Reply-to:` headers:

```
From: "{{user.display_name}}" <{{settings.DEFAULT_FROM_EMAIL}}>
Reply-to: "{{user.display_name}}" <{{user.email}}>
```



Discussion:
Note that in order to send emails this way, it'd be necessary to patch/replace the contact form package, as it's using Django's [`send_mail()`](https://docs.djangoproject.com/en/1.7/topics/email/#send-mail) function, and this works fine for most of the cases but it lacks the ability to specify advanced features such as extra headers.


Resolving patch:
diff --git a/pootle/apps/contact/forms.py b/pootle/apps/contact/forms.py
--- a/pootle/apps/contact/forms.py
+++ b/pootle/apps/contact/forms.py
@@ -16,6 +16,7 @@
 from contact_form.forms import ContactForm as OriginalContactForm
 
 from pootle.core.forms import MathCaptchaForm
+from pootle.core.mail import send_mail
 
 
 class ContactForm(MathCaptchaForm, OriginalContactForm):
@@ -56,6 +57,15 @@ def from_email(self):
     def recipient_list(self):
         return [settings.POOTLE_CONTACT_EMAIL]
 
+    def save(self, fail_silently=False):
+        """Build and send the email message."""
+
+        kwargs = self.get_message_dict()
+        kwargs["headers"] = {"Reply-To": kwargs["from_email"]}
+        kwargs["from_email"] = settings.DEFAULT_FROM_EMAIL
+        send_mail(fail_silently=fail_silently, **kwargs)
+
+
 # Alters form's field order. Use `self.field_order` when in Django 1.9+
 ContactForm.base_fields = OrderedDict(
     (f, ContactForm.base_fields[f])
diff --git a/pootle/core/mail.py b/pootle/core/mail.py
new file mode 100644
--- /dev/null
+++ b/pootle/core/mail.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) Pootle contributors.
+#
+# This file is a part of the Pootle project. It is distributed under the GPL3
+# or later license. See the LICENSE file for a copy of the license and the
+# AUTHORS file for copyright and authorship information.
+
+from django.conf import settings
+from django.core.mail import get_connection, EmailMultiAlternatives
+
+
+def send_mail(subject, message, from_email, recipient_list,
+              fail_silently=False, auth_user=None, auth_password=None,
+              connection=None, html_message=None, headers=None):
+    """Override django send_mail function to allow use of custom email headers.
+    """
+
+    connection = connection or get_connection(username=auth_user,
+                                              password=auth_password,
+                                              fail_silently=fail_silently)
+
+    mail = EmailMultiAlternatives(subject, message,
+                                  from_email, recipient_list,
+                                  connection=connection, headers=headers)
+
+    if html_message:
+        mail.attach_alternative(html_message, 'text/html')
+
+    return mail.send()


Test patch: