Send PJ oracle by mail - oracle

I am unable to retrieve the content of my attachment (an SQL result file created using the spool command).
The attachment is empty even though the corresponding file stored on my server is properly populated.
My procedure:
DECLARE
l_clob CLOB := 'MayBe';
begin
send_mail_bis_bis(p_to => 'toto#gmail.com',
p_from => 'toto#gmail.com',
p_subject => 'result',
p_text_msg => 'Maintenant reste la requête à bien la mettre dedans :) ',
p_attach_name => 'table.csv',
p_attach_mime => '/home/oracle/pj',
p_attach_clob => l_clob,
p_smtp_host => 'smtp.domaine.fr');
EXCEPTION
WHEN utl_smtp.Transient_Error OR utl_smtp.Permanent_Error then
raise_application_error(-20000, 'Unable to send mail: '||sqlerrm);
END;
/
Thanks
Attache file an email attachement

Related

How to send pl/sql procedure status through an email?

I have scheduled a PL/SQL procedure. I want to send the status of the PL/SQL procedure (whether it is successful or has any error messages) to my email address.
I saw some ways of sending pre-defined templates of emails using UTL_MAIL. But how can I get the status of my procedure into an email?
Send an e-mail at the end of the scheduled stored procedure, e.g.
create or replace procedure p_your_proc as
l_error varchar2(300);
begin
-- do some processing
-- if there were no errors
utl_mail.send(sender => 'rosh#gmail.com',
recipients => 'rosh#gmail.com',
cc => null,
bcc => null,
subject => 'Procedure P_YOUR_PROC completed successfully',
message => null);
exception
when others then
l_error := sqlerrm;
utl_mail.send(sender => 'rosh#gmail.com',
recipients => 'rosh#gmail.com',
cc => null,
bcc => null,
subject => 'Procedure P_YOUR_PROC ended with an error',
message => l_error);
raise;
end;

How to use the MEMBER_OF2 function in Oracle Apex using the APEX_LDAP package

