簡體   English   中英

使用mailx和bash腳本解析郵件的正文

[英]Parse mail's body using mailx and bash scripting

我試圖使用電子郵件自動化我的工作的某些部分。是否有任何方法已經可以使用mailx和bash我可以用來提取郵件的正文?

如果這是通過類似sendmail的MTA發送到本地用戶帳戶的郵件,那么您可以使用procmail在發送郵件時對其進行解析。

在我使用的系統上,sendmail會檢查~/.forward文件,所以我在~username / .forward中有這個

# pipe incoming mail to procmail
# ref: http://www.panix.com/~elflord/unix/procmail.html
# ref: http://porkmail.org/era/procmail/mini-faq.html#forward
"|IFS=' ' && p=/usr/local/bin/procmail && test -x $p && exec $p -f- || exit 75 #username"

然后, ~username/.procmailrc包含:

# procmail tutorial: http://tldp.org/LDP/LG/issue14/procmail.html

PATH=/usr/local/bin:/bin:/usr/bin
MAILDIR=$HOME/Mail
DEFAULT=$HOME/Mail/inbox
LOGFILE=$HOME/procmail.`date +%Y-%m`.log
SHELL=/usr/bin/ksh

MY_XLOOP='X-Loop: username@hostname.subdomain.example.com'
MY_RECIPIENT='mailing.list@example.com'


#############################################################################
# if the email comes from the client with a specific Subject,
# send a copy of the message to the processing script, and 
# carry on with the next recipe

:0c
* ^From:.*@clientdomain\.invalid
* ^Subject:.*Account.*(Request|Access|Approval)
| $HOME/bin/process_account_request_email.pl | \
  mailx -s "Account request results" $MY_RECIPIENT


#############################################################################
# forward all mail to mailing list
:0
* ! ^$MY_XLOOP
{
    # add a header
    # 'f' = filter: pass message to program and continue processing results 
    # 'h' = pass message headers to program
    # 'w' = wait for program to return
    :0fhw
    | formail -A "$MY_XLOOP"

    # then forward the message
    # 'c' = send a copy to recipient and continue processing
    :0c
    ! $MY_RECIPIENT
}

# if we get here, then the message has an X-Loop header.
# let it fall into $DEFAULT

來自procmail包的formail很好地完成了另一個答案中所述的工作,但是這里是我認為直接回答問題的總結:

$ sudo apt-get install procmail

$ cat test.eml | formail -x To
 test@mydomain.com

$ cat test.eml | formail -x Subject
 hello

$ cat test.eml | formail -x Content
 multipart/alternative; boundary="f403043eea78e8658a0554677278"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM