Home » SQL & PL/SQL » SQL & PL/SQL » Use demo_mail with attachments < 64k
Use demo_mail with attachments < 64k [message #162734] Mon, 13 March 2006 06:32
adebeer
Messages: 1
Registered: March 2006
Location: RSA
Junior Member
Help anyone!

I have succeeded in sending e-mail messages with attachments larger than 64k, using the demo_mail package as follows:

CREATE OR REPLACE PROCEDURE SMTP_MAILATT (t_pers in varchar2,t_subject_txt in varchar2,
		  line1 in varchar2,line2 in varchar2,
		  line3 in varchar2,line4 in varchar2,
		  line5 in varchar2, line6 in varchar2,
		  line7 in varchar2, line8 in varchar2,
		  line9 in varchar2, line10 in varchar2,
		  t_attfilename in varchar2)
IS

fil 								BFILE;
file_len 						PLS_INTEGER;
MAX_LINE_WIDTH 			PLS_INTEGER := 54;
buf 								RAW(4000);
amt 								BINARY_INTEGER := 672 * 3;  /* ensures proper format;  2016 */
pos 								PLS_INTEGER := 1; /* pointer for each piece */
filepos 						PLS_INTEGER := 1; /* pointer for the file */
filenm 							VARCHAR2(50) := t_attfilename; /* binary file attachment */
v_file_name 				VARCHAR2(100) := 'aa.csv'; /* ascii file attachment */
v_file_handle 			UTL_FILE.FILE_TYPE;
v_directory_name 		VARCHAR2(100) := 'BFILE_DIR';
v_line 							VARCHAR2(1000);
conn 								UTL_SMTP.CONNECTION;
mesg 								VARCHAR2(32767);
mesg_len 						NUMBER;
crlf 								VARCHAR2(2) := chr(13) || chr(10);
data 								RAW(4000);
chunks 							PLS_INTEGER;
len 								PLS_INTEGER := 1;
modulo 							PLS_INTEGER;
pieces 							PLS_INTEGER;
err_num 						NUMBER;
err_msg 						VARCHAR2(100);
v_mime_type_bin 		varchar2(30) := 'application/xls';
BOUNDARY CONSTANT VARCHAR2(256) := '-----7D81B75CCC90D2974F7A1CBD';
MULTIPART_MIME_TYPE CONSTANT VARCHAR2(256) := 'multipart/mixed; boundary="'||BOUNDARY || '"';

BEGIN

BEGIN
conn := demo_mail.begin_mail(
sender => '<[email]mysite@whereever.com[/email]>',
recipients => t_pers,
subject => t_subject_txt,
mime_type => MULTIPART_MIME_TYPE
);
END begin_mail;

BEGIN
demo_mail.attach_text(
conn => conn,
data => line1||'
'||line2||'
'||line3||'
'||line4||'
'||line5||'
'||line6||'
'||line7||'
'||line8||'
'||line9||'
'||line10||'
PLEASE DO NOT REPLY TO THIS E-MAIL. This address is not monitored.' || crlf || crlf,
mime_type => 'text/plain');
END attach_text;


--This is needed for including attachments
BEGIN
demo_mail.begin_attachment(conn => conn,mime_type => 'text/plain',inline => TRUE,filename => v_file_name,
transfer_enc => '7 bit');
begin
v_file_handle := utl_file.fopen(v_directory_name, v_file_name, 'r');
loop
utl_file.get_line(v_file_handle, v_line);
mesg := v_line || crlf;
demo_mail.write_text(
conn => conn,
message => mesg);
end loop;
exception
when others then
null;
end;
utl_file.fclose(v_file_handle);
demo_mail.end_attachment(
conn => conn );
END begin_attachment;

BEGIN
demo_mail.begin_attachment(conn => conn,mime_type => v_mime_type_bin,inline => TRUE,filename => filenm, transfer_enc => 'base64');
   BEGIN
      fil := BFILENAME('BFILE_DIR', filenm);
      file_len := dbms_lob.getlength(fil);
      dbms_output.put_line('file_len is '||file_len);
      modulo := mod(file_len, amt);
      pieces := trunc(file_len / amt);
      if (modulo <> 0) then
         pieces := pieces + 1;
      end if;
      dbms_lob.fileopen(fil, dbms_lob.file_readonly);
      dbms_lob.read(fil, amt, filepos, buf);
      data := NULL;
      FOR i IN 1..pieces LOOP
         filepos := i * amt + 1;
         file_len := file_len - amt;
         data := utl_raw.concat(data, buf);
         chunks := trunc(utl_raw.length(data) / MAX_LINE_WIDTH);
         IF (i <> pieces) THEN
            chunks := chunks - 1;
         END IF;
          glis_mail.write_raw( conn => conn,
                                 message => utl_encode.base64_encode(data ) );
            data := NULL;
         if (file_len < amt and file_len > 0) then
            amt := file_len;
         end if;
         dbms_lob.read(fil, amt, filepos, buf);
      END LOOP;
   END;
   dbms_lob.fileclose(fil);
   demo_mail.end_attachment(conn => conn );
END begin_attachment;


demo_mail.end_mail(conn => conn);

EXCEPTION
   when no_data_found then
      demo_mail.end_attachment( conn => conn );
      dbms_lob.fileclose(fil);
      when others then
         demo_mail.end_attachment( conn => conn );
         err_num := SQLCODE;
         err_msg := SUBSTR(SQLERRM, 1, 100);
         dbms_output.put_line('Error number is ' || err_num);
         dbms_output.put_line('Error message is ' || err_msg);
         dbms_lob.fileclose(fil);
--END sendit;


END;
/


I would like to know how to send attachments smaller than 64k, using the demo_mail package.

Any help would be highly appreciated!

Regards
AdB
Previous Topic: Unique columns and Insert statements
Next Topic: Passing a 2-D Array to a Stored Procedure
Goto Forum:
  


Current Time: Sat Aug 23 02:33:57 CDT 2025