Showing results for 
Search instead for 
Did you mean: 

Forward mail file with mail/mailx/sendmail

Forward mail file with mail/mailx/sendmail

For scripting purposes, how can I take a raw mail file and use mail/mailx/sendmail to forward it to an email address. When I say "raw mail file" I mean a copy of a mail message saved to a file, so it has all the header information and such... but I don't want all of that information included in the body of the mail message when it's sent to the recipient, and I want to preserve the subject in said mail file.

So just doing:

cat mail.file | mailx admin@server

doesn't work because the headers are included in the body and the subject will be blank (or a different subject if I use the -s option).

Basically what I'm looking for would be a way to script the way elm does a "forward mail" command, except elm only does that in interactive mode, unfortunately.
Rita C Workman
Honored Contributor

Re: Forward mail file with mail/mailx/sendmail

There are a plethora of threads dealing with mailing files as attachments. I just used "mail attached files mime" and got plenty.

Now my favorite way to do this, since I can send any kind of file (msword, raw text, tiff image, etc.) and script it is:

I have two files.
1. message.file will print in the body of the email
2. file-to-mail will show up as an attachment!

Create a /path/dummy file that states
[include /path/message.file text/plain base64]
[include /path/file-to-mail text/plain base64]

I don't know who told you that you can't script using elm ???'s always worked for me.

In my script to mail is the following line:

elm -s " Sending the file " < /path/dummy

Just one the threads and you will find plenty more!


Re: Forward mail file with mail/mailx/sendmail

Rita, thank you for the response.

I'm not looking for a way to send the file as an attachment. I want to treat the file as an SMTP mail message that I then forward to an email address. I don't have control over the mail file itself or its contents... it gets generated (as an email message) on another system and then transferred as a raw file to my system. The eventual recipient of the message doesn't need to see all of the mail headers and such in the message that they read. They just want the body of the message and the exact same subject as is.

As far as using elm, yes you can use it in scripts, but the "foward mail" feature is only available in interactive mode, so I can't take advantage of that via command line. If I could, that would solve my problem.

To make it more clear, look at this example of scripting a reply to a mail message saved as a file:

mailx -f mail.file <R
This is an automated response, please do not reply.

Using the above, I'm able to reply to a message saved in mail.file and the return messaged has the same subject and just the body of the message that's being replied to. If there were a similar command to do a foward, then I'd be set, but there's no such thing with mailx, unfortunately.
Honored Contributor

Re: Forward mail file with mail/mailx/sendmail

As you noted, because the mail file already includes the headers, piping it to mailx does not work... but how about piping it directly to sendmail?

But before doing that, we should examine the "raw mail file" a bit further.

- Is it certain that one file contains only one email message, and not several?

- Is the file in Berkeley mailbox format, or an actual raw mail file?

If your file is in Berkeley mailbox format and might contain multiple messages, the "formail" command that comes with the free "procmail" software package would be useful:

If what you have really is a raw mail file, I think simply piping it to sendmail should do the trick:

sendmail admin@server < mail.file

It should preserve just about all the headers, including the subject.

If it is a Berkeley mailbox with just a single message, you'll need to remove the "From " pseudo-header that indicates the beginning of the message, and probably also un-escape any escaped ">From " lines in the message body. (Note: "From", *not* "From:". The difference is important.)

Off the top of my head, I'd try something like this:

grep -v "^From " mail.file \
| sed -e 's/^>From /From /g' \
| sendmail admin@server

If it's possible the file contains multiple messages, the previous solution could be made into a script or a shell function:

---- script ----
# NOTE: mail message should be piped in
grep -v "^From " \
| sed -e 's/^>From /From /g' \
| sendmail "$@"
---- end script ----

Then "formail -s" could be used to split the file into multiple messages and send each of them:

cat mail.file | formail -s admin@server

Disclaimer: not tested, just trying to give you some ideas.


Re: Forward mail file with mail/mailx/sendmail

Matti, thank you for the response.

The messages come to my server as individual files (one file per message) and they are indeed stored in the Berkeley format that you referred to (each message beginning with "From "), however redirecting each file into sendmail did work, somewhat. The messages arrive in the recipient's mailbox with the original subject and just the original body, so that at least was progress.

However, it wasn't exactly the same as forwarding a message because there was no indication of where the original message came from and who sent it, at least not without examining the (now hidden) headers. It acted more like relaying the message than forwarding it, but it was a very good suggestion and I give you credit for that.

I eventually was able to solve the problem using the Linux version of mailx called "nail". Surprisingly enough it does include the "forward" command that's missing from the HP-UX version. I wasn't happy about switching to a Linux server to accomplish this, but it's the only thing I found that worked.

Re: Forward mail file with mail/mailx/sendmail

Problem solved using "nail" on a Linux server and the following syntax:

echo "forward admin@server" | nail -f mail.file