---
 data/org.gnome.evolution.mail.gschema.xml.in |    5 +
 libemail-engine/e-mail-session-utils.c       |   89 ++++++++++++++++++++++++++-
 2 files changed, 92 insertions(+), 2 deletions(-)

Index: b/libemail-engine/e-mail-session-utils.c
===================================================================
--- a/libemail-engine/e-mail-session-utils.c	2013-04-08 13:59:38.000000000 +0200
+++ b/libemail-engine/e-mail-session-utils.c	2013-04-08 14:07:35.000000000 +0200
@@ -15,16 +15,21 @@
  * License along with the program; if not, see <http://www.gnu.org/licenses/>
  *
  */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 
+#include <sys/param.h>  /* for MAXHOSTNAMELEN */
+#ifndef MAXHOSTNAMELEN
+#define MAXHOSTNAMELEN 1024
+#endif
+
 #include "e-mail-session-utils.h"
 
 #include <glib/gi18n-lib.h>
 #include <libedataserver/libedataserver.h>
 
 #include <libemail-engine/e-mail-folder-utils.h>
 #include <libemail-engine/e-mail-utils.h>
 #include <libemail-engine/mail-tools.h>
@@ -789,16 +794,97 @@
 	null = camel_stream_null_new ();
 	camel_data_wrapper_write_to_stream_sync (CAMEL_DATA_WRAPPER (message), null, cancellable, NULL);
 	res = CAMEL_STREAM_NULL (null)->written;
 	g_object_unref (null);
 
 	return res;
 }
 
+static gchar *
+mail_session_get_hostname (void)
+{
+	gchar host[MAXHOSTNAMELEN];
+	const gchar *name;
+	gint retval;
+	struct addrinfo *ai = NULL, hints = { 0 };
+	static gchar *cached_hostname = NULL;
+
+	if (!cached_hostname) {
+		retval = gethostname (host, sizeof (host));
+		if (retval == 0 && *host) {
+			hints.ai_flags = AI_CANONNAME;
+			ai = camel_getaddrinfo (
+				host, NULL, &hints, NULL, NULL);
+			if (ai && ai->ai_canonname)
+				name = ai->ai_canonname;
+			else
+				name = host;
+			cached_hostname = g_strdup (name);
+			if (ai)
+				camel_freeaddrinfo (ai);
+		} else
+			return NULL;
+	}
+	return cached_hostname;
+}
+
+static void
+e_mail_session_set_from_address (EMailSession *session,
+				 CamelMimeMessage *message,
+				 CamelAddress *from)
+{
+	GSettings *settings;
+	gboolean use_hostname;
+	gchar *hostname = NULL;
+
+	settings = g_settings_new ("org.gnome.evolution.mail");
+	use_hostname = g_settings_get_boolean (settings, "send-with-hostname-from");
+	g_object_unref (settings);
+
+	if (use_hostname)
+		hostname = mail_session_get_hostname ();
+
+	if (!hostname) {
+		/* derive the SMTP return-path from the 'From' header of the message */
+		const CamelInternetAddress *addr;
+
+		addr = camel_mime_message_get_from (message);
+		camel_address_copy (from, CAMEL_ADDRESS (addr));
+	} else {
+		ESourceRegistry *registry;
+		ESource *source = NULL;
+		const gchar *extension_name;
+		ESourceMailIdentity *extension;
+		gchar *who;
+		gchar *res;
+
+		registry = e_mail_session_get_registry (session);
+		source = e_source_registry_ref_default_mail_identity (registry);
+
+		extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
+		extension = e_source_get_extension (source, extension_name);
+
+		who = e_source_mail_identity_dup_address (extension);
+		if (!who)
+			who = g_strdup ("fake"); /* not sure what we should do here */
+		else {
+			gchar *p;
+			p = strchr (who, '@');
+			if (p != NULL)
+				*p = '\0';
+		}
+		res = g_strconcat (who, "@", hostname, NULL);
+		g_free (who);
+		g_object_unref (source);
+		camel_address_decode (from, res);
+		g_free (res);
+	}
+}
+
 void
 e_mail_session_send_to (EMailSession *session,
                         CamelMimeMessage *message,
                         gint io_priority,
                         GCancellable *cancellable,
                         CamelFilterGetFolderFunc get_folder_func,
                         gpointer get_folder_data,
                         GAsyncReadyCallback callback,
@@ -913,18 +999,17 @@
 		type = CAMEL_RECIPIENT_TYPE_RESENT_BCC;
 		addr = camel_mime_message_get_recipients (message, type);
 		camel_address_cat (recipients, CAMEL_ADDRESS (addr));
 
 	} else {
 		const CamelInternetAddress *addr;
 		const gchar *type;
 
-		addr = camel_mime_message_get_from (message);
-		camel_address_copy (from, CAMEL_ADDRESS (addr));
+		e_mail_session_set_from_address (session, message, from);
 
 		type = CAMEL_RECIPIENT_TYPE_TO;
 		addr = camel_mime_message_get_recipients (message, type);
 		camel_address_cat (recipients, CAMEL_ADDRESS (addr));
 
 		type = CAMEL_RECIPIENT_TYPE_CC;
 		addr = camel_mime_message_get_recipients (message, type);
 		camel_address_cat (recipients, CAMEL_ADDRESS (addr));
Index: b/data/org.gnome.evolution.mail.gschema.xml.in
===================================================================
--- a/data/org.gnome.evolution.mail.gschema.xml.in	2013-04-08 13:59:38.000000000 +0200
+++ b/data/org.gnome.evolution.mail.gschema.xml.in	2013-04-08 13:59:38.000000000 +0200
@@ -531,16 +531,21 @@
       <_summary>Save directory</_summary>
       <_description>Directory for saving mail component files.</_description>
     </key>
     <key name="composer-current-folder" type="s">
       <default>''</default>
       <_summary>Composer load/attach directory</_summary>
       <_description>Directory for loading/attaching files to composer.</_description>
     </key>
+    <key name="send-with-hostname-from" type="b">
+      <default>false</default>
+      <_summary>Use the hostname as FROM in the envelope</_summary>
+      <_description>Whether to use the local hostname when creating the FROM header used as SMTP reverse-path. When false or when the hostname is not available, the reverse-path is derived from the message 'From' header.</_description>
+    </key>
     <key name="send-recv-on-start" type="b">
       <default>true</default>
       <_summary>Check for new messages on start</_summary>
       <_description>Whether to check for new messages when Evolution is started. This includes also sending messages from Outbox.</_description>
     </key>
     <key name="send-recv-all-on-start" type="b">
       <default>false</default>
       <_summary>Check for new messages in all active accounts</_summary>
