Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AUTOMATING EMAIL ATTACHMENTS

Status
Not open for further replies.

eanda10

Programmer
Apr 9, 2002
141
US
does anyone have a clue how I can go about scripting a process to detach a file attachment from an email

For example my warehouse sends me a comma delimited file of that days recieving via email I want to set up an account called warehousedata@xxxxx.com I always get this email between 5:30 - 6pm so at 6 pm I want to sched. this cron job to seperate the attachment so I can write a program to upload this data in to my database.


Thank you
 
Ok, I've given this a bit of thought and played a bit on my test machine and heres what I find:

Test system = RH Linux, MTA = Sendmail, client mailer = Outlook on Win2K.

I generated a Excel (test.cvs) file and attached it to a mail to a user on my linux box, sent the mail to the user.

On the Linux server as root I read the file /var/spool/mail/username in that file I had the following:

START MAIL
+++++++++++++++++++++++++++++++++++++++++++++++++
From me@mymail.co.uk Mon Sep 8 21:02:33 2003
Return-Path: <me@mymail.co.uk>
Received: from testpc (me.fredbloggs.co.uk [xxx.xxx.xxx.xxx])
by mytest.server.com (8.9.3/8.9.3) with SMTP id VAA09252
for <username@hisdomain.co.uk>; Mon, 8 Sep 2003 21:02:31 +0100
From: &quot;Laurie&quot; <me@mymail.co.uk>
To: &quot;MR Tester&quot; <username@hisdomain.co.uk>
Subject:
Date: Mon, 8 Sep 2003 21:05:47 +0100
Message-ID: <LPBBIINFNJDFEAFPCKDKMEIKCKAA.username@hisdomain.co.uk>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=&quot;----=_NextPart_000_001C_01C3764C.F9C49D30&quot;
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0)
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Importance: Normal
X-OW-MailScanner: Found to be clean

This is a multi-part message in MIME format.

------=_NextPart_000_001C_01C3764C.F9C49D30
Content-Type: text/plain;
charset=&quot;iso-8859-1&quot;
Content-Transfer-Encoding: 7bit



Laurie Baker
IT Consultant

Information contained in this e-mail and any attachments is confidential and
intended for the use of the addressee only. Dissemination, distribution,
copying or use of this communication without prior permission of the
addressee is strictly prohibited. If you have received this transmission in
error, please advise the originator by reply e-mail and delete it. Thank
you.




------=_NextPart_000_001C_01C3764C.F9C49D30
Content-Type: application/octet-stream;
name=&quot;TEST.csv&quot;
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=&quot;TEST.csv&quot;

1,11,35,578,3465,44,1,9
578,578,1,578,1,578,578,578
11,4,85,356,1,234,33,345

------=_NextPart_000_001C_01C3764C.F9C49D30--

++++++++++++++++++++++++++++++++++++++++++++++++++++
END MAIL

