I have a stored procedure that reads value from table and sends them as CSV file. I wanted the following to be the headers of the CSV file .
'ID ,INPUT_DATE ,PAYER ,AMOUNT ,TYPE ,PAYEE-SORTCODE_&_BANK_ACCOUNT_NO ,ADDITIONAL_REMARKS ,COMMENTS ,ACCOUNT_NUMBER ,POLICY_NUMBER ,DATE_OF_BRANCH_CONFIRMATION ,CONFIRMED_BY ,SHEETUPDATE_DATE ,MAILUPDATE_DATE ,DATE-TIME ,USER_ID ,STATUS '
The procedure reads data from table and adds header and stores in a clob variable . the data looks like this when printed in log before it is added as mail attachment .
But when it is mailed as attachment it doesnt have a header and only has the data
Here is my code :
create or replace PROCEDURE EMAIL_DUMP AS
l_clob2 clob;
l_attach_text2 clob;
l_attach_text_h2 clob;
v_From VARCHAR2(280) := 'abc';
v_Recipient VARCHAR2(280) := 'efg';
v_Subject VARCHAR2(280) := ' Details';
v_Mail_Host VARCHAR2(230) := 'internal';
v_Mail_Conn utl_smtp.Connection;
crlf VARCHAR2 (32767) := chr(13)||chr(10);
c_mime_boundary CONSTANT VARCHAR2 (256) := '¿AAAAA000956¿';
v_index integer;
v_len integer;
FC_SV_STATUS_DESC VARCHAR2(100) := 'open';
Record_id Number ;
Input_date varchar2(100);
Payer varchar2(100);
Amount varchar2(100);
Trans_Type varchar2(100);
Payee varchar2(100);
Remarks varchar2(500);
Comments varchar2(500);
Acc_no varchar2(100);
Policy_no varchar2(100);
Branch_of date ;
Confirmed varchar2(100);
Sheet_update date;
Mail_update date;
Upload_time date;
Upload_id varchar2(100);
CURSOR c2 IS
select FC_CA_RECORD_ID as Record_id,FC_CA_INPUT_DATE as Input_date,FC_CA_PAYER as Payer,FC_CA_AMOUNT as Amount,FC_CA_TYPE as Trans_Type,FC_CA_PAYEE as Payee,FC_CA_ADD_REMARKS as Remarks,FC_CA_COMMENTS as Comments,FC_CA_ACC_NO as Acc_no,FC_CA_POLICY_NO as Policy_no,FC_CA_BRANCHCONF_DATE as Branch_of,FC_CA_CONFIRMED_BY as Confirmed,FC_CA_SHEETUPDATE_DATE as Sheet_update,FC_CA_MAILUPDATE_DATE as Mail_update,FC_CA_UPLOAD_TIME as Upload_time,FC_CA_UPLOAD_ID as Upload_id into Record_id,Input_date,Payer,Amount,Trans_Type,Payee,Remarks,Comments,Acc_no,Policy_no,Branch_of,Confirmed,Sheet_update,Mail_update,Upload_time,Upload_id FROM abc where FC_CA_STATUS =3 ;
BEGIN
l_attach_text_h2 :=
'ID ,INPUT_DATE ,PAYER ,AMOUNT ,TYPE ,PAYEE-SORTCODE_&_BANK_ACCOUNT_NO ,ADDITIONAL_REMARKS ,COMMENTS ,ACCOUNT_NUMBER ,POLICY_NUMBER ,DATE_OF_BRANCH_CONFIRMATION ,CONFIRMED_BY ,SHEETUPDATE_DATE ,MAILUPDATE_DATE ,DATE-TIME ,USER_ID ,STATUS ';
FOR employee_rec2 in c2
LOOP
l_attach_text2 := '"' ||
employee_rec2.Record_id || '","' ||
employee_rec2.Input_date || '","' ||
employee_rec2.Payer || '","' ||
employee_rec2.Amount || '","' ||
employee_rec2.Trans_Type || '","' ||
employee_rec2.Payee || '","' ||
employee_rec2.Remarks || '","' ||
employee_rec2.Comments || '","' ||
employee_rec2.Acc_no || '","' ||
employee_rec2.Policy_no || '","' ||
employee_rec2.Branch_of || '","' ||
employee_rec2.Confirmed || '","' ||
employee_rec2.Sheet_update || '","' ||
employee_rec2.Mail_update || '","' ||
employee_rec2.Upload_time || '","' ||
employee_rec2.Upload_id || '","' ||
FC_SV_STATUS_DESC || '"' ||chr(13);
l_clob2 := l_clob2||chr(10)||l_attach_text2;
END LOOP;
l_clob2 := l_attach_text_h2 ||chr(13)|| l_clob2;
DBMS_OUTPUT.put_line(l_clob2);
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.OPEN_DATA(v_Mail_Conn);
UTL_SMTP.write_data(v_Mail_Conn, 'From: ' || v_From || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'To: ' || v_Recipient || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Subject: ' || REPLACE(v_Subject, '[DATE]',TO_CHAR(sysdate,'DD.MM.YYYY')) || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Type: multipart/mixed; boundary="' || c_mime_boundary || '"' || UTL_TCP.crlf);
-- Mail body:
UTL_SMTP.write_data(v_Mail_Conn, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Type: text/plain' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, ' Details' || UTL_TCP.crlf);
-- Set up attachment header
UTL_SMTP.write_data(v_Mail_Conn, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Type: text/plain' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Disposition: attachment; filename="' || 'myfile.csv' || '"' || UTL_TCP.crlf);
-- Write attachment contents
v_len := DBMS_LOB.getlength(l_clob2);
v_index := 1;
WHILE v_index <= v_len
LOOP
UTL_SMTP.write_data(v_Mail_Conn, DBMS_LOB.SUBSTR(l_clob2, 32000, v_index));
v_index := v_index + 32000;
END LOOP;
-- End attachment
UTL_SMTP.write_data(v_Mail_Conn, UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, '--' || c_mime_boundary || '--' || UTL_TCP.crlf);
utl_smtp.CLOSE_DATA(v_mail_conn);
utl_smtp.Quit(v_mail_conn);
DBMS_OUTPUT.put_line('mail send completed...');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.put_line ( 'Error raised: '|| DBMS_UTILITY.FORMAT_ERROR_BACKTRACE || ' - '||sqlerrm);
system.intranet_utils.INTRANET_LOG_ERRORS('procedure EmailDump',
system.intranet_utils.INTRANET_GET_ERRMSG, 'Error in EmailDump');
END EMAIL_DUMP;
If I edit the mail attachment part like below ,then I'm able to get a proper CSV file as attachment with hearder and data , but then I am unable to add body message to the mail.
UTL_SMTP.write_data(v_Mail_Conn, 'From: ' || v_From || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'To: ' || v_Recipient || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Subject: ' || REPLACE(v_Subject, '[DATE]',TO_CHAR(sysdate,'DD.MM.YYYY')) || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Type: multipart/mixed; boundary="' || c_mime_boundary || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'This is a multi-part message in MIME format.' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, '--' || c_mime_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Type: text/plain' || UTL_TCP.crlf);
-- Set up attachment header
UTL_SMTP.write_data(v_Mail_Conn, 'Content-Disposition: attachment; filename="' || 'CMTL.csv' || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, UTL_TCP.crlf);
-- Write attachment contents
v_len := DBMS_LOB.getlength(l_clob2);
v_index := 1;
WHILE v_index <= v_len
LOOP
UTL_SMTP.write_data(v_Mail_Conn, DBMS_LOB.SUBSTR(l_clob2, 32000, v_index));
v_index := v_index + 32000;
END LOOP;
-- End attachment
UTL_SMTP.write_data(v_Mail_Conn, UTL_TCP.crlf);
UTL_SMTP.write_data(v_Mail_Conn, '--' || c_mime_boundary || '--' || UTL_TCP.crlf);
Can anyone tell me why I cant send a mail with both mail body message and attachment ?
I am new to oracle and here I am trying generate a CSV file from the values from my table and mail it. I am able to generate the csv file but I am unable to add a heading to my file and my date values are being shown as ########. I want the CSV file to be generated as the expected one .
The expected CSV :
The generated CSV :
Here is my code :
l_attach_text_h :=
'RECORD_ID ,INPUTTED DATE ,INPUTTED BY ,BROKER .....
FOR employee_rec in c1
LOOP
l_attach_text := '"' ||
employee_rec.FC_ED_RECORD_ID || '","' ||
employee_rec.FC_ED_UPLOADTIME || '","' ||
employee_rec.FC_ED_USER_ID || '","' ||
employee_rec.FC_ED_BROKER || '","' ||
........
l_clob := l_clob||chr(10)||l_attach_text;
END LOOP;
l_clob := l_attach_text_h ||chr(13)|| l_clob;
DBMS_OUTPUT.put_line(' Dtls processing completed...');
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.Data(v_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: ' || v_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 ||
'Please find the following in the attachments :'|| crlf || -- Message body
'Entry details & Entry details'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="Files.csv"'|| crlf ||
'Content-Transfer_Encoding: 8bit'|| crlf ||
'Content-Disposition: attachment;'|| crlf ||
' filename="Files.csv"'|| crlf ||
crlf ||
l_clob || crlf || -- Content of attachment
crlf ||
'-------SECBOUND--' -- End MIME mail
);
utl_smtp.Quit(v_mail_conn);
DBMS_OUTPUT.put_line('mail send completed...');
can anyone help me with this?
As of the heading: create a new comma-separated line which contains all headings, e.g.
SQL> select '"Record ID"' ||','|| '"Inputted Date"' ||','|| '"Inputted By"' as heading from dual;
HEADING
-----------------------------------------
"Record ID","Inputted Date","Inputted By"
SQL>
and include it as the 1st line in the output file.
As of the ###### issue: are you sure it is a problem? What happens when you double-click in between columns B and C in Excel? That operation should adjust column B width so that it matches data width (so I'd expect actual data to be seen afterwards).
I am trying to send mail using Oracles UTL_SMTP. It works, but I get weird behaviour and I don't know why. When I send mail using my code below HTML code is printed with all the tags. Another problem, header information such as the sender and content type is also printed. I have been looking at this code for couple of hours now, but failed to figure out what the problem is.
Please Help!
l_mail_conn UTL_SMTP.connection;
l_mail_conn := UTL_SMTP.open_connection('myhost', '25');
UTL_SMTP.helo(l_mail_conn, 'myhost');
UTL_SMTP.mail(l_mail_conn, 'noreply#myhost.com');
UTL_SMTP.rcpt(l_mail_conn, 'test#gmail.com');
UTL_SMTP.open_data(l_mail_conn);
UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'To: test#gmail.com' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'From: noreply#myhost.com'|| UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Subject: test' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Reply-To: sales#myhost.ie' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/html; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, '<html>
<head>
<title>Test HTML message</title>
</head>
<body>
<p>This is a <b>HTML</b> <i>version</i> of the test message.</p>
<p><img src="http://oracle-base.com/images/site_logo.gif" alt="Site Logo" />
</body>
</html>');
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.close_data(l_mail_conn);
UTL_SMTP.quit(l_mail_conn);
And this is the e-mail I get when I send mail using this code:
Content-Type: text/html; charset="iso-8859-1"
<html>
<head>
<title>Test HTML message</title>
</head>
<body>
<p>This is a <b>HTML</b> <i>version</i> of the test message.</p>
<p><img src="http://oracle-base.com/images/site_logo.gif" alt="Site Logo" />
</body>
</html>
you are missing just a couple of lines.
For sending html content, you are missing:
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/html; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, p_html_msg);
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
Where l_boundary is a boundary varchar2 and p_html_msg is your html code.
For attachments:
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: ' || p_attach_mime || '; name="' || p_attach_name || '"' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Transfer-Encoding: base64' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Disposition: attachment; filename="' || p_attach_name || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
FOR i IN 0 .. TRUNC((DBMS_LOB.getlength(p_attach_blob) - 1 )/l_step) LOOP
UTL_SMTP.write_data(l_mail_conn, UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(DBMS_LOB.substr(p_attach_blob, l_step, i * l_step + 1))));
END LOOP;
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
Where l_step is a PLS_INTEGER (multiple of 3, for instance 12000) and p_attach_blob is your attachment (file).
And update this part of your code adding the following #2 lines:
UTL_SMTP.open_data(l_mail_conn);
UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'To: test#gmail.com' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'From: noreply#myhost.com'|| UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Subject: test' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Reply-To: sales#myhost.ie' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'MIME-Version: 1.0' || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: multipart/alternative; boundary="' || l_boundary || '"' || UTL_TCP.crlf || UTL_TCP.crlf);
Where l_boundary is a boundary varchar2 mentioned above.
Here you have your code updated:
GIST PL/SQL updated
And that's it.
Regards
I am new to Toad, SQLPlus and Oracle. I am attempting to run the following query using the UTL_SMTP package.
DECLARE
v_From VARCHAR2(80) := 'noreply#myemail.com';
v_Recipient VARCHAR2(80) := 'MyEmail#myemail.com';
v_Subject VARCHAR2(80) := 'Test with attachment';
v_Mail_Host VARCHAR2(30) := 'smtpserver.mycompany.com';
v_Mail_Conn utl_smtp.Connection;
crlf VARCHAR2(2) := chr(13)||chr(10);
BEGIN
v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);
utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
utl_smtp.Data(v_Mail_Conn,
'Date: ' || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: ' || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: ' || v_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 ||
'some message text'|| crlf || -- Message body
'more message text'|| crlf ||
crlf ||
'-------SECBOUND'|| crlf ||
'Content-Type: text/plain;'|| crlf ||
' name="excel.csv"'|| crlf ||
'Content-Transfer_Encoding: 8bit'|| crlf ||
'Content-Disposition: attachment;'|| crlf ||
' filename="TextFile.txt"'|| crlf ||
crlf ||
'Hello World'|| crlf || -- Content of attachment
crlf ||
'-------SECBOUND--' -- End MIME mail
);
utl_smtp.Quit(v_mail_conn);
EXCEPTION
WHEN utl_smtp.Transient_Error OR utl_smtp.Permanent_Error then
raise_application_error(-20000, 'Unable to send mail: '||sqlerrm);
END;
/
If I run the package in SQL Plus (Connect / as SYSDBA), it runs fine and I receive the email with the attachment.
If I run the package in Toad (Connected as SYSTEM), I receive the following error message.
[Error] ORA-06550: line 6, column 17:
PLS-00201: identifier 'UTL_SMTP' must be declared
ORA-06550: line 6, column 17:
I am running on a local Oracle Database 11g Express Edition Version 11.2.0.2.0 64-bit
What are the privileges on the package? These are the privileges on a default 12c installation:
select grantee, privilege from dba_tab_privs where table_name = 'UTL_SMTP';
GRANTEE PRIVILEGE
------- ---------
PUBLIC EXECUTE
APEX_040200 EXECUTE
If that PUBLIC grant is revoked then even SYSTEM cannot use the package. You could fix this by logging in as SYS and executing grant execute on sys.utl_smtp to public;. If the grant was revoked you may want to find out why. Some organisations may have a policy against using the package, or may require it to be granted to a specific user.
Also, consider using the newer, simpler package, UTL_MAIL.
CREATE OR REPLACE PROCEDURE bcy_genera_file_cedacri_G2 (p_errbuff OUT VARCHAR2, p_errcode OUT NUMBER, p_data_lancio IN date, p_directory IN VARCHAR2) is
file_csv utl_file.file_type;
v_src_file BFILE;
v_content BLOB;
g_application varchar2(3) := 'OIC';
g_ambient varchar2(4) := 'TEST';
g_ret_code_exception_value number := 2;
g_ret_code_exception number := 0;
v_date_extraction varchar2(15); --yyyymmddhhMM ..
creazione_file_except exception;
v_p_date_launching date;
cursor cur_csv is
SELECT flag_pubblicato, data_pubblicazione
FROM XXBCYIN.bcy_supporto_garante_2 bsg2;
BEGIN
v_date_extraction:= to_char (nvl(p_date_launching, sysdate), 'yyyymmddhhMM');
BEGIN
--to create file csv
file_csv := utl_file.fopen ('p_directory', 'GARANTE2||g_application||g_ambient||v_date_extraction.csv', 'W');
FOR r IN cur_csv
LOOP
utl_file.put_line (
file_csv,
--cur_csv.ID || ';' ||
--cur_csv.TRX_TYPE || ';' ||
'USER_ID' || ';' ||
'USERNAME' || ';' ||
'DOMINIO' || ';' ||
'TIMESTAMP' || ',' ||
'CODICE_POSTAZIONE_1' || ';' ||
'CODICE_POSTAZIONE_2' || ';' ||
'CODICE_POSTAZIONE_3' || ';' ||
'CODICE_POSTAZIONE_4' || ';' ||
'CODICE_POSTAZIONE_5'|| ';' ||
'NDG_CLIENTE' || ';' ||
'CODICE_SERVIZIO' || ';' ||
'CODICE_ABI' || ';' ||
'CODICE_ISTITUTO' || ';' ||
'CODICE_CAB_OPERATORE' || ';' ||
'CODICE_OPERATORE' || ';' ||)
END LOOP;
utl_file.fclose (file_csv);
v_src_file:= 'GARANTE2'||g_application||g_ambient||v_date_extraction.csv;
At the last line I get the error :
PLS-00487 invalid reference to variable 'v_date_extraction'.
This variable is defined
v_date_extraction:= to_char (nvl(p_date_launching, sysdate), 'yyyymmddhhMM');
where v_date_extraction is a varchar2 and p_data_lancio and sysdate are date. This may be the problem?
The problem appears to be that you failed to put ".csv" in quotes. I believe the line raising the error should be:
v_src_file:= 'GARANTE2'||g_application||g_ambient||v_date_extraction||'.csv';