Reading the Attachment from UNIX email


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Reading the Attachment from UNIX email
# 1  
Old 02-20-2017
Reading the Attachment from UNIX email

Hi - How would you read in the attachment from your unix mail account? The attachment is in BASE64 so I need to isolate those lines so I can convert them. The number of lines varies from a few to a few hundred. To complicate matters, the mail box gets non related emails I want to ignore. The lines before the attachment look like this:

Code:
--========4567899043590==--
 Content - Type: text/csv
 MIME - Version: 1.0
 Content-Transfer-Encoding:base64
 Content - Disposition: attachment
  filename="Report_attachment 2017-01-28.csv"
 <SPACE>
 jkhdfuhusduidfnkjdfhljkhoiuhjflkjdhfuhudhf
 jdljkjuuouiyudfahkjiuiuykjkjhpoudpudfaouu
 nwliowedfiooindsdiuknjnojeweuihsdfiolkjioij
 kasdfjdfookfkdfjkadjfoisjdfsjpiudfidsfioiosdf
 <SPACE>
  --========4567899043590==--

In the header of the mail message it has this:
Code:
Content-Type multipart/mixed: boundary=" --========4567899043590==--"

I want to do this check programmatically multiple times per day.
Any ideas on the best approach to take for this??
Thanks!



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-20-2017 at 02:39 PM.. Reason: Added CODE tags.
# 2  
Old 02-20-2017
Welcome to the forum.

Please clarify
- mail from your unix account (to somewhere else) or read in your unix account?
- how to identify non related e-mails to be ignored?
- lines before the attachment or lines including the attachment?
# 3  
Old 02-20-2017
These are emails coming from another system to my unix mailbox. They have an attachment made up of lines of data. I want to isolate them, convert from base64, then send them on for further processing. The other emails could be anything but none of them will have an attachments. That's why I was trying to look at only the ones that had this on a line: filename="Report_attachment 2017-01-28.csv". The date would change so I would only search for filename="Report_attachment. I thought if I found that line in the mail file, then found the space afterwards, then start reading in the data until I hit another space. But I'm open to any suggestions.
# 4  
Old 03-04-2017
I would first change your email alias.
Code:
                   
faxctl:         faxctl, |/u/spool/fax/faxextract 
faxout:         faxout, |/u2/fax/faxage

Now the emails will automatically be piped into the process as they arrive.
Then add something similar to:
Code:
#!/usr/bin/bash                                  
subject1=" Email job submission status"          
subject2=" Fax job status - Conversion Failure"  
subject3=" Fax Status for Job ID"                
first=0                                          
start=0                                          
JOBSTATUS=; export JOBSTATUS                     
SPOOLID=; export SPOOLID                         
DATETIME=; export DATETIME                       
PAGES=; export PAGES                             
STATUS=; export STATUS                           
TOTTIME=; export TOTTIME                         
#                                                
while read line                                  
do                                               
if [ $first -eq 0 ]                              
then                                             
        first=1                                  
        from=`echo $line |cut -d" " -f2 `        
        if [ "$from" != "support@faxage.com" ]   
        then                                     
                echo not from faxage             
                exit                             
        fi   
fi                                                       
line1=${line/:/_}                                        
key=`echo $line1|cut -d"_" -f1`                          
value=`echo $line1|cut -d"_" -f2`                        
value=`echo "$value"`                                    
if [ "a$value" = "a" ]                                   
then                                                     
        continue                                         
fi                                                       
if [ "a$key" != "aSubject" -a $start -eq 0 ]             
then                                                     
        continue                                         
fi                                                       
if [ "a$key" = "aSubject" ]                              
then                                                     
        start=1                                          
        if [ "a${value:0:${#subject1}}" = "a$subject1" ] 
        then                                             
                JOBSTATUS="QUEUED"                       
                continue                                 
        fi                                               
        if [ "a${value:0:${#subject2}}" = "a$subject2" ] 
               
        then                                                      
                JOBSTATUS="ERROR"                                 
                continue                                          
        fi                                                        
        if [ "a${value:0:${#subject3}}" = "a$subject3" ]          
        then                                                      
                JOBSTATUS="OK"                                    
                continue                                          
        fi                                                        
fi                                                                
if [ "$key" = "CustID" ]                                          
then                                                              
        SPOOLID=$value                                            
fi                                                                
if [ "$key" = "Status" ]                                          
then                                                              
        STATUS=`echo $value`                                      
fi                                                                
if [ "$key" = "Finished At" ]                                     
then                                                              
        DATETIME=`echo $value`    
                                
        DATETIME=${DATETIME:0:10}${DATETIME:11:8}${DATETIME:20:7} 
fi    
 if [ "$key" = "Transmit Time" ]                
then                                           
        TOTTIME=`echo $value`                  
fi                                             
if [ "$key" = "Page Count" ]                   
then                                           
        PAGES=`echo $value`                    
fi                                             
done                                           
echo JOBSTATUS=$JOBSTATUS >>/u2/fax/faxage.log 
echo SPOOLID=$SPOOLID     >>/u2/fax/faxage.log 
echo STATUS=$STATUS       >>/u2/fax/faxage.log 
echo PAGES=$PAGES         >>/u2/fax/faxage.log 
echo DATETIME=$DATETIME   >>/u2/fax/faxage.log 
echo TOTTIME=$TOTTIME     >>/u2/fax/faxage.log

