很多公司都有这样的需求,希望系统可以定时邮件发送一些统计报表给到相关人员,以便做数据分析,或者是希望做单时,发送单据内容给到相关人员提醒审核和备案,下面分享一种十分简单方便的实现方式,javamail+4gl+perl脚本实现;
1:在aooi999配置好系统标准的javamail 功能,测试可以正常发送报表邮件,配置方式可以百度 2:添加邮件发送功能函数,可以参考cl_prt里面的发邮件功能,我简化了一些东西代码如下: FUNCTION p_sendmail1(l_xml) DEFINE p_chk LIKE type_file.chr100 DEFINE l_cmd STRING DEFINE l_top STRING DEFINE l_tempdir STRING DEFINE res LIKE type_file.num5 DEFINE l_xml RECORD file STRING, #XML檔名(不含路徑,檔案放置在os.Path.join(FGL_GETENV("TEMPDIR")底下) mailserver STRING, #MAIL SERVER IP serverport STRING, #MAIL SERVER Port user STRING, #MAIL SERVER User passwd STRING, #MAIL SERVER User Password checkauth LIKE type_file.chr1, #No.FUN-690005 VARCHAR(1), #CheckAuth subject STRING, #信件主旨 body STRING, #信件本文內容檔路徑 attach STRING, #信件附件檔路徑 recipient STRING, #收件者 cc STRING, #副本 bcc STRING, #密件副本 sender STRING #寄件者 END RECORD DEFINE l_mlj RECORD LIKE mlj_file.* DEFINE l_str STRING DEFINE lc_channel base.Channel DEFINE ch base.Channel, l_status SMALLINT, l_index SMALLINT, l_temp STRING, l_text STRING LET res = 1 {INITIALIZE l_mlj.* TO NULL SELECT * INTO l_mlj.* FROM mlj_file WHERE mlj01 = "DEFAULT" INITIALIZE l_xml.* TO NULL LET l_top = fgl_getenv("TOP") LET l_tempdir =fgl_getenv("TEMPDIR") # 生成XML文件写入系统 LET l_xml.file = p_chk CLIPPED,'.xml' #按传入的名称 LET l_xml.mailserver = l_mlj.mlj03 #邮件主机 LET l_xml.serverport = l_mlj.mlj04 #邮件主机端口 LET l_xml.user = l_mlj.mlj05 #邮件主机用户 LET l_xml.passwd = l_mlj.mlj06 #邮件主机密码 LET l_xml.checkauth = l_mlj.mlj08 #认证 LET l_xml.sender = 'caozq@test.com' #寄件人 LET l_xml.subject ='TEST' #邮件主旨 LET l_xml.body = l_tempdir CLIPPED,'/',p_body CLIPPED #文本内容 LET l_xml.attach = l_tempdir CLIPPED,'/',p_attach CLIPPED #附件地址 LET l_xml.recipient = '839438304@qq.com' #收件人 #LET l_xml.cc = g_xml.cc #LET l_xml.bcc = g_xml.bcc} #########產生for javamail的xml檔############ LET l_tempdir =fgl_getenv("TEMPDIR") LET l_top = fgl_getenv("TOP") LET lc_channel = base.Channel.create() LET l_str = os.Path.join(l_tempdir CLIPPED,l_xml.file CLIPPED) CALL lc_channel.openFile(l_str, "w") CALL lc_channel.setDelimiter("") CALL lc_channel.write("<?xml version=""1.0"" encoding=""UTF-8""?>") #No.FUN-740189 CALL lc_channel.write("<Mail>") CALL lc_channel.write("<Protocol>smtp</Protocol>") LET l_str = '<CheckAuth>',l_xml.checkauth CLIPPED,'</CheckAuth>' CALL lc_channel.write(l_str CLIPPED) LET l_str = '<MailServer>',l_xml.mailserver CLIPPED,'</MailServer>' CALL lc_channel.write(l_str CLIPPED) LET l_str = '<MailServerPort>',l_xml.serverport CLIPPED,'</MailServerPort>' CALL lc_channel.write(l_str CLIPPED) LET l_str = '<MailServerUser>',l_xml.user CLIPPED,'</MailServerUser>' CALL lc_channel.write(l_str CLIPPED) LET l_str = '<MailServerUserPassword>',l_xml.passwd CLIPPED,'</MailServerUserPassword>' CALL lc_channel.write(l_str CLIPPED) Let l_str = '<Subject>',l_xml.subject CLIPPED,'</Subject>' CALL lc_channel.write(l_str CLIPPED) IF NOT cl_null(l_xml.body) THEN LET l_str = '<MessageBody>',l_xml.body CLIPPED,'</MessageBody>' CALL lc_channel.write(l_str CLIPPED) END IF IF NOT cl_null(l_xml.attach) THEN LET l_str = '<Attach>',l_xml.attach CLIPPED,'</Attach>' CALL lc_channel.write(l_str CLIPPED) END IF LET l_str = '<Recipient>',l_xml.recipient CLIPPED,'</Recipient>' CALL lc_channel.write(l_str CLIPPED) IF NOT cl_null(l_xml.cc) THEN LET l_str = '<CCRecipient>',l_xml.cc CLIPPED,'</CCRecipient>' CALL lc_channel.write(l_str CLIPPED) END IF IF NOT cl_null(l_xml.bcc) THEN LET l_str = '<BCCRecipient>',l_xml.bcc CLIPPED,'</BCCRecipient>' CALL lc_channel.write(l_str CLIPPED) END IF LET l_str = '<From>',l_xml.sender CLIPPED,'</From>' CALL lc_channel.write(l_str CLIPPED) CALL lc_channel.write("</Mail>") CALL lc_channel.close() #將產生給javamail的xml檔轉成客戶端的編碼 # 生成XML文件写入系统 LET l_cmd="chmod 777 ",os.Path.join(l_tempdir CLIPPED,l_xml.file CLIPPED)," 2>/dev/null" #FUN-8B0011 RUN l_cmd LET l_cmd="sh ",l_top CLIPPED,"/ds4gl2/bin/javamail/UnixMailSender.bat ", os.Path.join(l_tempdir CLIPPED,l_xml.file CLIPPED)," >/u1/out/caozq.txt" #FUN-510006 FUN-8B0011 RUN l_cmd IN FORM MODE RETURNING res if res = 0 then let l_str = "Send ok" else let l_str = "Send fail" end if #读取caozq.txt,判断是否成功 SLEEP 1 LET ch = base.Channel.create() CALL ch.openFile('/u1/out/caozq.txt','r') LET l_status = ch.read(l_temp) WHILE l_status LET l_text = l_text.trim(),l_temp.trim() LET l_status = ch.read(l_temp) END WHILE LET l_index = 0 LET l_index = l_text.getIndexOf('successfully',1) IF l_index > 0 THEN let l_str = "Send ok" else let l_str = "Send fail" END IF END FUNCTION 函数可做一个公用函数,供其他程序调用 3:如何生成附件excel档可以参考另一个帖子 perl生成excel 的,没写太详细,码字太麻烦.... |
|
沙发#
发布于:2017-12-14 10:08
下次上传例子~
|
|
板凳#
发布于:2017-12-15 19:01
mark ,学习下,谢谢分享
|
|
地板#
发布于:2018-10-22 14:47
mark一下,以后学习
|
|
4楼#
发布于:2019-12-05 16:50
18824582472:下次上传例子~回到原帖版主,可以传个例子参考咩 |
|
5楼#
发布于:2021-12-01 09:35
l_cmd = "/u1/toptest/topcust/czz/4gl/txt2excel.pl /u1/out/cxmp409_menmrtzg.txt /u1/out/cxmp409_menmrtzg.xls " 已经按大佬的模式弄了,但是txt就是不能转成xls,导致发送的邮件无附件。请教怎么弄?大佬。。。。。。 |
|
6楼#
发布于:2021-12-06 09:27
|
|
7楼#
发布于:2022-01-04 09:31
插个眼,后续便查。
|
|