在Linux中从命令行转发电子邮件

我有我的Maildir收到与Postfix服务器(Ubuntu)的电子邮件文件。 是否有一个实用工具可以将选定的电子邮件文件转发到指定的电子邮件地址? 就像是:

cat emailfile | utility [email protected] 

我尝试使用邮件命令,但它只是将整个文件内容(包括所有技术标题信息)作为纯文本发送,看起来不太好。

 cat emailfile | mail -s subject [email protected] 

更新

对不起,没有具体。 我想要的是从一个shell脚本转发一个电子邮件文件,没有附件,但删除所有的头和元数据,并以人性化的方式呈现。 就像在gmail中一样,当你点击“转发”时,它会自动parsing电子邮件,在上面添加“转发邮件”文本,然后放置正文文本消息。 我知道我可以自己parsing电子邮件文件,并构build一个新的电子邮件,但我认为有一个实用工具,可以节省几个小时。

有比一个更多的可能性。

  1. 这个工具叫做sendmail。 cat emailfile | sendmail -f [email protected] cat emailfile | sendmail -f [email protected] 。 也许你之前必须重写邮件,因为这不会“转发”邮件,而是“发送”邮件。
  2. 在Postfix中做到这一点。 您可以使用Postfix中已有的许多可能性向本地用户和其他人(本地和/或远程)发送邮件。 线索: *_alias_maps

mail [email protected] < mailfile使电子邮件的正文成为文件的内容。 如果这不适合你,那么也许你将不得不写你自己的。

这是来自Python 2.7库文档 :

 # 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() 

这里唯一真正的改变是你会使用类email.mime.image.MIMEApplication而不是MIMEImage …并且当然将to,from和subject字段改为适当的。