Since I linked our Microsoft Active Directory with my Apex application using LDAP, I am trying to retrieve the groups for the user currently logged in from the Active Directory.
Here's the documentation: https://docs.oracle.com/cd/E59726_01/doc.50/e39149/apex_ldap.htm#AEAPI242
Here's my code of my dynamic action on page load, I am trying to retrieve a VARCHAR2 of all the groups the current user is part of, and to put it in a display-only field :
BEGIN
:P1_NEW := APEX_LDAP.MEMBER_OF2(
p_username => v('APP_USER'),
p_pass => 'mypassword',
p_auth_base => 'DOMAIN\',
p_host => 'XX.X.XXX.XX',
p_port => 389);
END;
But when I load my page, this error occurs
Ajax call returned server error ORA-31202: DBMS_LDAP : Erreur client/serveur LDAP : Invalid credentials. 80090308: LdapErr: DSID-0C090439, comment: AcceptSecurityContext error, data 52e, v4563 for Execute PL/SQL Code.
What's wrong in my code ? Thank you in advance for your help.
Thomas
I think you are using wrong the API Package APEX_LDAP. I am doing this on my own enviornment in a different way, but I am using Enterprise Edition:
Oracle Database version 19c
Oracle Apex version 20.1
Oracle Oracle Rest Data Services version 20.4
I have an authentication schema to make the login against the LDAP server of my own company. That authentication schema replaces the default one of Apex Locally managed users.
Application --> Shared Components --> Authentication Schemas --> Custom
function fn_val_user_pwd_ldap (
p_username in varchar2,
p_password in varchar2
)
return boolean
is
l_ldap_host varchar2(100) := 'myldaphost.com';
l_ldap_port number := 389 ;
begin
if APEX_LDAP.AUTHENTICATE(
p_username =>p_username,
p_password =>p_password,
p_search_base => 'OU=Users,OU=Mycompany,DC=de,DC=com,DC=corp',
p_host => l_ldap_host,
p_port => l_ldap_port)
then
dbms_application_info.set_action(null);
return true;
else
apex_util.set_authentication_result(p_code => 110);
apex_util.set_custom_auth_status(p_status => 'Username or password incorrect.');
dbms_application_info.set_action(null);
return false;
end if;
end;
Then, I have a post auth procedure in the same authentication method to retrieve the groups in LDAP. I prefer this way because it executes after the authentication, but you can do it also with a dynamic action.
declare
l_groups varchar2(4000);
BEGIN
l_groups := APEX_LDAP.MEMBER_OF2(
p_username => ':APP_USER',
p_pass => 'mypassword',
p_auth_base => 'OU=Users,OU=Mycompany,DC=de,DC=com,DC=corp',
p_host => 'myldaphost.com',
p_port => 389);
htp.p('Is Member of:'||l_groups);
END;
We don't use SSL to communicate internally with the LDAP Server, as this application is intranet. So the parameter p_use_ssl is by default to N.
As I was telling you in the comment section of your own question, I think the parameter p_auth_base refers to the LDIF format of your own LDAP server, not the domain name that refers to the AD.
UPDATE
Let me show you how it works. ( Of course, I hide sensitive information of my own company ). Sometimes it might happen that you can't get the group information as null. I am not sure here whether is a LDAP authorization issue rather than a problem with the APEX package itself
SQL> SET SERVEROUTPUT ON SIZE UNLIMITED ECHO ON
DECLARE
is_ok boolean;
l_mes varchar2(2000);
l_val varchar2(4000);
l_ldap_host varchar2(100) := 'myldapserver.com';
l_ldap_port number := 389 ;
BEGIN
if APEX_LDAP.AUTHENTICATE(
p_username =>'X329097',
p_password =>'********',
p_search_base => 'OU=Users,OU=Mycompany,DC=****,DC=*****,DC=****,DC=corp',
p_host => l_ldap_host,
p_port => l_ldap_port )
then
is_ok := true;
l_mes := 'Username and Password Correct' ;
dbms_output.put_line(l_mes);
else
l_mes := 'Username and Password Invalid' ;
dbms_output.put_line(l_mes);
end if;
if is_ok
then
l_val := APEX_LDAP.MEMBER_OF2(
p_username => 'X329097',
p_pass => '*********',
p_auth_base => 'OU=Users,OU=Mycompany,DC=****,DC=*****,DC=****,DC=corp',
p_host => l_ldap_host,
p_port => l_ldap_port);
dbms_output.put_line(l_val);
end if;
END;
/
Username and Password Correct
SC_APEX_ADMIN:SC_ORACLE_ADMIN
PL/SQL procedure successfully completed.
I tried to add your function and I cannot access to my application from my Login Page anymore. I've tried on the SQL Commands, here's what I got
DECLARE
VAL BOOLEAN;
BEGIN
IF APEX_LDAP.AUTHENTICATE(
p_username => 'thomas.tirole',
p_password => 'mypassword',
p_search_base => 'OU=Users,OU=Domain,DC=domain,DC=ch',
p_host => 'xx.x.xxx.xx',
p_port => 389
) THEN
dbms_output.put_line('AUTENTHICATED');
ELSE
DBMS_OUTPUT.PUT_LINE('NOT AUTHENTICATED');
END IF;
END;
---------------------------------------
NOT AUTHENTICATED
Statement processed.
0.00 seconds
If i try to do the MEMBER_OF2 function on the SQL Command, here's what I got :
declare
l_groups varchar2(4000);
BEGIN
l_groups := APEX_LDAP.MEMBER_OF2(
p_username => 'thomas.tirole',
p_pass => 'mypassword',
p_auth_base => 'OU=Users,OU=Domain,DC=domain,DC=ch', -- SAME WITH DOMAIN\
p_host => 'xx.x.xxx.xx',
p_port => 389);
htp.p('Is Member of:'||l_groups);
END;
---------------------------------------
ORA-31202: DBMS_LDAP : Erreur client/serveur LDAP : Invalid credentials. 80090308: LdapErr: DSID-0C090439, comment: AcceptSecurityContext error, data 52e, v4563
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 508
ORA-06512: à "SYS.DBMS_SYS_ERROR", ligne 86
ORA-06512: à "SYS.DBMS_LDAP", ligne 1487
ORA-06512: à "SYS.DBMS_LDAP", ligne 79
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 85
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 172
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 214
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 235
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 464
ORA-06512: à "APEX_210100.WWV_FLOW_LDAP", ligne 523
ORA-06512: à ligne 4
ORA-06512: à "SYS.DBMS_SQL", ligne 1721

CREATE TABLE INSIDE PROCEDURE

I can't seem to create the table inside this procedure. I read it online that for any DDL, I need to use EXECUTE IMMEDIATE and tried following few examples online. However, even after trying several solutions it keeps failing. '
Error ""ORA-00904: "End": invalid identifier ORA-06512: at
"EXTRACT_AUTOMATED_CHECKS", line 89 "
CREATE OR REPLACE PROCEDURE EXTRACT_AUTOMATED_CHECKS AS
BEGIN
--DROP TABLE
BEGIN
EXECUTE IMMEDIATE ('DROP TABLE extract_checks') ;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
--CREATE TABLE, INDEX
--TABLE
BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE extract_checks
(
Card number(19) NOT NULL PRIMARY KEY
,Customer_Id number(19)
)';
COMMIT;
END;
--INDEX
BEGIN
EXECUTE IMMEDIATE('CREATE INDEX IDX_EXT_CHECKS extract_checks(Customer_Id)');
COMMIT;
END;
SOURCE.DBA_SEND_MAIL(
V_FROM=>'Notification#Company.com;',
V_RECIPIENT => 'employee#company.com',
V_SUBJECT => 'Automated Checks Completed',
V_MESSAGE => 'Automated Checks Completed' );
EXCEPTION WHEN OTHERS THEN
SOURCE.DBA_SEND_MAIL(
V_FROM=>'Notification#Company.com;',
V_RECIPIENT => 'employee#company.com',
V_SUBJECT => 'Automated Checks Failed',
V_MESSAGE => 'Automated Checks Failed' );
RAISE;
END EXTRACT_AUTOMATED_CHECKS;
There is no issue with CREATE TABLE.
you missed ; at the end of last call to send mail in exception block.
SOURCE.DBA_SEND_MAIL(
V_FROM=>'Notification#Company.com;',
V_RECIPIENT => 'employee#company.com',
V_SUBJECT => 'Automated PX Checks Failed',
V_MESSAGE => 'Automated PX Checks Failed' ); <<-- this ; is missing
Cheers!!

