create or replace PROCEDURE USD_EMAIL_ATTACHMENTS (from_name varchar2, to_name varchar2, subject varchar2, message varchar2, max_size number default 9999999999, filename1 varchar2 default null, debug number default 0 ) is v_smtp_server varchar2(100) := 'smtp.XXXXXX.edu'; v_directory_name varchar2(100); v_file_name varchar2(100); v_line varchar2(1000); crlf varchar2(2):= chr(13) || chr(10); mesg varchar2(32767); conn UTL_SMTP.CONNECTION; v_file_handle utl_file.file_type; v_slash_pos number; mesg_len number; mesg_too_long exception; invalid_path exception; mesg_length_exceeded boolean := false; begin -- Open the SMTP connection ... conn:= utl_smtp.open_connection( v_smtp_server, 25 ); -- Initial handshaking ... utl_smtp.helo( conn, v_smtp_server ); utl_smtp.mail( conn, from_name ); utl_smtp.rcpt( conn, to_name ); utl_smtp.open_data ( conn ); -- build the start of the mail message ... -- ----------------------------------- mesg:= 'Date: ' || TO_CHAR( SYSDATE, 'dd Mon yy hh24:mi:ss' ) || crlf || 'From: ' || from_name || crlf || 'Subject: ' || subject || crlf || 'To: ' || to_name || crlf || 'Mime-Version: 1.0' || crlf || 'Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"' || crlf || '' || crlf || 'This is a Mime message, which your current mail reader may not' || crlf || 'understand. Parts of the message will appear as text. If the remainder' || crlf || 'appears as random characters in the message body, instead of as' || crlf || 'attachments, then you''ll have to extract these parts and decode them' || crlf || 'manually.' || crlf || '' || crlf || '--DMW.Boundary.605592468' || crlf || /*'Content-Type: text/plain; name="message.txt"; charset=US-ASCII' || crlf || 'Content-Disposition: inline; filename="message.txt"' || crlf || 'Content-Transfer-Encoding: 7bit' || crlf || '' || crlf || message || crlf ;*/ 'Content-Type: application/octet-stream; name="' || v_file_name || '"' || crlf || 'Content-Disposition: attachment; filename="' || v_file_name || '"' || crlf || 'Content-Transfer-Encoding: 7bit' || crlf || crlf ; mesg_len := length(mesg); if mesg_len > max_size then mesg_length_exceeded := true; end if; utl_smtp.write_data ( conn, mesg ); -- Append the files ... -- ---------------- -- If the filename has been supplied ... if filename1 is not null then begin -- locate the final '/' or '\' in the pathname ... v_slash_pos := instr(filename1, '/', -1 ); if v_slash_pos = 0 then v_slash_pos := instr(filename1, '\', -1 ); end if; -- separate the filename from the directory name ... v_directory_name := substr(filename1, 1, v_slash_pos - 1 ); v_file_name := substr(filename1, v_slash_pos + 1 ); -- open the file ... v_file_handle := utl_file.fopen('FILEPATH', v_file_name, 'r' ); -- generate the MIME boundary line ... mesg := crlf || '--DMW.Boundary.605592468' || crlf || 'Content-Type: application/octet-stream; name="' || v_file_name || '"' || crlf || 'Content-Disposition: attachment; filename="' || v_file_name || '"' || crlf || 'Content-Transfer-Encoding: 7bit' || crlf || crlf ; mesg_len := mesg_len + length(mesg); utl_smtp.write_data ( conn, mesg ); -- and append the file contents to the end of the message ... loop utl_file.get_line(v_file_handle, v_line); if mesg_len + length(v_line) > max_size then mesg := '*** truncated ***' || crlf; utl_smtp.write_data ( conn, mesg ); mesg_length_exceeded := true; raise mesg_too_long; end if; mesg := v_line || crlf; utl_smtp.write_data ( conn, mesg ); mesg_len := mesg_len + length(mesg); end loop; exception when utl_file.invalid_path then if debug > 0 then dbms_output.put_line('Error in opening attachment '|| filename1); end if; -- All other exceptions are ignored .... when others then DBMS_OUTPUT.PUT_LINE('Inside Error Handler: ' || SQLERRM); end; mesg := crlf; utl_smtp.write_data ( conn, mesg ); -- close the file ... utl_file.fclose(v_file_handle); end if; -- append the final boundary line ... mesg := crlf || '--DMW.Boundary.605592468--' || crlf; utl_smtp.write_data ( conn, mesg ); -- and close the SMTP connection ... utl_smtp.close_data( conn ); utl_smtp.quit( conn ); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Outside Error Handler: ' || SQLERRM); END USD_EMAIL_ATTACHMENTS;