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

1 comment: