Error using UTL_MAIL in a SQL procedure - sql

I am attempting to send an email using a sql procedure, and am getting some errors. Here is my relevant code:
IF cur_email%FOUND THEN
stu_email := schema.get_person_email_adr(v_id);
IF send_email = 'Y' THEN
UTL_MAIL.SEND (sender => v_email_from, -- line of error
recipients => stu_email,
subject => v_email_subject,
mime_type => 'text/html',
message => v_email_body );
END IF;
END IF;
I will be getting multiple "v_id", and am trying to send an email to each one that I get, so I was also wondering if I am doing that correctly? The error that I am getting is this:
PLS-00201: identifier 'UTL_MAIL' must be declared
I am not sure why I am encountering this, Before I made some of these changes, I never received this error, so I believe that the utl_mail set up is not the problem.
Thanks!

UTL_MAIL is not installed by default, because it requires some configuring on the part of the sysadmin team. Find out more.
So, perhaps it has not been installed, or perhaps EXECUTE privilege on it has not been granted to your stored procedure owner.

Related

How to send SQL script results as a table in email body via Automic

I have a workflow which looks like below.
The first task of this workflow runs a simple select * query and the next one sends an email. They are working fine individually. What I want is to send the output of the SQL task as an input to the email task so that it can be attached to the email being sent.
I have tried to manually enter the runId of the SQL task in below field of notification object and it works as expected. But how to make this field take dynamic value from its predecessor instead of a hardcoded one?
Also, is there a way I can include the output of the select * in the email body as a table ?
Update --1
I was able to get a hold of runId of preceding task via the below script. Now only need help with including it in the mail body as opposed to attachement.
:SET &NR# = SYS_ACT_PREV_NR()
:PRINT "RunID of the previous task is &NR#."
Firstly;
Setup
The package is loaded by running the following scripts.
sys/passwordord AS SYSDBA
#$ORACLE_HOME/rdbms/admin/utlmail.sql
#$ORACLE_HOME/rdbms/admin/prvtmail.plb
In addition the SMTP_OUT_SERVER parameter must be set to identify the SMTP server.
CONN sys/password AS SYSDBA
ALTER SYSTEM SET smtp_out_server='smtp.domain.com' SCOPE=SPFILE;
-- Instance restart only necessary in 10gR1.
SHUTDOWN IMMEDIATE
STARTUP
I would suggest you use a mail relay on the database server, rather than connecting directly to an external mail server. The mail relay configuration can be simple, with a reference to "localhost" in the SMTP_OUT_SERVER parameter. Any complexities about connecting to your external mail server are then hidden in the mail relay configuration.
Send Emails
With the configuration complete we can now send a mail using the SEND procedure. It accepts the following parameters.
SENDER : This should be a valid email address.
RECIPIENTS : A comma-separated list of email addresses.
CC : An optional comma-separated list of email addresses.
BCC : An optional comma-separated list of email addresses.
SUBJECT : The subject line for the email.
MESSAGE : The email body.
MIME_TYPE : Set to 'text/plain; charset=us-ascii' by default.
PRIORITY : (1-5) Set to 3 by default.
REPLYTO : Introduced in 11gR2. A valid email address.
Below is an example of the usage.
BEGIN
UTL_MAIL.send(sender => 'me#domain.com',
recipients => 'person1#domain.com,person2#domain.com',
cc => 'person3#domain.com',
bcc => 'myboss#domain.com',
subject => 'UTL_MAIL Test',
message => 'If you get this message it worked!');
END;
/
Send Emails with Attachments
The package also supports sending mails with RAW and VARCHAR2 attachments using the SEND_ATTACH_RAW and SEND_ATTACH_VARCHAR2 packages respectively. They work in a similar fashion to the SEND procedure, with a few extra parameters.
ATTACHMENT : The contents of the attachment. This should be VARCHAR2 or RAW depending on which procedure you call.
ATT_INLINE : Indicates if the attachment should be readable inline. Default FALSE.
ATT_MIME_TYPE : The default is 'text/plain; charset=us-ascii'.
ATT_FILENAME : The name for the attachment.
Below is an example of sending an email with a text attachment.
BEGIN
UTL_MAIL.send_attach_varchar2 (
sender => 'me#domain.com',
recipients => 'person1#domain.com,person2#domain.com',
cc => 'person3#domain.com',
bcc => 'myboss#domain.com',
subject => 'UTL_MAIL Test',
message => 'If you get this message it worked!',
attachment => 'The is the contents of the attachment.',
att_filename => 'my_attachment.txt'
);
END;
/

Authentication - PLS-00488: 'VALIDATE_USER_FROM_DB' must be a type ORA-06550