So as you will see the attached file TEST.csv is clear in teh mail message so in this case its just a case of chopping out the bit you want and saving it to a file (you could have your data team add a row at the top and bottom to give your awk, sed or perl script identify the attachment start/finish perhaps.

NOW, if the mail is sent to you from a Unix system like the following: uuencode test.cvs test.cvs |mail -s &quot;todays data&quot; username@hisdomain.co.uk, then you will have an encoded attachment that would look something like:

+++++++++++++++++++++++++++++++++++++++++++++++++++
From: Root <root@myserver.com>
Message-Id: <200309081916.UAA07239@myserver.com>
To: username@hisdomain.com
Subject: TEST
Status: RO

begin 664 test.sql
M(R!->5-13&quot;!D=6UP(#@N,30*(R!486)L92!S=')U8W1U<F4@9F]R('1A8FQE
M(&quot;=E;G1R:65S)PI#4D5!5$4@5$%&quot;3$4@;7ET97-T(&quot;@*(&quot;!I9&quot;!I;G0H,3$I
M($Y/5&quot;!.54Q,(&%U=&]?:6YC<F5M96YT+`H@('1I=&QE('9A<F-H87(H-C`I
M(&1E9F%U;'0@3E5,3&quot;P*(&quot;!T97AT(&)L;V(L&quot;B`@9&%T92!D871E=&EM92!D
M969A=6QT($Y53$PL&quot;B`@4%))34%262!+15D@(&quot;AI9&quot;D**2!465!%/4UY25-!
M33L*(R!$=6UP:6YG(&1A=&$@9F]R('1A8FQE(&quot;=E;G1R:65S)PI)3E-%4E0@
M24Y43R!M>71E<W0@5D%,5453(&quot;@Q+&quot;=P87!E<B<L)T-U=',@<W1O;F4G+&quot;<R
M,#`R+3`T+3`Q(#$P.C(S.C,Q)RD[&quot;DE.4T525&quot;!)3E1/(&UY=&5S=&quot;!604Q5
M15,@*#(L)TQI;G5X(&%N9&quot;!!<&%C:&4G+&quot;=)(&AA=F4@9&ES8V]V97)E9&quot;!T
M:&%T($QI;G5X(&%N9&quot;!!<&%C:&4@87)E('-H:6YI;F<@97AA;7!L97,@;V8@
M3W!E;B!3;W5R8V4@<')O:F5C=',N)RPG,C`P,BTP-&quot;TP,R`Q,3HP,SHU,B<I
M.PI)3E-%4E0@24Y43R!M>71E<W0@5D%,5453(&quot;@S+&quot;=097)L)RPG4&5R;&quot;!I
M<R!G<F5A=&quot;<L)S(P,#(M,#0M,#4@,38Z,3,Z,C(G*3L*24Y315)4($E.5$\@
M;7ET97-T(%9!3%5%4R`H-&quot;PG0T=)(&%N9&quot;!M;V1?<&5R;&quot;<L)U=A>2!T;R!G
M;R$N)RPG,C`P,BTP-&quot;TP.&quot;`Q,CHT,SHR-B<I.PI)3E-%4E0@24Y43R!M>71E
M<W0@5D%,5453(&quot;@U+&quot;=02%`@:7,@8V]O;&quot;$G+&quot;=,96%R;FEN9R!02%`@:7,@
M=&AE(&)E<W0@=&AI;F<@=&AA=&quot;!)(&1I9&quot;!T;V1A>2XG+&quot;<R,#`R+3`T+3$V
-(#`X.C$Y.C(X)RD[&quot;@``
`
end
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Then you would do as you would before chop out the attachment part save it as a text file (in this case test.txt) then do: uudecode -o test.sql test.txt

This will give you a file called test.sql that looks like:

# MySQL dump 8.14
# Table structure for table 'entries'
CREATE TABLE mytest (
id int(11) NOT NULL auto_increment,
title varchar(60) default NULL,
text blob,
date datetime default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
# Dumping data for table 'entries'
INSERT INTO mytest VALUES (1,'paper','Cuts stone','2002-04-01 10:23:31');
INSERT INTO mytest VALUES (2,'Linux and Apache','I have discovered that Linux an
d Apache are shining examples of Open Source projects.','2002-04-03 11:03:52');
INSERT INTO mytest VALUES (3,'Perl','Perl is great','2002-04-05 16:13:22');
INSERT INTO mytest VALUES (4,'CGI and mod_perl','Way to go!.','2002-04-08 12:43:
26');
INSERT INTO mytest VALUES (5,'PHP is cool!','Learning PHP is the best thing that
I did today.','2002-04-16 08:19:28');
===========================================

Sorry about the content it's just test data.

Well I hope that gives you a starting point?
It should'nt be to hard to script what you need.

Good Luck,
Laurie

 
Cannot remember where I found it, but I hunted around on perl forums and found a perl script which does it nicely.

I can post the script contents here or place it in the FAQ, all depends what the demand is from other forum users.

IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration
MS Certified Windblows Rebooter
 
count me in - either place owuld be fine.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Placed it in the FAQ, hope you guys find it useful:

faq80-4138

IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration
MS Certified Windblows Rebooter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top