I have email files in my Maildir received with postfix server (Ubuntu). Is there a utility that can forward a selected email file to the specified email address? Something like:

cat emailfile | utility to@address.com

I tried to use mail command but it just sent the entire file contents including all technical header information as a plain text, which does not look nice.

cat emailfile | mail -s subject to@address.com


Sorry for not being specific. What I wanted is to forward an email file from a shell script, without attachments, but removing all the header and metadata and presenting it in human-friendly way. Like in gmail, when you click 'forward' it automatically parses the email nicely, adds "forwarded message" text on the top and then puts the body text message. I know I can parse the email file myself and construct a new email, but I thought there was a utility that could save me couple hours.

  • Always or just one-time?
    – mailq
    Aug 16 '11 at 17:41
  • Can you tell us more about what you need to do here (do you want to forward it as an attachment, or just forward the body, or what)? There are a few ways to do this that come to mind, some uglier than others.
    – voretaq7
    Aug 16 '11 at 22:04

There are more possibilities than one.

  1. This utility is called sendmail. cat emailfile | sendmail -f to@address.com. Maybe you have to rewrite the mail before, as this does not "forward" the mail, but instead "send" the mail.
  2. Do this in Postfix itself. You could use the many possibilities already present in Postfix to send a mail to the local user and additionally to others (locally and/or remote). Clue: *_alias_maps
  • 2
    -f in standard sendmail command line options does not do what they want here (it sets the FROM information of what it's sending, and the From: header if missing) -- They want to change the TO (recipient) which as you noted would require modifying the input to change the recipient and strip out OTHER recipients (extra To: & CC: headers).
    – voretaq7
    Aug 16 '11 at 18:20
  • 2
    Yes sorry. I would always prefer option 2. And when I do it, I use mutt as it allows to send the mail as an attachment of a new mail. This is "real" forwarding instead of the mentioned workarounds.
    – mailq
    Aug 16 '11 at 21:34

mail to@address.com < mailfile makes the body of the email be the contents of the file. If that is not working for you, then perhaps you will have to write your own.

This is taken from the Python 2.7 library documentation:

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart


# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())

The only real change here is that you would use class email.mime.image.MIMEApplication instead of MIMEImage ... and of course change the to, from, and subject fields to something appropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.