From: Martin Schulze <joey@kuolema.Infodrom.North.DE>
Date: Tue, 9 Jun 1998 22:42:19 +0200
Subject: 04 Add custom header.

Provide a possibility to add custom header fields such as
"X-Loop: This service" or "X-Generated: via cron"
(Closes: Bug#23356, Bug#13756).
---
 def.h    |  1 +
 extern.h |  2 +-
 mail.1   |  8 +++++++-
 main.c   | 24 +++++++++++++++++++++---
 send.c   |  6 +++++-
 5 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/def.h b/def.h
index af2ecce..1905d00 100644
--- a/def.h
+++ b/def.h
@@ -179,6 +179,7 @@ struct headline {
 struct header {
 	struct name *h_to;		/* Dynamic "To:" string */
 	char *h_subject;		/* Subject string */
+	char *h_header;			/* Additional header string */
 	struct name *h_cc;		/* Carbon copies string */
 	struct name *h_bcc;		/* Blind carbon copies */
 	struct name *h_smopts;		/* Sendmail options */
diff --git a/extern.h b/extern.h
index 714f48c..febe362 100644
--- a/extern.h
+++ b/extern.h
@@ -164,7 +164,7 @@ void	 load(char *);
 struct var *
 	 lookup(char *);
 int	 mail (struct name *, struct name *, struct name *, struct name *,
-	       char *);
+	       char *, char *);
 void	 mail1(struct header *, int);
 void	 makemessage(FILE *, int);
 void	 mark(int);
diff --git a/mail.1 b/mail.1
index ebb7f90..be1653e 100644
--- a/mail.1
+++ b/mail.1
@@ -41,6 +41,7 @@
 .Nm mail
 .Bk -words
 .Op Fl dEIinv
+.Op Fl a Ar header
 .Op Fl b Ar bcc-addr
 .Op Fl c Ar cc-addr
 .Op Fl s Ar subject
@@ -62,6 +63,11 @@ with lines replaced by messages.
 .Pp
 The options are as follows:
 .Bl -tag -width Ds
+.It Fl a
+Specify additional header fields on the command line such as "X-Loop:
+foo@bar" etc.  You have to use quotes if the string contains spaces.
+This argument may be specified more than once, the headers will then
+be concatenated.
 .It Fl b Ar bcc-addr
 Send blind carbon copies to
 .Ar bcc-addr .
@@ -1239,7 +1245,7 @@ and are not supported by this implementation of
 .Nm mailx .
 .Pp
 The flags
-.Op Fl bcdEIv
+.Op Fl abcdEIv
 are extensions to the specification.
 .Sh HISTORY
 A
diff --git a/main.c b/main.c
index cdfa7c5..c0553b0 100644
--- a/main.c
+++ b/main.c
@@ -50,6 +50,7 @@ main(int argc, char **argv)
 	int i;
 	struct name *to, *cc, *bcc, *smopts;
 	char *subject;
+	char *header = NULL;
 	char *ef;
 	char nosrc = 0;
 	char *rc;
@@ -89,7 +90,7 @@ main(int argc, char **argv)
 	bcc = NULL;
 	smopts = NULL;
 	subject = NULL;
-	while ((i = getopt(argc, argv, "EIN:b:c:dfins:u:v")) != -1) {
+	while ((i = getopt(argc, argv, "EIN:a:b:c:defins:u:v")) != -1) {
 		switch (i) {
 		case 'u':
 			/*
@@ -120,6 +121,22 @@ main(int argc, char **argv)
 			 */
 			subject = optarg;
 			break;
+
+		case 'a':
+			/*
+			 * Give additional header fields for sending from
+			 * non terminal
+			 */
+			if (header == NULL) {
+				if ((header = (char *)malloc(strlen(optarg)+1)) != NULL)
+					strcpy(header, optarg);
+                        } else {
+				if ((header = (char *)realloc(header, strlen(optarg)+strlen(header)+2)) != NULL) {
+					strcat(header, "\n");
+					strcat(header, optarg);
+				}
+			}
+			break;
 		case 'f':
 			/*
 			 * User is specifying file to "edit" with Mail,
@@ -165,6 +182,7 @@ main(int argc, char **argv)
 			 */
 			bcc = cat(bcc, nalloc(optarg, GBCC));
 			break;
+		case 'e':
 		case 'E':
 			/*
 			 * Don't send messages with an empty body.
@@ -216,7 +234,7 @@ main(int argc, char **argv)
 		rc = "~/.mailrc";
 	load(expand(rc));
 	if (!rcvmode) {
-		mail(to, cc, bcc, smopts, subject);
+		mail(to, cc, bcc, smopts, subject, header);
 		/*
 		 * why wait?
 		 */
@@ -284,7 +302,7 @@ static void
 usage(void)
 {
 
-	fprintf(stderr, "usage: %s [-dEIinv] [-b bcc-addr] [-c cc-addr] "
+	fprintf(stderr, "usage: %s [-dEIinv] [-a header] [-b bcc-addr] [-c cc-addr] "
 	    "[-s subject] to-addr ...\n", __progname);
 	fprintf(stderr, "       %s [-dEIiNnv] -f [name]\n", __progname);
 	fprintf(stderr, "       %s [-dEIiNnv] [-u user]\n", __progname);
diff --git a/send.c b/send.c
index e8084fd..e79d9dc 100644
--- a/send.c
+++ b/send.c
@@ -280,12 +280,13 @@ statusput(struct message *mp, FILE *obuf, char *prefix)
  */
 int
 mail(struct name *to, struct name *cc, struct name *bcc, struct name *smopts,
-     char *subject)
+     char *subject, char *header)
 {
 	struct header head;
 
 	head.h_to = to;
 	head.h_subject = subject;
+	head.h_header = header;
 	head.h_cc = cc;
 	head.h_bcc = bcc;
 	head.h_smopts = smopts;
@@ -306,6 +307,7 @@ sendmail(void *v)
 
 	head.h_to = extract(str, GTO);
 	head.h_subject = NULL;
+	head.h_header = NULL;
 	head.h_cc = NULL;
 	head.h_bcc = NULL;
 	head.h_smopts = NULL;
@@ -510,6 +512,8 @@ puthead(struct header *hp, FILE *fo, int w)
 	if (hp->h_bcc != NULL && w & GBCC)
 		fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
 */
+	if (hp->h_header != NULL && w)
+		fprintf(fo, "%s\n", hp->h_header), gotcha++;
 	if (gotcha && w & GNL)
 		(void)putc('\n', fo);
 	return(0);
