1 2 /* Sccsid = %Z% generic/sproc/%M% %I% %G% */3 4 /*
5 ** sp_processmail processes all the mail messages in the SQL Server's
6 ** mail box: it locates the next mail in the mail box(xp_findnextmsg),
7 ** gets the parameters of the mail by reading it(xp_readmail), sends
8 ** a response back to the Sybmail client(xp_sendmail) and finally
9 ** deletes the mail that was just read(xp_deletemail). If the message
10 ** retrieved thru xp_readmail contained sql queries, these are executed in
11 ** xp_sendmail and results returned to the mail client.
12 **
13 ** Messages for "sp_processmail"
14 ** 18398, "Failed to read mail from SQL Server's mailbox.
15 ** Check XP Server log file for more information."
16 ** 18399, "Failed to send mail to %1!. Check XP Server log
17 ** file for more information."
18 ** 18400, "Failed to delete mail. Check XP Server log
19 ** file for more information."
20 */21 22 createproceduresp_processmail23 24 @subject varchar(255)=NULL,/* subject of mail to look for */25 @originator varchar(255)=NULL,/* sender of mail to look for */26 @dbuser varchar(255)=NULL,/* context of query execution */27 @dbname varchar(255)=NULL,/* name of database to use */28 @filetype varchar(10)=NULL,/* extention for attachment file */29 @separator char(10)=NULL/* separator between columns */30 31 as32 33 declare@msgid binary(255)/* id of msg retrieved */34 declare@ccrecp varchar(255)/* carbon copy recipients */35 declare@bccrecp varchar(255)/* blind copy recipients */36 declare@msubject varchar(255)/* mail subject */37 declare@query varchar(255)/* query batch to be executed */38 declare@attach varchar(255)/* path to attach file */39 declare@sender varchar(255)/* sender of mail retrieved */40 declare@ret int /* return status from sybmail */41 declare@msg varchar(255)42 declare@errornum int /* error num from xp_findnextmsg */43 declare@filenum varchar(3)/* contains file name substring */44 45 /* Load the default values for procedure parameters */46 if(@filetypeisNULL)47 select@filetype= "TXT"
48 if(@dbuserisNULL)49 select@dbuser= "guest"
50 if(@dbnameisNULL)51 select@dbname= "master"
52 53 /* Generate the attachment file name */54 select@filenum= datename(millisecond, getdate())55 select@attach=("syb" +@filenum+ "00000")56 select@attach= substring(@attach, 1, 8)57 select@attach=(@attach+ "." +@filetype)58 59 /* If a message is found, process it */60 while(1 < 2)61 BEGIN62 /* We select the first unread msg each time. Set @msgid to NULL */63 select@msgid=NULL64 exec@ret=xp_findnextmsg@msgidOUTPUT65 select@errornum= @@error
66 67 /* if there is no message, this sp is done. 11526 is 'eom' error */68 if(@ret!= 0)69 BEGIN70 if(@errornum!= 11526)71 return 1
72 else73 break74 END75 76 exec@ret=xp_readmail@msgid,NULL,@senderOUTPUT,NULL,77 @msubjectOUTPUT,@ccrecpOUTPUT,78 @bccrecpOUTPUT,@queryOUTPUT,79 NULL,NULL,NULL,NULL,80 NULL, 0,NULL81 82 if(@ret!= 0)83 BEGIN84 /*
85 ** 18398, "Failed to read mail from SQL Server's mailbox.
86 ** Check XP Server log file for more information."
87 */88 raiserror 18398
89 return 1
90 END91 92 /*
93 ** If a 'subject' or 'originator' filter was provided,
94 ** check against them for a match. If match is found
95 ** send the mail containing query results to sender.
96 */97 if(@subjectisnotNULL)98 BEGIN99 if(@subject!=@msubject)100 continue101 END102 103 if(@originatorisnotNULL)104 BEGIN105 if(@originator!=@sender)106 continue107 END108 109 exec@ret=xp_sendmail@sender,@msubject,@ccrecp,110 @bccrecp,@query,NULL,@attach,111 'true',NULL,NULL,112 NULL,NULL,NULL,@separator,113 @dbuser,@dbname,NULL,NULL114 115 if(@ret!= 0)116 BEGIN117 /*
118 ** 18399, "Failed to send mail to %1!. Check XP Server log
119 ** file for more information."
120 */121 raiserror 18399,@originator122 return 1
123 END124 125 /* Delete the mail from SQL Server's mail box */126 exec@ret=xp_deletemail@msgid127 128 if(@ret!= 0)129 BEGIN130 /*
131 ** 18400, "Failed to delete mail. Check XP Server log
132 ** file for more information."
133 */134 raiserror 18400
135 return 1
136 END137 138 END139 140 /* sp_processmail successfully completed */141 return 0
142