Showing posts with label UTL_SMTP. Show all posts
Showing posts with label UTL_SMTP. Show all posts

Wednesday, March 4, 2015

Sending e-mail with UTL_SMTP along with TEXT attachment

Dear Readers

You may have requirements to maybe e-mail a text file which you generate using some process via a concurrent program.

In such case below code can help you out. You just need to pass the below

1. Email ID to who mail is to be sent
2. Subject of mail
3. Body of mail
4. Attachment flag (whether or not an attachment is there, pass N to get simple mail and no attachment, pass Y to send the attachment also)
5. Attachment directory : the directory where the text file resides
6. File Name: Name of text file

PROCEDURE send_email_notif
(
   p_email_id     IN    VARCHAR2,
   p_subject      IN    VARCHAR2,
   p_body         IN    VARCHAR2,
   p_attachment   IN    VARCHAR2,
   p_attach_dir   IN    VARCHAR2,
   p_attach_file  IN    VARCHAR2
)
IS
   lv_from           VARCHAR2(80) := 'someone@somedomain.com';
   lv_recipient      VARCHAR2(80) := p_email_id;
   lv_subject        VARCHAR2(80) := p_subject;
   lv_mail_host      VARCHAR2(30) := 'localhost';
   lv_mail_conn      utl_smtp.Connection;
   crlf              VARCHAR2(2)  := chr(13)||chr(10);
   lv_line           VARCHAR2(2000);
   lv_file           Utl_File.file_type;
BEGIN
   lv_mail_conn := utl_smtp.Open_Connection(lv_mail_host, 25);
   utl_smtp.Helo(lv_mail_conn, lv_mail_host);
   utl_smtp.Mail(lv_mail_conn, lv_from);
   utl_smtp.Rcpt(lv_mail_conn, lv_recipient);

   IF p_attachment = 'N'
   THEN
      utl_smtp.Data(lv_mail_conn,
         'Date: '   || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
         'From: '   || lv_from || crlf ||
         'Subject: '|| lv_subject || crlf ||
         'To: '     || lv_recipient || crlf ||
         crlf || p_body || crlf
      );
   ELSE
      Utl_Smtp.open_data(lv_mail_conn);
      Utl_Smtp.write_data(lv_mail_conn,   'Date: '   || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
                                          'From: '   || lv_from || crlf ||
                                          'Subject: '|| lv_subject || crlf ||
                                          'To: '     || lv_recipient || crlf ||

                                             'MIME-Version: 1.0'|| crlf ||    -- Use MIME mail standard
                                          'Content-Type: multipart/mixed;'|| crlf ||
                                          ' boundary="-----SECBOUND"'|| crlf ||
                                          crlf ||

                                          '-------SECBOUND'|| crlf ||
                                          'Content-Type: text/plain;'|| crlf ||
                                          'Content-Transfer_Encoding: 7bit'|| crlf ||
                                          crlf || p_body || crlf ||
                                          crlf ||

                                          '-------SECBOUND'|| crlf ||
                                          'Content-Type: text/plain;'|| crlf ||
                                          ' name="' || p_attach_file || '"'|| crlf ||
                                          'Content-Transfer_Encoding: 8bit'|| crlf ||
                                          'Content-Disposition: attachment;'|| crlf ||
                                          ' filename="' || p_attach_file || '"'|| crlf ||
                                          crlf);


      lv_file := Utl_File.fopen(p_attach_dir, p_attach_file, 'R');

      LOOP
         BEGIN
            Utl_File.get_line(lv_file, lv_line);
         EXCEPTION
            WHEN OTHERS THEN
               EXIT;
         END;

         Utl_Smtp.write_data(lv_mail_conn, lv_line || crlf);
      END LOOP;
      Utl_Smtp.write_data(lv_mail_conn, crlf || '-------SECBOUND--');
      Utl_File.fclose(lv_file);
      Utl_Smtp.close_data(lv_mail_conn);
   END IF;

   utl_smtp.Quit(lv_mail_conn);

END send_email_notif;

Hope this helps

Cheers
A

Friday, September 19, 2014

Sending text attachment using UTL_SMTP

Dear Reader

In this post we will see the sample code which can be used for sending an e-mail with a text attachment using the UTL_SMTP package

Here is the code

DECLARE
   lv_mail_conn      UTL_SMTP.connection;
   lv_subject        VARCHAR2(240) := 'Test UTL_SMTP with Text Attachment';
   lv_body           VARCHAR2(2000) := 'Dear User, Please see attached the text file with the details of testing';
   lv_email_id       VARCHAR2(2000) := 'myemail@mycompany.com';
   lv_smtp_host      VARCHAR2(100) := 'localhost';
   lv_smtp_port      NUMBER := 25;
   lv_from_email_id  VARCHAR2(240) := 'utlsmtp_textattach@mycompany.com';
   lv_boundary       VARCHAR2(50) := '----=*#abc1234321cba#*=';
BEGIN
   lv_mail_conn := UTL_SMTP.open_connection(lv_smtp_host, lv_smtp_port);
   UTL_SMTP.helo(lv_mail_conn, lv_smtp_host);
   UTL_SMTP.mail(lv_mail_conn, lv_from_email_id);
   UTL_SMTP.rcpt(lv_mail_conn, lv_email_id);
   UTL_SMTP.open_data(lv_mail_conn);
   UTL_SMTP.write_data(lv_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'To: ' || lv_email_id || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'From: ' || lv_from_email_id || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Subject: ' || lv_subject || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Reply-To: ' || lv_from_email_id || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Content-Type: multipart/mixed; boundary="' || lv_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf);

   UTL_SMTP.write_data(lv_mail_conn, '--' || lv_boundary || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);

   UTL_SMTP.write_data(lv_mail_conn, lv_body);
   UTL_SMTP.write_data(lv_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, '--' || lv_boundary || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Content-Type: ' || 'text/plain' || '; name="' || 'text_attachment.txt' || '"' || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Content-Disposition: attachment; filename="' || 'text_attachment.txt' || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, 'Column1            Column2        Column3' || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, '================== ============== ============' || UTL_TCP.crlf);

   --Here simply we are writing 20 rows into the text file for testing purpose with 3 column data
   FOR i IN 1..20 LOOP
      UTL_SMTP.write_data(lv_mail_conn,   RPAD('Col1-Row' || i, 19) ||
                                          RPAD('Col2-Row' || i, 15) ||
                                          RPAD('Col3-Row' || i, 12) || UTL_TCP.crlf);
   END LOOP;
  
   UTL_SMTP.write_data(lv_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
   UTL_SMTP.write_data(lv_mail_conn, '--' || lv_boundary || '--' || UTL_TCP.crlf);
   UTL_SMTP.close_data(lv_mail_conn);
   UTL_SMTP.quit(lv_mail_conn);
END;


This will end up sending you a mail which looks like below



Hope this helps you all

Cheers
A