bash dump raw email body txt from Maildir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash dump raw email body txt from Maildir
# 1  
Old 12-01-2011
bash dump raw email body txt from Maildir

I'm setting up a cronjob that will hopefully dump the contents of a email delivered to Maildir/new/ to /home/user/raw.txt (and then delete that email)

I could manually remove the bottom line of the raw email, then place the contents in the next line up in the /home/user/raw.txt, but it seems to me there's probably a more elegant way to script mutt to do it (which would also parse html email if the user forgot), or mail/pine/whatever, plus raw sed/awk/whatever wouldn't seem to be very error tolerant, is there a better way to do this? This is for an automated weather report I want to push to a website via a junk email account I've created on the webserver (that part is working fine).

I guess if I could figure out how to get procmail to do it directly, that'd work too. Right now I'm using (in postfix)
Code:
mailbox_command = /usr/bin/procmail -a "$EXTENSION" DEFAULT=$HOME/Maildir/ MAILDIR=$HOME/Maildir


Last edited by unclecameron; 12-01-2011 at 02:52 AM..
# 2  
Old 12-01-2011
I used procmail only as a mail filter - I don't know if there is any editing capabilities oir somethign similar.
If you want to cut and paste some line of your raw email, best just post a snippet using code tags so we can maybe assist with using something like sed/awk etc.
# 3  
Old 12-01-2011
cat /home/user/Maildir/new/1322721060.6211_0.www
Code:
Return-Path: <user1@blah.com>
X-Original-To: blah@blah.com
Delivered-To: blah@blah.com
Received: from smtp178.blah.com (smtp178.blah.com [207.97.245.178])
    by hostname.com (Postfix) with ESMTPS id C10893DD2F
    for <blah@blah.com>; Wed, 30 Nov 2011 22:30:59 -0800 (PST)
Received: from smtp27.blah.com (localhost.localdomain [127.0.0.1])
    by smtp27.blah.com (SMTP Server) with ESMTP id 04736118FC4
    for <blah@blah.com>; Thu,  1 Dec 2011 01:30:54 -0500 (EST)
X-SMTPDoctor-Processed: csmtpprox 2.7.4
Received: from localhost (localhost.localdomain [127.0.0.1])
    by smtp27.blahcom (SMTP Server) with ESMTP id F3BCC118FC5
    for <blah@blah.com>; Thu,  1 Dec 2011 01:30:53 -0500 (EST)
X-Virus-Scanned: OK
Received: by smtp27.blah.com (Authenticated sender: blah-AT-blah.com) with ESMTPSA id 78556118FC4
    for <blah@user.com>; Thu,  1 Dec 2011 01:30:53 -0500 (EST)
From: "blah <blah1@user.com>
To: <blah@user.com>
Subject: test4
Date: Wed, 30 Nov 2011 22:30:52 -0800
Message-ID: <00bc01ccaff2$c6b88670$54299350$@com>
MIME-Version: 1.0
Content-Type: text/plain;
    charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: Acyv8sWFTCt6aRGGS22J2iL97tXyVw==
Content-Language: en-us

This is the text I need, it may possibly span two lines or more
but it will always have a blank line above and below it.

the closing code tag truncated the blank line after the text I need, but it's really there.

I keep thinking mailx/mutt should somehow be able to dump the body to something.txt, I just can't seem to figure out how.
# 4  
Old 12-02-2011
Code:
$ awk 'NR==2 {printf("%s",$0)}' RS= infile
This is the text I need, it may possibly span two lines or more
but it will always have a blank line above and below it.

This User Gave Thanks to zaxxon For This Post:
# 5  
Old 12-03-2011
hey, that's great Zaxxon, thanks Smilie

I don't understand how the NR==2 works, can you explain what's happening with that? I've been a bit fuzzy on awk, just trying to understand it better. Thanks again Smilie
# 6  
Old 12-04-2011
No problem^^

NR stands for the awk built-in variable "Number Record". It automatically has the current line number assigned as value. If you have more than one file as input, there is also FNR which stands for "File Number Record".
Basically it is the line number of the current file that is being processed.

