设为首页 收藏本站
查看: 995|回复: 0

[经验分享] EBS FORM : 发送邮件API(转)

[复制链接]

尚未签到

发表于 2015-9-23 10:36:54 | 显示全部楼层 |阅读模式
  功能:不同的发送类型---抄送、发送、密送。
  
  
  CREATE OR REPLACE PACKAGE send_email IS
  FUNCTION fnk_mail_test(p_header_id  IN NUMBER,
                         ic_mail_from IN VARCHAR2, --1. Mail from
                         ic_title     IN VARCHAR2, --4.title
                         ic_message   IN VARCHAR2, --5. Message
                         ic_filename  IN VARCHAR2) RETURN VARCHAR2;
  END send_email;
/
CREATE OR REPLACE PACKAGE BODY send_email IS
  PROCEDURE log(buff IN VARCHAR2) IS
  BEGIN
    fnd_file.put(fnd_file.log, buff);
    fnd_file.new_line(fnd_file.log, 1);
  EXCEPTION
    WHEN OTHERS THEN
      RETURN;
  END log;
  FUNCTION fnk_mail_test(p_header_id  IN NUMBER,
                         ic_mail_from IN VARCHAR2, --1. Mail from
                         ic_title     IN VARCHAR2, --4. title
                         ic_message   IN VARCHAR2, --5. Message
                         ic_filename  IN VARCHAR2) RETURN VARCHAR2 IS
    cc_error CONSTANT CHAR(1) := '1'; --Return Code Value Error
    cc_ok    CONSTANT CHAR(1) := '0'; --Return Code Value Success
    --Declare Variable
    lv_mailtitle VARCHAR2(250); --Mail Title
    lv_mailtext  VARCHAR2(2550); --Content of Mail
    lv_mailto    VARCHAR2(2550); --All Address of the Recipients---all
    lv_mailto_to VARCHAR2(2550); --All Address of the Recipients---to
    lv_mailto_cc VARCHAR2(2550); --All Address of the Recipients---cc
  
    lv_mailfrom          VARCHAR2(100) := 'out-xuxiaoxiao@cn.panasonic.com'; --Sender Address
    lv_smtpserver        VARCHAR2(30) := 'smtp.cn.panasonic.com'; --'10.195.74.239'; --'129.249.64.64' --SMTP Server Name or IP
    ln_smtpport          NUMBER(10) := 25; --SMTP Server Port
    lc_return            CHAR(1); --Return Code
    c_mail               utl_smtp.connection;
    lv_directory_name    VARCHAR2(100);
    lv_file_name         VARCHAR2(100);
    lv_file_handle       utl_file.file_type;
    lv_line              VARCHAR2(1000);
    ln_slash_pos         NUMBER;
    crlf                 VARCHAR2(2) := chr(13) || chr(10);
    mesg                 VARCHAR2(32767);
    mesg_len             NUMBER;
    max_size             NUMBER DEFAULT 9999999999;
    mesg_length_exceeded BOOLEAN := FALSE;
    mesg_too_long EXCEPTION;
    debug     NUMBER DEFAULT 0;
    err_msg   VARCHAR2(200);
    error_msg VARCHAR2(250);
    --所有的邮箱
    CURSOR c_get_mail_address IS
      SELECT email
        FROM zzom_send_email
       WHERE header_id = p_header_id
         AND enabled_flag = 'Y'
         AND SYSDATE BETWEEN nvl(effective_start_date, SYSDATE - 1) AND nvl(effective_end_date, SYSDATE + 1);
    --发送
    CURSOR c_get_mail_address_to IS
      SELECT email
        FROM zzom_send_email
       WHERE header_id = p_header_id
         AND send_type = '发送'
         AND enabled_flag = 'Y'
         AND SYSDATE BETWEEN nvl(effective_start_date, SYSDATE - 1) AND nvl(effective_end_date, SYSDATE + 1);
    --抄送
    CURSOR c_get_mail_address_cc IS
      SELECT email
        FROM zzom_send_email
       WHERE header_id = p_header_id
         AND send_type = '抄送'
         AND enabled_flag = 'Y'
         AND SYSDATE BETWEEN nvl(effective_start_date, SYSDATE - 1) AND nvl(effective_end_date, SYSDATE + 1);
  BEGIN
    log('12311111');
    --Get Mail Title
    lv_mailtitle := 'Subject:' || ic_title;
    --Get Mail Text
    lv_mailtext := ic_message;
    log('12311112');
    --Connection
    c_mail := utl_smtp.open_connection(lv_smtpserver, ln_smtpport);
    log('12311113');
    --Set SMTP Server
    utl_smtp.helo(c_mail, lv_smtpserver);
    log('12311114' || lv_mailfrom);
    --Set Sender Address
    IF ic_mail_from IS NOT NULL THEN
      lv_mailfrom := ic_mail_from;
    END IF;
    log('12311115' || lv_mailfrom);
    utl_smtp.mail(c_mail, lv_mailfrom);
    log('123' || lv_mailtitle);
    --Set Recipients Address--for all
    --获取全部收件人地址-并发送
    FOR v_get_mail_address IN c_get_mail_address
    LOOP
      utl_smtp.rcpt(c_mail, v_get_mail_address.email);
      lv_mailto := lv_mailto || v_get_mail_address.email || ';';
    END LOOP;
    --获取发送地址  
    FOR v_get_mail_address_to IN c_get_mail_address_to
    LOOP
      lv_mailto_to := lv_mailto_to || v_get_mail_address_to.email || ';';
    END LOOP;
    --获取抄送地址  
    FOR v_get_mail_address_cc IN c_get_mail_address_cc
    LOOP
      lv_mailto_cc := lv_mailto_cc || v_get_mail_address_cc.email || ';';
    END LOOP;
  
    log('124' || lv_mailto);
    utl_smtp.rcpt(c_mail, lv_mailfrom);
    --Open Data
    utl_smtp.open_data(c_mail);
    mesg := 'Date: ' || to_char(SYSDATE, 'dd Mon yy hh24:mi:ss') || crlf || 'From: ' || lv_mailfrom || crlf;
    --|| lv_mailtitle || crlf ||
    utl_smtp.write_data(c_mail, mesg);
    utl_smtp.write_raw_data(c_mail, utl_raw.cast_to_raw(convert(lv_mailtitle || crlf, 'ZHS16CGB231280')));
    mesg := 'To: ' || lv_mailto_to || crlf || 'Cc: ' || lv_mailto_cc || crlf || 'Mime-Version: 1.0' || crlf ||
            'Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"' || crlf || '' || crlf ||
            '--DMW.Boundary.605592468' || crlf ||
           
           --US-ASCII         
            'Content-Type: text/plain; name="lv_mailtext.txt"; charset=GB2312' || crlf ||
            'Content-Disposition: inline; filename="lv_mailtext.txt"' || crlf || 'Content-Transfer-Encoding: 32bit' || crlf || '' || crlf;
    utl_smtp.write_data(c_mail, mesg);
    utl_smtp.write_raw_data(c_mail, utl_raw.cast_to_raw(convert(utl_tcp.crlf || lv_mailtext, 'ZHS16CGB231280')));
    --=============================================================
    -- reade file
    --=============================================================
    IF ic_filename IS NOT NULL THEN
      BEGIN
        -- locate the final '/' or '\' in the pathname ...
        ln_slash_pos := instr(ic_filename, '/', -1);
        IF ln_slash_pos = 0 THEN
          ln_slash_pos := instr(ic_filename, '\', -1);
        END IF;
        -- separate the filename from the directory name ...
        lv_directory_name := substr(ic_filename, 1, ln_slash_pos - 1);
        lv_file_name      := substr(ic_filename, ln_slash_pos + 1);
        -- open the file ...
        lv_file_handle := utl_file.fopen(lv_directory_name, lv_file_name, 'r');
        mesg           := crlf || '--DMW.Boundary.605592468' || crlf ||
                          'Content-Type: application/octet-stream; name="' || lv_file_name || '"' || crlf ||
                          'Content-Disposition: attachment; filename="' || lv_file_name || '"' || crlf ||
                          'Content-Transfer-Encoding: 16bit' || crlf || crlf;
        mesg_len       := mesg_len + length(mesg);
        --utl_smtp.write_data ( c_mail, mesg );
        utl_smtp.write_raw_data(c_mail, utl_raw.cast_to_raw(convert(utl_tcp.crlf || mesg, 'ZHS16CGB231280')));
        -- and append the file contents to the end of the message ...
        LOOP
          utl_file.get_line(lv_file_handle, lv_line);
          IF mesg_len + length(lv_line) > max_size THEN
            mesg := '*** truncated ***' || crlf;
            --utl_smtp.write_data ( c_mail, mesg );
            utl_smtp.write_raw_data(c_mail, utl_raw.cast_to_raw(convert(utl_tcp.crlf || mesg, 'ZHS16CGB231280')));
            mesg_length_exceeded := TRUE;
            RAISE mesg_too_long;
          END IF;
          mesg := lv_line || crlf;
          --utl_smtp.write_data ( c_mail, mesg );
          utl_smtp.write_raw_data(c_mail, utl_raw.cast_to_raw(convert(mesg, 'ZHS16CGB231280')));
          mesg_len := mesg_len + length(mesg);
        END LOOP;
      EXCEPTION
        WHEN utl_file.invalid_path THEN
          err_msg := SQLERRM;
          IF debug > 0 THEN
            dbms_output.put_line('Error in opening attachment ' || ic_filename || ':' || err_msg);
          END IF;
          -- All other exceptions are ignored ....
        WHEN OTHERS THEN
          NULL;
      END;
    END IF;
    mesg := crlf;
    --utl_smtp.write_data ( c_mail, mesg );
    utl_smtp.write_raw_data(c_mail, utl_raw.cast_to_raw(convert(utl_tcp.crlf || mesg, 'ZHS16CGB231280')));
    --Close Data
    utl_smtp.close_data(c_mail);
    --Quit UTL_SMTP
    utl_smtp.quit(c_mail);
    --Return Success
    lc_return := cc_ok;
    RETURN(lc_return);
  EXCEPTION
    WHEN OTHERS THEN
      error_msg := SQLERRM;
      lc_return := cc_error;
      RETURN(lc_return);
  END fnk_mail_test;
END;
/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-117593-1-1.html 上篇帖子: 这段时间总结的EBS方面的知识,没有整理 下篇帖子: EBS中FND_REQUEST.SUBMIT_REQUEST的一点介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表