given an email that looks like:
Code:
From support@faxage.com Tue Oct 18 13:47:58 2016                       
Message-Id: <201610up.com>                                             
Received: from [6.199]                                                 
        for <faxout@p.com>; Tue, 18 Oct 2016 11:47:51 -0600 (MDT)      
Content-Type: multipart/mixed; boundary="_----------=_1476812871189590"
MIME-Version: 1.0                                                      
X-Mailer: MIME::Lite 3.029 (F2.78; T1.35; A2.12; B3.08; Q3.08)         
Date: Tue, 18 Oct 2016 11:47:51 -0600                                  
From: "FAXAGE" <support@faxage.com>                                    
To: "Canada Inc" <faxout@p.com>                                        
Subject: Fax Status for Job ID 289215564 to (819)555-1212              
X-Faxage-Status: Failure                                               
upportontent-Transfer-Encoding: 7bit                                   
                                                                       
This is a multi-part message in MIME format.                           
                                                                       
--_----------=_1476812871189590                                        
Content-Disposition: inline                                            
Content-Type: text/plain                                               
Content-Transfer-Encoding: quoted-printable                            
                                                                       
Your fax job has completed. Details are below:                         
                                                                       
To: Email to FAX                                                       
                           
Number: (819)555-1212                      
Status: Failure                            
Reason: No Answer                          
Finished At: 2016-10-18 13:47:49 EDT       
Transmit Time: 00:00:16                    
Pages Sent: 0                              
                                            
 CustID:999999                              
                                            
 --_----------=_1476812871189590--

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Email with attachment from local machine through UNIX

Hello everyone, So, here is my requirement. I want to run an SQL query and export the result in an excel file and send that as an attachement in an email to the intended receipients. Please help me understand how to do that. How to connect to database, how to export the output in an excel... (3 Replies)
Discussion started by: Mohdrafi12
3 Replies

2. Shell Programming and Scripting

html format email with attachment in unix

Team, I have the below code, which is working fine and it sends the html report using sendmail command. I want to attach one more file ( which goes as attachment ) in that email. How to achieve it. i tried with uuencode. But no luck :mad: outputFile="/tmp/out.html" ( echo... (2 Replies)
Discussion started by: itkamaraj
2 Replies

3. Shell Programming and Scripting

UNIX file attachment in email

Hi, I have a syntax for mail attachment as $EMAIL "Wrapper $wrapper_script_name has failed" " $wrapper_script_name has Failed " $failed_email_address and $EMAIL is as below MSGSub=${1} MSGText=${2} RMAIL=${3} #set LANG='' export LANG='' echo "${MSGSub}" | mailx -s "${MSGText}" ... (1 Reply)
Discussion started by: satgur
1 Replies

4. AIX

Send email from unix (AIX) with PDF attachment

I am using the following command to send PDF attachment with a mail. uuencode <attachment.pdf> <attachment.pdf>|mailx -s <subject> <mail_id> < <Message_file.txt> This one send the message with attachment. I would like send PDF attachment with the mail Can any one help with this issue ? ... (0 Replies)
Discussion started by: sunjup
0 Replies

5. Shell Programming and Scripting

Send email with attachment in form of excel in unix

Hi, I have a shell script which send email with an attachment in the form of an email. However, the when I open the attachment, all the data comes in one column. How do I format the data in the excel sheet while sending the email? Thanks (8 Replies)
Discussion started by: bdebroy
8 Replies

6. Shell Programming and Scripting

send email from unix with attachment

Hello All, This is a common question that I found lot of results in the forums. I am trying to use uuencode to attach a file and send email. I have no issues sending email, but not able to attach any files using sendmail. Is uuencode part of sendmail or does 'uuencode' utility need to be... (1 Reply)
Discussion started by: chiru_h
1 Replies

7. UNIX for Dummies Questions & Answers

unix to Lotus Notes email attachment

Hi all, I have searched the FAQ and find that there is some threads related to this subject. But can you please give an examples on how to send attachment to Lotus Notes email through UNIX? Since i have gone through the RFC and the URL. But i still have no idea on it. Please give some... (7 Replies)
Discussion started by: wilsonchan1000
7 Replies

8. UNIX for Dummies Questions & Answers

unix to Lotus Notes email attachment

We have been trying to get an email from unix to Lotus Notes to work. We finally got it to work with the following code: cat filename | uuencode filename | mailx -s "subject title" email address Now our problem is that Lotus Notes doesn't show the paper clip icon, indicating an attachment... (3 Replies)
Discussion started by: cowgilm
3 Replies

9. How do I send email?

Transfering unix file as an email attachment

I've written a a script which generates a report file, saved to a unix directory. I need to transfer it, via email, to some users. The command I'm using in my script is: (note that subject & cur_address are set in the script prior to this line) cat /u/sandyl/sm_o_commdt_archive/c | uuencode... (3 Replies)
Discussion started by: slivi
3 Replies

10. UNIX for Dummies Questions & Answers

Transfering unix file as an email attachment

I've written a a script which generates a report file, saved to a unix directory. I need to transfer it, via email, to some users. The command I'm using in my script is: (note that subject & cur_address are set in the script prior to this line) cat /u/sandyl/sm_o_commdt_archive/c | uuencode... (3 Replies)
Discussion started by: slivi
3 Replies
Login or Register to Ask a Question