Oracle PL/SQL send HTML mail does not behave as expected - oracle

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

Related

Oracle procedure fails to write headers to csv file

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 ?

CSV File generation in Oracle

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).

Send emails using oracle with sender's name

I have this procedure to send emails. I was wondering is there any way to add senders displaying name in addition to the email address, like in .NET?
I have attached the working code. The problem is is doesn't add the senders name.
p_smtp_host := '192.xxx.xxx.xxx';
p_smtp_port := 25;
l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port);
UTL_SMTP.helo(l_mail_conn, p_smtp_host);
UTL_SMTP.mail(l_mail_conn, p_from);
--UTL_SMTP.rcpt(l_mail_conn, p_to);
process_recipients(l_mail_conn, p_to);
process_recipients(l_mail_conn, p_cc);
process_recipients(l_mail_conn, p_bcc);
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: ' || p_to || UTL_TCP.crlf);
IF TRIM(p_cc) IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, 'CC: ' || REPLACE(p_cc, ',', ';') || UTL_TCP.crlf);
END IF;
IF TRIM(p_bcc) IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, 'BCC: ' || REPLACE(p_bcc, ',', ';') || UTL_TCP.crlf);
END IF;
UTL_SMTP.write_data(l_mail_conn, 'From: ' || p_from || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || p_subject || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Reply-To: ' || p_from || 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);
IF p_text_msg IS NOT NULL THEN
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, 'Content-Type: text/plain; charset="iso-8859-1"' || UTL_TCP.crlf || UTL_TCP.crlf);
UTL_SMTP.write_data(l_mail_conn, p_text_msg);
UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf);
END IF;
IF p_html_msg IS NOT NULL THEN
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);
END IF;
UTL_SMTP.write_data(l_mail_conn, '--' || l_boundary || '--' || UTL_TCP.crlf);
UTL_SMTP.close_data(l_mail_conn);
UTL_SMTP.quit(l_mail_conn);
Provide the mail like this
FromName := 'Wernfried Domscheit';
FromMail := 'wernfired.domscheit#server.com';
UTL_SMTP.WRITE_DATA(l_mail_conn, 'From: "'||FromName||'" <'||FromMail||'>'||UTL_TCP.CRLF);
Please note, by default SMTP supports only ASCII characters in From field, in case your name may contain Non-ASCII characters (e.g. 'Peter Müller') you should write it as this:
IF CONVERT(FromName, 'US7ASCII') = FromName THEN
UTL_SMTP.WRITE_DATA(l_mail_conn, 'From: "'||FromName||'" <'||FromMail||'>'||UTL_TCP.CRLF);
ELSE
UTL_SMTP.WRITE_DATA(l_mail_conn, 'From: =?UTF-8?B?'|| UTL_ENCODE.TEXT_ENCODE(FromName, NULL, UTL_ENCODE.BASE64) ||'?= <'||FromMail||'>'||UTL_TCP.CRLF);
END IF;
In order to check whether FromName is plain ASCII I prefer CONVERT(FromName, 'US7ASCII') = FromName rather than REGEXP_LIKE(FromName, '^[ -~]+$'), see Find out if a string contains only ASCII characters

Query running fine in SQLPlus but will not run in Toad when connected to the same database

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.

how to declare a variable in pl sql the right way?

here is my code
vJS VARCHAR2(3500); --25065
gMaxDays s_criteria%rowtype := get_criteria_rec('MAX_DAYS_BOOKING');
BEGIN
IF NOT Sec_Pkg.chk_sec('ATLAS_INV_OVERBOOK') THEN
-- Exit procedure if security did not pass
RETURN;
END IF;
--
vJS := ' gMaxDays;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
i created gMaxDays,at first i had it hardcoded but now its on a table called s_criteria and MAX_DAYS_BOOKING is part of s_criteria. im i calling it the right way?
this is how it use to look and it work
vJS VARCHAR2(3500); --25065
BEGIN
IF NOT Sec_Pkg.chk_sec('ATLAS_INV_OVERBOOK') THEN
-- Exit procedure if security did not pass
RETURN;
END IF;
--
vJS := 'var gMaxDays = 366;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
[/code]
Err ... Are you saying that
You used to have a code snippet that "worked":
vJS := 'var gMaxDays = 366;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
When you modified the snippet to be:
vJS := ' gMaxDays;'||CHR(10)||
'function checkfields() {' ||CHR(13) ||
' // Setting the target here' ||CHR(13) ||
' document.frmInvSelect.target="_top"' ||CHR(13) ||
It "works" no more ?
I guess you're trying to generate a JavaScript code snippet. Maybe this is what you're looking for:
declare
vJS VARCHAR2(3500);
gMaxDays s_criteria%rowtype := get_criteria_rec('MAX_DAYS_BOOKING');
begin
-- here you have to call the correct field from the record
-- in this example I assume it's max_day
vJS := 'var gMaxDays = ' || gMaxDays.max_day || ';' || CHR(10)||
'function checkfields() {' || CHR(10) ||
' // Setting the target here' || CHR(10) ||
' document.frmInvSelect.target="_top"' || CHR(10) ||
' // Add here something that makes this a valid JavaScript.'
end;
Check what the documentation says about PL/SQL record variables.

Resources