[Developer] Procedure傳送html形式表格的mail

tolilong發表於2016-04-07
如下procedure先生成html格式的樣式,然後利用sendmail procedure 傳送郵件出來。
create or replace procedure mon_filesystem_pro
as
v_body long;
v_subject varchar2(1000) :='Filesystem Warning';
msg varchar2(1000);
cursor c1 is 
  SELECT hostname,filesystem,allbytes,usebytes,freebytes,usepct,mounton,mon_datetime FROM MON_FILESYSTEM WHERE replace(usepct,'%','')>70;
v1  c1%rowtype;
v_count number default 0;
begin
   if c1%isopen then
      close c1;
   end if;


   open c1;
   v_body:='&nbsp;&nbsp;'||'<a href="" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||'& nbsp;'||'帳號和密碼和工號一樣<br><br>';
   v_body:=v_body||'<TABLE BORDER="0" cellspacing="1" cellpadding="1" bgcolor=Black>';
   v_body:=v_body||'<TR bgcolor=Blue>';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">HostName&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">FileSystem&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">AllBytes&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">UseBytes&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">FreeBytes&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">UsePCT&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">MountON&nbsp;';
   v_body:=v_body||'<TH style="FONT-FAMILY: Gulim">Mon_DateTime&nbsp;';
   loop
      fetch c1 into v1;
      exit when c1%notfound;
      v_body:=v_body||'<TR bgcolor=White>';
      v_body:=v_body||'<TD align="left" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.hostname||'</TD>';
      v_body:=v_body||'<TD align="left" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.filesystem||'</TD>';
      v_body:=v_body||'<TD align="center" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.allbytes||'</TD>';
      v_body:=v_body||'<TD align="center" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.usebytes||'</TD>';
      v_body:=v_body||'<TD align="center" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.freebytes||'</TD>';
      v_body:=v_body||'<TD align="center" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.usepct||'</TD>';
      v_body:=v_body||'<TD align="center" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.mounton||'</TD>';
      v_body:=v_body||'<TD align="center" style="FONT-FAMILY: Gulim; FONT-SIZE: 10pt">'||v1.mon_datetime||'</TD>';
   v_count:=v_count+1;
   end loop;
   v_body:=v_body||'</TABLE>';
   close  c1;
   
   if(v_count>0) then
      sendmail(v_body,v_subject);
   end if;
exception
  when others then
  msg:=substr(sqlerrm,1,1000);
  insert into proc_err values(to_char(sysdate,'yyyy/mm/dd hh24:mi:ss'),msg);
end;

生成的格式如下:
 & nbsp;帳號和密碼和工號一樣

HostName  FileSystem  AllBytes  UseBytes  FreeBytes  UsePCT  MountON  Mon_DateTime 
xxxxx /dev/sda5 62216440 50560564 8444488 86% /u01 2016/03/28 23:53:01
standby /dev/mapper/VolGroup-lv_home 211849568 152769972 48318176 76% /home 2016/03/28 23:53:01
xxxxx /dev/sda5 62216440 50559512 8445540 86% /u01 2016/03/27 23:51:08
standby /dev/mapper/VolGroup-lv_home 211849568 152200112 48888036 76% /home 2016/03/27 23:51:08
xxxxx /dev/sda5 62216440 50559512 8445540 86% /u01 2016/03/27 23:53:01
standby /dev/mapper/VolGroup-lv_home 211849568 152200136 48888012 76% /home 2016/03/27 23:53:01
xxxxx /dev/sda5 62216440 50559876 8445176 86% /u01 2 016/03/28 07:53:01
standby /dev/mapper/VolGroup-lv_home 211849568 152320352 48767796 76% /home 2016/03/28 07:53:01
xxxxx /dev/sda5 62216440 50560188 8444864 86% /u01 2016/03/28 15:53:01
standby /dev/mapper/VolGroup-lv_home 211849568 152508940 48579208 76% /home 2016/03/28 15:53:01


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24237320/viewspace-2077091/,如需轉載,請註明出處,否則將追究法律責任。

相關文章