Sunday, 3 July 2016

Sending Calendar Invite via APEX Application

With the below code we can send individual emails to the users with an ICS file as attachment.
ICS is a global format for calendar files widely being utilized by various calendar and email programs including Google Calendar, Apple iCal, and Microsoft Outlook. These files enable users to share and publish information directly from their calendars over email or via uploading it to the world wide web.

Step 1 :

Compile the below code

CREATE OR REPLACE FUNCTION ical_events (
   p_summary       IN   VARCHAR2,
   p_description   IN   VARCHAR2,
   p_start_date    IN   DATE,
   p_end_date      IN   DATE
)
   RETURN VARCHAR2
AS
   lv_desc      VARCHAR2 (20000);
   lv_summary   VARCHAR2 (10000);
   lv_dtstart   VARCHAR2 (100);
   lv_date      VARCHAR2 (100);
   lv_dtend     VARCHAR2 (100);
   l_retval     VARCHAR2 (32767);
   l_lf         CHAR (1)         := CHR (10);
BEGIN
   lv_summary := 'SUMMARY:' || p_summary;
   lv_date :=
         'DTSTAMP:'
      || TO_CHAR (SYSDATE, 'RRRR-MM-DD')
      || 'T'
      || TO_CHAR (SYSDATE, 'HH24:MI:SS');
   lv_dtstart :=
         'DTSTART:'
      || TO_CHAR (p_start_date, 'RRRR-MM-DD')
      || 'T'
      || TO_CHAR (p_start_date, 'HH24:MI:SS');
   lv_dtend :=
         'DTEND:'
      || TO_CHAR (p_end_date, 'RRRR-MM-DD')
      || 'T'
      || TO_CHAR (p_end_date, 'HH24:MI:SS');
   l_retval :=
         'BEGIN:VCALENDAR
VERSION:2.0
PRODID:-// Oracle Application Express //ENCAL
SCALE:GREGORIAN
BEGIN:VEVENT
'
      || lv_date
      || CHR (10)
      || lv_dtstart
      || CHR (10)
      || lv_dtend
      || CHR (10)
      || lv_summary
      || CHR (10)
      || lv_desc
      || '
SEQUENCE:0
END:VEVENT
END:VCALENDAR';
   DBMS_OUTPUT.put_line (l_retval);
   RETURN l_retval;
END ical_events;

Step 2:

 Use APEX to create the necessary items to collect the following details:
1. Start Date
2. End Date
3. Summary
4. Description

Step 3 :

Execute the below PLSQL process to send email with ICS file

DECLARE
   l_id           NUMBER;
   l_ical_event   VARCHAR2 (32767);
   l_blob         BLOB;
BEGIN
   BEGIN
      SELECT ical_events (p_summary          => :p2_summary,
                          p_description      => :p2_description,
                          p_start_date       => :p2_start_date,
                          p_end_date         => :p2_end_date
                         )
        INTO l_ical_event
        FROM DUAL;
   END;

   BEGIN
      l_id :=
         apex_mail.send (p_from           => 'notification-noreply@doyensys.com',
                         p_to             => 'priyadharshini.ramesh@doyensys.com',
                         p_subj           => 'Subject of the Email',
                         p_body           => 'BODY',
                         p_body_html      => 'HTML - BODY'
                        );
   EXCEPTION
      WHEN OTHERS
      THEN
         raise_application_error (-20004,
                                  'Error in sending an Email' || SQLERRM
                                 );
   END;

   l_blob := UTL_RAW.cast_to_raw (l_ical_event);
   --Converting Varchar2 to BLOB content
   
   BEGIN
      apex_mail.add_attachment (p_mail_id         => l_id,
                                p_attachment      => l_blob,
                                p_filename        => 'training.ics',
                                --Name of the ICS file
                                p_mime_type       => 'application/hbs-ics'
                               );
   END;
END;

6 comments:

  1. awesome.. worked perfectly.. many thanks...

    ReplyDelete
  2. Works great, thank you :)
    In the function, the description isn't being set so the calendar doesn't have the body text.
    Also, you can add location as well with "LOCATION:" using the same method for summary.

    ReplyDelete
  3. Hi,
    It is working fine. If I confirm yes then add the gmail calendar otherwise it does not display or auto sync in gmail calendar. But i need auto sync in gmail calendar. What can i do? Please any one help me.

    ReplyDelete
    Replies
    1. Hi @blog
      You can use Google CalDAV API to add events without any user intervention (initially after the user has allowed access to your app). You will have to generate authorization token using clientID and clientSecret (store for every user in your local repo). There's a neat demo available here: https://youtu.be/tNo9IoZMelI

      There are a lot of resources available to help you out with the specific tech/platform you want to use.

      Delete
  4. It didn't work for me. The plsql process running successfully but didn't get any email.

    ReplyDelete
    Replies
    1. You have to add apex_mail.push_queue; at the end

      Delete