Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Attach file with PL/SQL procedure and sand it true mail

Lela23Nov 18 2022 — edited Nov 18 2022

Hi, I hope could someone help me. I have this procedure and it compiles with no errors. But the problems that I have are:
1. File name - ATT36279.txt - the file name is change
2. File Content - no data content in the file when I open it in true MS Outlook
This is the procedure:
CREATE OR REPLACE procedure p_send_email_with_attach(
directory in varchar2,
recipients in varchar2,
subject in varchar2,
message in varchar2) is

file_handle utl_file.file_type;
output varchar2(20000);
attachment_text varchar2(20000);
add_date varchar2(100) := to_char(sysdate,'dd.mm.yyyy' || '_' || 'hh24:mi:ss');
v_dir varchar2(30) := directory;
v_recipients varchar2(200) := recipients;
v_sub varchar2(30) := subject;
v_message varchar2(2000) := message;

begin
file_handle := utl_file.fopen(location => v_dir,
filename => 'name_of_file.txt',
open_mode => 'R');

loop
begin
utl_file.get_line(file_handle, output); -- we read the file, line by line
attachment_text := attachment_text||utl_tcp.crlf;
--and store every line in the attachment_text variable, separated by a ?new line? character.
exception
when no_data_found then
exit;
end;
end loop;
dbms_output.put_line(attachment_text);
utl_file.fclose(file_handle);

utl_mail.send_attach_varchar2(sender => 'xxxxxxxxxxxxx@xxx',
recipients => v_recipients,
subject => v_sub,
message => v_message,
attachment => attachment_text);

exception
when others then
raise_application_error(-20001,
'Error: ' ||
sqlerrm);
end;
/

for me is really important:
1. that the file has been read from a directory (from original file that I put there) and
2.I don't wont to show attached file like content of my mail. I would like it to be attached as a file.
All the mail recipients use MS Outlook.
Thanks for reading this and helping me.

This post has been answered by Saubhik Banerjee on Dec 2 2022
Jump to Answer
Comments
Post Details
Added on Nov 18 2022
26 comments
1,347 views