RS is the "Record Separator" variable, that stores the char that separates lines. Per default that is a newline "\n". By deleting that, awk automatically takes an expression like "\n\n+" as RS. That means that 1 or more empty lines will separate lines/records in terms of awk.
Since the text of the mail is in the second block, we just check if NR==2 and this evaluates to true, the commands inside {} will be executed. I took printf as it leaves out the additional blank line that would be displayed else.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Not able to generate table in email body

Below is the code snippet and I'm not able to generate the table in email, output email has nothing if ; then echo "File $fName exists." awk -F "," ' BEGIN { print "MIME-Version: 1.0" print "Content-Type: text/html" print "Subject: Out OF Network Fee - Portfolio Level Stats" print... (1 Reply)
Discussion started by: vyomdev
1 Replies

2. Shell Programming and Scripting

Need special charecter in email body

I have a unix shell script generate.sh that writes to a file hello.txt using redirect. For example:echo " Today's report shows progress by: " > hello.txt This hello.txt is then send as an email body to the recipients. My requirement is to have this special characters(up arrow and down arrow... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. UNIX for Dummies Questions & Answers

Sending mail in UNIX with body and attachment(.txt) using sendmail command

Hi All, In my .ksh script, I am sending an email with body and attachment (.txt) using sendmail command. I am able to receive the attachement along with the body of the mail. But I am getting special characters along with the content in the .txt. Also the alignment is incorrect. Can you please... (7 Replies)
Discussion started by: KRR
7 Replies

4. Shell Programming and Scripting

awk print the body of an email

I have fetched a couple of emails and stored then in /fetch/mail/oracle.txt, the displayed content is Date: From: Subject: To: Content-Type: ---body ----- --------- Date: From: Subject: To: Content-Type: ----body ----- ------ Date: From: I want to retrieve the body only from... (3 Replies)
Discussion started by: maddog21
3 Replies

5. Shell Programming and Scripting

issue with Email Body

Hi, I have written shell program to send email as below - #!/bin/ksh filename=`ls -ltrh *.Z` echo $filename |mailx -s "FOR TESTING" rahul.b@infotech.net I am getting the email but email body is - -rw-rw-r-- 1 bahulra dba 173M Mar 22 04:37 corphist.txt.Z -rw-rw-r-- 1 bahulra dba 107M... (3 Replies)
Discussion started by: rahulbahulekar
3 Replies

6. Shell Programming and Scripting

Crontab issue: getting email but with out body

I have a oracle sh script test.sh #!/bin/bash R=`sqlplus -S *****/*********@dfsdf <<ENDOFSQL set serveroutput on size unlimited; execute DBMS_SNAPSHOT.REFRESH( '"MIS_ADMIN"."MV_SURVEY_UNITS_CENSUS"','F'); execute DBMS_SNAPSHOT.REFRESH( '"MIS_ADMIN"."MV_SURVEY_UNITS_CENSUS_PART"','F');... (1 Reply)
Discussion started by: ramkia6
1 Replies

7. Shell Programming and Scripting

The body of the Email -- mailx

Hi, I am using mailx command to send an email and i took the body of the email from a file, mailx -r gtt.org -s "Status Report " ss@org.com < $ProcessStatisticsFile but now i want to declare the body of the email in the command itself. I have tried with the following command but... (4 Replies)
Discussion started by: chinnu01
4 Replies

8. Shell Programming and Scripting

emailing as body of email

hi all, how do i email a file in the body of an email rather than as an attachment ?? have a ksh script which i need to read a file and email as part of the body rather than an attachment. my code is : uuencode file.log | mailx -s "test" but this sends file as an attachment. ... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Please suggest how do i dump in .txt

I have requirement where i need to read data from .sh file. I am using awk to identify the particualr data and extracting it. mY CODE : awk '/SELECT/,/;/' SOURCE.sh I am getting output as SELECT.... .... FROM TABLE1; SELECT.... .... FROM TABLE2; SELECT.... .... FROM TABLE3; But my... (0 Replies)
Discussion started by: goutam_igate
0 Replies

10. Shell Programming and Scripting

how to check body of the email

Dears, i have user called dellsh i hope to make this script when this user recieve email check the budy of the email about (StatusRequest) when i find this email contain this subject run crontab do this job (create file in my home directory called index) thanks for your attention (1 Reply)
Discussion started by: dellsh
1 Replies
Login or Register to Ask a Question