Sending email from oracle apex UTL_SMTP with smtp server credentials

Is there a way to send email using UTL_SMTP method by providing smtp server credentials which is not localhost.
i.e. SMTP service is running somewhere else but is not https secures, i want to use utl_smtp package but i dont see any function parameter that take smtp server id or password in it
If you want to use UTL_SMTP, you provide the SMTP host when you call utl_smtp.open_connection.
Example code (taken from Oracle docs);
DECLARE
c UTL_SMTP.CONNECTION;
PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS
BEGIN
UTL_SMTP.WRITE_DATA(c, name || ': ' || header || UTL_TCP.CRLF);
END;
BEGIN
c := UTL_SMTP.OPEN_CONNECTION('smtp-server.acme.com');
UTL_SMTP.HELO(c, 'foo.com');
UTL_SMTP.MAIL(c, 'sender#foo.com');
UTL_SMTP.RCPT(c, 'recipient#foo.com');
UTL_SMTP.OPEN_DATA(c);
send_header('From', '"Sender" <sender#foo.com>');
send_header('To', '"Recipient" <recipient#foo.com>');
send_header('Subject', 'Hello');
UTL_SMTP.WRITE_DATA(c, UTL_TCP.CRLF || 'Hello, world!');
UTL_SMTP.CLOSE_DATA(c);
UTL_SMTP.QUIT(c);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
BEGIN
UTL_SMTP.QUIT(c);
EXCEPTION
WHEN UTL_SMTP.TRANSIENT_ERROR OR UTL_SMTP.PERMANENT_ERROR THEN
NULL; -- When the SMTP server is down or unavailable, we don't have
-- a connection to the server. The QUIT call raises an
-- exception that we can ignore.
END;
raise_application_error(-20000,
'Failed to send mail due to the following error: ' || sqlerrm);
END;
If your smtp server accepts PLAIN, LOGIN or CRAM-MD5 authentication, you need to call utl_smtp.auth as well, e.g.:
DECLARE
c utl_smtp.connection;
BEGIN
c := utl_smtp.open_connection(
host => 'smtp.example.com',
port => 25,
wallet_path => 'file:/oracle/wallets/smtp_wallet',
wallet_password => 'password',
secure_connection_before_smtp => FALSE);
UTL_SMTP.STARTTLS(c);
UTL_SMTP.AUTH(
c => c,
username => 'scott',
password => 'password'
schemes => utl_smtp.all_schemes);
END;
That said, if you're using APEX you may want to consider using APEX_MAIL instead. You configure the host in the APEX instance settings.
Documentation:
UTL_SMTP: https://docs.oracle.com/cd/E11882_01/appdev.112/e40758/u_smtp.htm#ARPLS074
APEX_MAIL: https://docs.oracle.com/database/apex-5.1/AEAPI/APEX_MAIL.htm

ORA-24247 when sending through FTP

I've been trying to solve this problem for days and I always get the same error over and over. I am user Oracle SQL Developer Version 3.2.20.09 and I want to send files through FTP with this code
CREATE OR REPLACE
PROCEDURE subirFTP(dirServer VARCHAR2, port VARCHAR2, usr VARCHAR2, pass VARCHAR2, dirRemitente VARCHAR2, dirDestinatario VARCHAR2, nombreArchivo VARCHAR2)
IS
l_conn UTL_TCP.connection;
BEGIN
l_conn := ftp.login(dirServer,port,usr,pass);
ftp.binary(p_conn => l_conn);
ftp.put(p_conn => l_conn,
p_from_dir => dirRemitente,
p_from_file => nombreArchivo,
p_to_file => dirDestinatario);
ftp.logout(l_conn);
END subirFTP;
Informe de error:
ORA-24247: acceso de red denegado por la lista de control de acceso (ACL)
ORA-06512: en "SYS.UTL_TCP", línea 17
ORA-06512: en "SYS.UTL_TCP", línea 267
ORA-06512: en "WORKFLOW.FTP", línea 76
ORA-06512: en "WORKFLOW.SUBIRFTP", línea 5
ORA-06512: en línea 2
this error would be thown on 11g due to the ACL (access control list) restictions. you need to grant the privs to your "WORKFLOW" user to communicate with the FTP server.
you can see the existing grants in
select * from dba_network_acls
your dba can grant access with something like:
begin
dbms_network_acl_admin.create_acl(acl => 'ftp.xml',
description => 'Enables FTP',
principal => 'WORKFLOW',
is_grant => true,
privilege => 'connect');
end;
/
begin
dbms_network_acl_admin.assign_acl(acl => 'ftp.xml',
host => 'yourhost.com',
lower_port => ftp port);
end;
/
commit;
see the docs for more info.

Resources