I'm currently working on an application in Oracle Application Express 5, more specifically I'm trying to create a custom authentication scheme.
I started by creating a function in the SQL command window that would provide my application with a basic level of authentication. I want my users to be able to sign in with their personal student reference number (srn) and date of birth (dob) and this information is taken from the table "student".
create or replace function validate_user_from_db
(p_username in number, p_password in date)
return boolean
as v_pw_check varchar2(1);
begin select 'x' into v_pw_check
from student
where upper(srn) = upper(p_username)
and dob = p_password; apex_util.set_authentication_result(0);
return true; exception when no_data_found then apex_util.set_authentication_result(4);
return false;
end validate_user_from_db;
This function compiled with no errors and so I then went on to create a simple query that would test the function.
declare
vresult varchar2(10);
begin
if validate_user_from_db ('30134852', '08/17/1997') then
DBMS_OUTPUT.PUT_LINE('Welcome Back');
else
DBMS_OUTPUT.PUT_LINE('Error');
end if;
end;
The query was successful and was outputting "Welcome Back" when the correct credentials were filled. Now that everything was working how I'd hoped I went about creating a new authentication scheme. I named it "User Validation" and set the scheme type to custom.
I've been following a tutorial and researching into authentication schemes and it was suggested that in the PL/SQL code I place the line,
return validate_user_from_db;
but when I saved and compiled it I got this error message.
Error Code
In attempt to try and resolve this issue I decided to write the following function in PL/SQL code window and it compiled without any issues, but when I try run the application and use correct login credentials it just hits me with an error message stating "Invalid Login Credentials".
function validate_user_from_db
(p_username in number, p_password in date)
return boolean
as v_pw_check varchar2(1);
begin select 'x' into v_pw_check
from student
where upper(srn) = upper(p_username)
and dob = p_password; apex_util.set_authentication_result(0);
return true; exception when no_data_found then apex_util.set_authentication_result(4);
return false;
end validate_user_from_db;
Any help will be much appreciated as this is my firs time creating authentication with Oracle Apex.
I resolved the issue after messing around with a test application. Turns out a login page is automatically generated depending on the authentication scheme you have set in place. Make sure you delete your existing login page when you change authentication.

SQL SELECT with an exception in a WCF service

I have a method in the WCF service with a SQL query for a basic log in authentication :
SELECT StudentID, Password
FROM tbUserAccounts
WHERE StudentID = #ID AND Password = #Password
If this query does not find any results, will the WCF encounter this as an error / exception or does it return anything?
The reason I ask is because running this method from my Universal Windows app, I am still able to log in with the password box (which is a textbox) being empty. I must be missing something here since the AND statement is there?
I have a try catch also but it doesn't seem to trigger the catch
the Query will return a result set with 0 records
You should test for zero results in the result set
If it doesn't find any results it will still succeed and return an empty result set. You probably need to check and make sure that you are getting 1 and only 1 row back. Also, please don't use this as an actual authentication method for production code.

Give credentials to UTL_MAIL.SEND to bypass ORA-29278

