Forward an email from command line in Linux
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 [email protected]
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 [email protected]
Update
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.
There are more possibilities than one.
- This utility is called sendmail.
cat emailfile | sendmail -f [email protected]
. Maybe you have to rewrite the mail before, as this does not "forward" the mail, but instead "send" the mail. - 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
mail [email protected] < 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
COMMASPACE = ', '
# 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())
fp.close()
msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()
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.