Pages

Friday, January 31, 2014

Mail sending program

PROCEDURE SEND_MAIL ( p_subject   IN VARCHAR,
                               p_message   IN VARCHAR2)
   --****************************************************************************************
   --This Procedure is used to Send the Notification based on the Program.
   --****************************************************************************************
   IS
      mailhost CONSTANT   VARCHAR2 (30) := '192.168.185.6';
      port                NUMBER := 25;  -- 25 is default port for smtp server
      crlf CONSTANT       VARCHAR2 (2) := CHR (13) || CHR (10);
      mesg                VARCHAR2 (1000);
      mail_conn           UTL_SMTP.connection;
      lv_to               LONG := NULL;
      lv_cc               LONG := NULL;
      lv_bcc              LONG := NULL;
      lv_sender           VARCHAR2(100) :='no-reply@boobathy.com';

      CURSOR c1
      IS
           SELECT   lookup_code,
                    meaning,
                    description,
                    tag,
                    start_date_active,
                    end_date_active,
                    enabled_flag,
                    lookup_type
             FROM   fnd_lookup_values
            WHERE   lookup_type = 'PCSAS EMAIL USERS'
                    AND ENABLED_FLAG ='Y'
         ORDER BY   lookup_code;
   BEGIN
      mail_conn := UTL_SMTP.open_connection (mailhost, port);
      UTL_SMTP.helo (mail_conn, mailhost);
      UTL_SMTP.mail (mail_conn, lv_sender);

      FOR email_user IN c1
      LOOP
         IF UPPER (email_user.tag) = 'TO'
         THEN
            UTL_SMTP.rcpt (mail_conn, email_user.meaning);

            IF (lv_to IS NULL)
            THEN
               lv_to := email_user.meaning;
            ELSE
               lv_to := lv_to || ', ' || email_user.meaning;
            END IF;
         ELSIF UPPER (email_user.tag) = 'CC'
         THEN
            UTL_SMTP.rcpt (mail_conn, email_user.meaning);

            IF (lv_cc IS NULL)
            THEN
               lv_cc := email_user.meaning;
            ELSE
               lv_cc := lv_cc || ', ' || email_user.meaning;
            END IF;
         ELSIF UPPER (email_user.tag) = 'BCC'
         THEN
            UTL_SMTP.rcpt (mail_conn, email_user.meaning);

            IF (lv_bcc IS NULL)
            THEN
               lv_bcc := email_user.meaning;
            ELSE
               lv_bcc := lv_bcc || ', ' || email_user.meaning;
            END IF;
         END IF;
      END LOOP;

      mesg :=
            'Date: '
         || TO_CHAR (gd_current_date, 'dd Mon yy hh24:mi:ss')
         || crlf
         || 'From: NIPS Support '
         || lv_sender
         || crlf
         || 'Subject: '
         || p_subject
         || crlf
         || 'To: '
         || lv_To
         || crlf
         || 'Cc: '
         || lv_cc
         || crlf
         || 'Bcc: '
         || lv_bcc
         || crlf
         || ''
         || crlf
         || p_message
         || crlf
         || crlf
         || crlf
         || 'This is System Generated Information. Please do not Reply to this Message.';


      UTL_SMTP.DATA (mail_conn, mesg);
      UTL_SMTP.quit (mail_conn);
   END SEND_MAIL;
END;
/

No comments:

Post a Comment