I am trying to send email from a PL/SQL program using UTL_MAIL.Send packaged procedure. But I am getting the following error:
ORA-29278: SMTP transient error: 421 Service not available
I searched on web, and found that this error occurs because there is no SMTP service on the host running my Oracle database.
Now if I want to use another SMTP server, for example , hotmail's "smtp.live.com", i need my user name and password.
How can I give my password into the UTL_MAIL.Send procedure call?
(according to my understanding, in order to use any other SMTP server, I have to provide my username/password). I know that to use UTL_MAIL package, we set the SMTP server with an initialization parameter, and we can give username in "Sender" parameter, but the question is where should we give the password?
Basically you have to use the "lower level" UTL_SMTP package in order to send the various SMTP messages required by the distant SMTP server.
Authentication
From Stefano Ghio's blog:
-- prepare base64 encoded username and password
l_encoded_username := UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(username)));
l_encoded_password := UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(password)));
-- Open connection and send EHLO and AUTH messages
l_conn := UTL_SMTP.open_connection(smtpHost, smtpPort);
UTL_SMTP.ehlo(l_conn, smtpHost);--DO NOT USE HELO
UTL_SMTP.command(l_conn, 'AUTH', 'LOGIN');
UTL_SMTP.command(l_conn, l_encoded_username);
UTL_SMTP.command(l_conn, l_encoded_password);
The main issue here is that you need be able to send the AUTH message using the "right" authentication scheme for your server. I can't say for "smtp.live.com" specifically, but depending the server's configuration, they might be different authentication scheme, like PLAIN and LOGIN, DIGEST_MD5, ... Usually (always ?) the parameters (username, password) are base64 encoded.
Sending mail
But the bad news is, since you are now using a low-level library, you have to implement the client part of the SMTP protocol yourself. From the same source as above (edited by myself to only keep the absolutely minimum necessary stuff):
UTL_SMTP.mail(l_conn, mailFrom);
UTL_SMTP.rcpt(l_conn, rcptTo);
[...]
--start multi line message
UTL_SMTP.open_data(l_conn);
--prepare mail header
UTL_SMTP.write_data(l_conn, 'To: ' || rcptTo || crlf);
UTL_SMTP.write_data(l_conn, 'From: ' || mailFrom || crlf);
UTL_SMTP.write_data(l_conn, 'Subject: ' || messageSubject || crlf);
--include the message body
UTL_SMTP.write_data(l_conn, messageBody || crlf || crlf);
--send the email and close connection
UTL_SMTP.close_data(l_conn);
UTL_SMTP.quit(l_conn);
Using SSL/TLS
And now, for the very bad news: some server required a secure connection. Claiming something like 530 Must issue a STARTTLS command first. Unfortunately, UTL_SMTP.STARTTLS is only supported starting from Oracle Database 11g release 2 (11.2.0.2).
If you are lucky enougth to use a recent version of Oracle, you should write something like that to open a secure connection with your server:
l_conn := UTL_SMTP.open_connection(l_conn, smtpHost,
wallet_path => 'file:/oracle/wallets/smtp_wallet',
wallet_password => 'password',
secure_connection_before_smtp => FALSE);
UTL_SMTP.starttls(l_conn);
To quote Oracle's documentation:
SSL/TLS requires an Oracle wallet which must be specified when the connection was opened by the OPEN_CONNECTION Functions.
Please see the corresponding documentation to see how to create and manage wallet
Few more readings:
Oracle's documentation about sending e-mail from PL/SQL has some nice examples too depicting you to properly send your mail message using UTL_SMTP.
See the SMTP Authentification Wikipedia page for a transcription of a typical SMTP session.
found this online. seems like either of these should do the trick:
AUTH PLAIN
v_plain_string :=
UTL_RAW.cast_to_varchar2(
UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw('my_user_name'||chr(0)||'my_user_name'||chr(0)||'my_secret_password'))
);
v_connection := UTL_SMTP.open_connection(:v_smtp_server);
UTL_SMTP.ehlo(v_connection, 'mydomain.com'); -- Must use EHLO vs HELO
UTL_SMTP.command(v_connection, 'AUTH', 'PLAIN ' || v_plain_string);
AUTH LOGIN
v_username_b64 :=
UTL_RAW.cast_to_varchar2(
UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(:v_username))
);
v_password_b64 :=
UTL_RAW.cast_to_varchar2(
UTL_ENCODE.base64_encode(UTL_RAW.cast_to_raw(:v_password))
);
v_connection := UTL_SMTP.open_connection(:v_smtp_server);
UTL_SMTP.ehlo(v_connection, 'mydomain.com'); -- Must use EHLO vs HELO
UTL_SMTP.command(v_connection, 'AUTH', 'LOGIN'); -- should receive a 334 response, prompting for username
UTL_SMTP.command(v_connection, v_username_b64); -- should receive a 334 response, prompting for password
UTL_SMTP.command(v_connection, v_password_b64); -- should receive a 235 response, you are authenticated
also i think when you talk about the "Sender" parameter you might be referring to the "From" header for the email itself.

SugarCRM: Getting "Invalid Session ID" error with REST calls

I'm using SugarCRM CE 6.0.3.
When I make REST API calls like get_entry_list(), I always get this error:
{'description': 'The session ID is invalid',
'name': 'Invalid Session ID',
'number': 11}
I am very sure that I am logged in and using the correct session ID. In fact, when I can successfully call get_user_id() and retrieve my own user ID.
Googling has not produced any helpful results, anyone else encountered this problem?
I have found the problem, it is really just a matter of bad documentation on SugarCRM's part. The parameter naming is all wrong in this document:
http://developers.sugarcrm.com/docs/OS/6.0/-docs-Developer_Guides-Sugar_Developer_Guide_6.0-Chapter%202%20Application%20Framework.html#9000259
Simple fix for this problem: Do not use named parameters when making REST calls in SugarCRM. i.e. Use positional parameters (JSON array) for 'rest_data' in API calls.
I encountered this issue with the set_entry api call. For me the issue is one of the values that I was submitting to the call contained special characters that the api couldn't handle. My solution was to urlencode the value, and Sugar decodes it on their end. See below:
$name = "abc's ; 10/10/2013";
$values = array(
"name"=>$name
);
$sugar->set_entry("Accounts", $values);
The above threw the Invalid session ID error. Below is the code that works:
$name = urlencode("abc's ; 10/10/2013");
$values = array(
"name"=>$name
);
$sugar->set_entry("Accounts", $values);