Emailing attachments using utl_smtp
Hi,
How can one use UTL_SMTP to send a file generated by email as an attachment. I created a MS Excel Spreadsheet on an apps server. I would like to send it as an attachment to recipients.
Oneway is to use the uuencode in UNIX script.
But I wanted it to be sent using UTL_SMTP. Could someone please suggest a right direction?
Thanks
uds
Hi Billy,
Thanks for the updates. My question is how to send a file that is already created as an attachment using UTL_SMTP.
It is like a truck delivering cargo. It does not make the cargo. In other words, it does construct a completed e-mail for your.
The file (cargo) is created using UTL_FILE in the apps server. Now I want to use UTL_SMTP to send an email to a list of recipients with this file as an attachment.
The header that includes data like the e-mail subject, the type of mail and so on. Followed by the body that contains the actual e-mail contents, which includes any attachments
That is the isuue I am confronting. How do I attach the file already created? Here is the snippet of my code that formulates the body of the email.
UTL_SMTP.helo (srvr_conn, p_server);
UTL_SMTP.mail (srvr_conn, p_sender);
UTL_SMTP.rcpt (srvr_conn, p_recipient);
UTL_SMTP.open_data (srvr_conn);
UTL_SMTP.write_data (srvr_conn,
'Content-Type: text/html' || UTL_TCP.crlf
UTL_SMTP.write_data (srvr_conn,
'Subject : Transaction types available'
|| UTL_TCP.crlf
UTL_SMTP.write_data (srvr_conn, 'To : ' || p_recipient || UTL_TCP.crlf);
UTL_SMTP.write_data
(srvr_conn,
'<br> <html> <body> <table border="1" bordercolor="black">
<tr>
<th><font SIZE="2"><b>Trx ID</b></FONT></th>
<th><font SIZE="2"><b>Trx Code</b></FONT></th>
</tr>'
FOR rec IN c1
LOOP
UTL_SMTP.write_data (srvr_conn,
'<tr>
<td><font SIZE="1">'|| rec.trx_id|| '</FONT></td>
<td><font SIZE="1">'|| rec.trx_code|| '</FONT></td>
</tr>'
END LOOP;
UTL_SMTP.write_data (srvr_conn, '<br></table></p></body> </html>');
UTL_SMTP.close_data (srvr_conn);
UTL_SMTP.quit (srvr_conn);
Now, how do I ask it to pick the file from server and attach it to the email before "quitting SMTP"? Firstly, can I do that? If yes, can you please suggest me the means how to do it? Your help is much appreciated.
Thanks
uds
PS: c1 is a cursor stmnt; p_server, p_sender, p_recipient are derived from flex values and srvr_conn is type UTL_SMTP.connection
Similar Messages
-
Hi All,
I have a requirement to send email to an external ID for which I am using FM SO_NEW_DOCUMENT_ATT_SEND_API1.Can anyone give a sample code to send multiple excel attachments using this function module.
Points would be rewarded.
Thanks
Archana.Check the Thread,,
Re: more than 1 attachements/sheets in SO_DOCUMENT_SEND_API1 -
Auto saving email attachments using date email received as the name of the attachment file
I would like to be able to batch save hundreds of email attachments in my inbox to a specified folder and use the date and time email was received as the name of the file. i found sample script that would give it timestamp but would prefer date and time of receipt. any help would be greatly appreciated.
thanks.I found the below script online but it does not seem to be naming correctly. it appears that the month always defaults to 12. perhaps somehow could suggest a fix?
set theAttachmentPath to (path to desktop) as text
tell application "Mail"
set a to selection
end tell
repeat with s in a
tell application "Mail"
set current_date to date received of s
set CurrentSender to sender of s
end tell
set current_date to AppleScriptDateToString(current_date)
tell application "Mail" to set Attached to mail attachments of s
repeat with ThisAttach in Attached
tell application "Mail" to set FileName to name of ThisAttach
if FileName ends with ".pdf" then
set FileName to current_date & " from " & CurrentSender & ".pdf" as text
set FileName to checknamewith_pdfsuffix(FileName, theAttachmentPath, false)
tell application "Mail" to save ThisAttach in theAttachmentPath & (FileName)
end if
end repeat
end repeat
on AppleScriptDateToString(a)
set b to current date
set monthnames to {}
repeat with i from 1 to 12
set month of b to i
set monthnames to monthnames & {(month of b) as text}
end repeat
set Y to (year of b)
set M to 0
repeat with t in monthnames
set M to M + 1
if t as text = (month of b) as text then
exit repeat
end if
end repeat
set M to Twodigits(M)
set D to Twodigits(day of b)
set hh to Twodigits(hours of b)
set mm to (minutes of b)
set ss to Twodigits(seconds of b)
return Y & "-" & M & "-" & D as text
end AppleScriptDateToString
on Twodigits(a)
return (characters -2 through -1 of (("0" & a) as text)) as text
end Twodigits
on checknamewith_pdfsuffix(n, D, looped)
tell application "Finder"
set thefiles to name of every item of (D as alias)
end tell
if thefiles contains n then
if looped = false then
set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text
checknamewith_pdfsuffix(n, D, true)
else
set tmp to (last word of ((characters 1 through -5 of n) as text) as integer)
set tmpcount to (count of characters of (tmp as text)) + 5
set tmp to tmp + 1
set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text
checknamewith_pdfsuffix(n, D, true)
end if
else
return n
end if
end checknamewith_pdfsuffix -
IPad will not open email attachments using webmail ?
Hi We own a iPad 3, OS5.1.1 ( the iPad belongs to my wife ) therefore she has her own account set up using the iPad's email programme ( I do not have my email account on this iPad as we want to keep our accounts seperate) When I login to my email account using my webmail I can not open any attachement wheather it be pdf.doc,txt ext etc ? when I select the document ( attachment ) nothing happens - if my wife has any pdf's attached to her emails using the iPad's email programme they open without any problems ? I have spoken to my email provider as a webmail programme should be able to open attachments connected with an email - but they are not at all helpful they are suggesting that the problem is with the iPad which I am not at all convinced by unless I am advised other wise by anybody with a technical experience of this problem. We have 3rd party apps such as PDF Reader and also iBooks which can read ePub files - can any body help with this - I am not talking about downloading files here, I am simply talking about the capacity to just open and read an attachment which I can not do with our iPad - we have plenty of memory and drive space, therefore I think it is a bug which might be due to a combination of the webmail programme and the iPad not working with each other - or simply something that is not apparent to me Any help would be much appreciated Many Thanks Mavis
I had the same problem.
A colleage sent an email from an ipad with a pdf attachment (via FileApp Pro) and while the email arrived OK and the Inbox shows an attachment there is nowhere to click and open the document from.
The name of the pdf is listed in the detail of the message but there is no icon within the message to open it with.
The first page of the pdf file was shown within the message too.
The trick was to click and hold somewhere within the first page (displayed in the email).
This prompts the "Open in ..." popup from which you can open your preferred pdf reader to see the whole document.
Hope this helps. -
Random new line issue in email attachment using utl_smtp need your help.
Hi
I am getting one problem unable to solve, need your help.
I am creating one csv attachment in email using utl_smtp.
but there are random newline in the attachment value so the csv getting corrupted.
following is the attachment code
FOR C2 IN CUR_VALIDATION_ERROR
LOOP
IF CUR_VALIDATION_ERROR%ROWCOUNT = 1 THEN
UTL_SMTP.write_data(l_mail_conn,'"LOG DATE","DATA ERROR IDENTIFIER","EMPLOYEE ID","SOURCE SYSTEM","SOURCE FILE ROW","ERROR LEVEL","ERROR MESSAGE"'||CHR(13));
END IF;
UTL_SMTP.write_data(l_mail_conn,
chr(34)||C2.LOG_DT||chr(34)||chr(44)||
chr(34)||C2.DATA_ERR_ID||chr(34)||chr(44)||
chr(34)||C2.EMPE_ID||chr(34)||chr(44)||
chr(34)||C2.SOURCE||chr(34)||chr(44)||
chr(34)||C2.SOURCE_ROW||chr(34)||chr(44)||
chr(34)||C2.ERROR_LEVEL||chr(34)||chr(44)||
chr(34)||C2.ERR_MSG_DSCR||chr(34)||
CHR(13));
END LOOP;Thank you hm, but that is not the case, bcz I found newline character inside a sequence number. Its not possible to have newline character inside a sequence number
"LOG DATE","DATA ERROR IDENTIFIER","EMPLOYEE ID","SOURCE SYSTEM","SOURCE FILE ROW","ERROR LEVEL","ERROR MESSAGE"
"02-MAY-2012","7893660","123","XYZ","44952","WARNING","[02-MAY-12] - The value in field [PHONE]"
"02-MAY-2012","7893663","12
4","XYZ","52382","WARNING","[02-MAY-12] - The value in field [ADDRESS]" -
Email file attachments using utl_smtp
I would like to attach a PDF file to an e-mail. The PDF file
will be generated from Oracle Reports. I will send the e-mail
from Oracle Forms using the utl_smtp procedure. The email will
have some text in the body of the email and I would like to
attach the PDF file.
I have know how to send an emial using the utl_smtp procedure
with a subject and body text. I have seen an example of how to
attach in-line text, but wasn't sure how to attache a file.
In example It appears that the attached text file is generated
from inline text and they are not attaching a Text File.
Can anyone help.
P.S. I am doing this both from the WEB and Client ServerThe issue has to do with the space in filename in the utl_http.begin_request. I got it fixed. Thanks
-
EMail Attachments using PDF file
Hi guys
I have developed an application using Oracle 8i and forms 6i. There is a report developed in 6i repors and saved as PDF format. this file I have to send as attachemnts to the employees.
Is there any way to send PDF file as attachemnts using forms 6i.
REgards
Linga Murthy MudigondaThe easiest way (and OS independent, so you don't need Outlook or blat or something like that) is to send the email and attachment from the database. Package utl_smtp won't let you do that, but with Java mail you can. So, after you generated your report, you can mail it using Java mail.
Everything is described very clearly in note 120994.1 on Metalink (How to Send E-mail With Attachments from PL/SQL Using Java Stored Procedures). -
Sending email attachments using unix shell script
hi
I want to send report generated my spooled file as attachment using unix shell script.
Can somebody help me out ?
many thanksthanks a tonn it worked.
but i have another issue is it possible to add names in CC also ?
Also here is my code which spools the output of SP to a txt file. the File name is generated dynamically.
as shown below:
I need to send this generated file as attachement.
how do I do this? Here the shell script
=========================================================
#!/bin/sh
ORA_USER=scott
ORA_PWD=tiger
#Get the input parameter
if [ ! "$1" ]; then
STR="NULL"
else
STR="'"$1"'"
fi
#echo "exec pkg1($STR);"
#Connecting to oracle
sqlplus -s <<EOF
$ORA_USER/$[email protected]
---sql plus enviornment settings
set linesize 160
set pagesize 60
set serveroutput on size 1000000 for wra
set feedback off
set termout off
column dcol new_value mydate noprint
select to_char(sysdate,'YYYYMMDDHH24MISS') dcol from dual;
spool &mydate.report.txt
exec pkg1($STR);
spool off
EOF
exit
=========================================================
the file name will take sysdate as name so that every time a new file will be generated.
this file I need to send as attachment.
null -
Parsing email attachments using Javamail
Hi All,
I'm working on a multimedia application where I have to parse emails using Javamail.
Here is a problem that i'm facing.
When i'm receiving an attachment from icloud.com, i'm not able to get the filename whereas i'm able to get the fileSize and fileContent.
I'm using Part.getFileName() to get the file name. and Part.getFileSize() and Part.getInputStream() to get size and the content.
When i checked the attachment header, I could see the name encoded in ISO-8859-1.
--Boundary_(ID_kS5Ng+OB35IVBfC+scPoMA)
Content-id:[email protected]
Content-type:image/jpeg;
name*1*=utf-8"%20%32%33%30%32%32%30%31%33%37%32%35;
name*2*=%2E%6A%70%67
Content-transfer-encoding:BASE64
Content-disposition:inline;
filename*1*=utf-8"%20%32%33%30%32%32%%30%32%33%37%32%35
filename*2*=%2E%6A%70%67
In both the cases above, Part.getFileName() is returning null.
I set the parameter mail.mime.decodeparameters="true".Still it is not working.
It would be great if someone can suggest a solution for this.
Thanks
ShyamaJust adding to the problem description:
--Boundary_(ID_kzWHPgILjZtH2UtdriatGg)
Content-id: <[email protected]>
Content-type: image/jpeg; name*1*=ISO-8859-1''%32%2E%6A%70%67
Content-transfer-encoding: BASE64
Content-disposition: inline; filename*1*=ISO-8859-1''%32%2E%6A%70%67
This is the header which could see in another mail. -
Send email attachments using Java through Outlook Express
Hi
Can anyone suggest how we can interface Outlook Express with java in
order to send email?
I want to add 'Email Friend' link in my project, on clicking on this
link Outlook Express should open with a specified file attached.
I searched on net also, but I am getting only JOC everywhere which is
not freeware.
Please suggest me on the same that how it can be done?
Thanks
AshiYou can send files to Outlook Express using following:
using a windows exe runmenu. exe
File file = new File("./lib/runmenu.exe");
try
String commandForNotesMail =
file.getCanonicalPath() +
" \"/exec: send to\\mail recipient\" " + filename;
System.out.println("Cononical Path :" + commandForNotesMail);
Runtime.getRuntime()
.exec(commandForNotesMail);
catch (IOException e)
e.printStackTrace();
return true;
} -
How to add disclaimer on smtp server and use it using utl_smtp.
We use utl_smtp to send emails. The requirement is to add a disclaimer on smtp server so that any outgoing emails sent using utl_smtp should have this disclaimer.
Is that possible and how do we acheive this?
--Thank you for any suggestions.Hello Rahul,
the code would be something like this:
loop at itab1.
move-corresponding itab1 to itab2.
collect itab2.
endloop.
Hope this helps.
Regards,
Himanshu -
Sending email using UTL_SMTP
Dear experts,
I am trying to send an email using UTL_SMTP (i switched from UTL_MAIL to UTL_SMTP since i need to send mails with large attachments - BLOB). I am using the demo_mail package given here:
http://www.oracle.com/technology/sample_code/tech/pl_sql/htdocs/Utl_Smtp_Sample.html
While running the program I am getting an error message (from the error.log) saying:
[Mon Aug 04 14:00:21 2008] [error] [client 192.168.0.7] mod_plsql: /ns/email_p.send HTTP-404 ORA-29279: SMTP permanent error: 530 5.7.0 Must issue a STARTTLS command first. 9sm12723808qbw.6\nORA-06512: at "SYS.UTL_SMTP", line 20\nORA-06512: at "SYS.UTL_SMTP", line 98\nORA-06512: at "SYS.UTL_SMTP", line 221\nORA-06512: at "NEXTSTEP.SEND_EMAIL_HELPER", line 258\nORA-06512: at "NEXTSTEP.SEND_EMAIL_HELPER", line 119\nORA-06512: at "NEXTSTEP.EMAIL_P", line 33\nORA-06512: at line 31\n, referer: file:///C:/Documents and Settings/Mayank/My Documents/Flex Builder 3/ns5/bin-debug/main.swf
My code is as follows:
PACKAGE DECLARATION. This is the DEMO_MAIL package posted under above link (I have renamed it).
CREATE OR REPLACE PACKAGE NEXTSTEP.send_email_helper IS
----------------------- Customizable Section -----------------------
-- Customize the SMTP host, port and your domain name below.
smtp_host VARCHAR2(256) := 'smtp.gmail.com';
smtp_port PLS_INTEGER := 587;
smtp_domain VARCHAR2(256) := null;
-- Customize the signature that will appear in the email's MIME header.
-- Useful for versioning.
MAILER_ID CONSTANT VARCHAR2(256) := 'Mailer by Oracle UTL_SMTP';
--------------------- End Customizable Section ---------------------
-- A unique string that demarcates boundaries of parts in a multi-part email
-- The string should not appear inside the body of any part of the email.
-- Customize this if needed or generate this randomly dynamically.
BOUNDARY CONSTANT VARCHAR2(256) := '-----7D81B75CCC90D2974F7A1CBD';
FIRST_BOUNDARY CONSTANT VARCHAR2(256) := '--' || BOUNDARY || utl_tcp.CRLF;
LAST_BOUNDARY CONSTANT VARCHAR2(256) := '--' || BOUNDARY || '--' ||
utl_tcp.CRLF;
-- A MIME type that denotes multi-part email (MIME) messages.
MULTIPART_MIME_TYPE CONSTANT VARCHAR2(256) := 'multipart/mixed; boundary="'||
BOUNDARY || '"';
MAX_BASE64_LINE_WIDTH CONSTANT PLS_INTEGER := 76 / 4 * 3;
-- A simple email API for sending email in plain text in a single call.
-- The format of an email address is one of these:
-- someone@some-domain
-- "Someone at some domain" <someone@some-domain>
-- Someone at some domain <someone@some-domain>
-- The recipients is a list of email addresses separated by
-- either a "," or a ";"
PROCEDURE mail(sender IN VARCHAR2,
recipients IN VARCHAR2,
subject IN VARCHAR2,
message IN VARCHAR2);
-- Extended email API to send email in HTML or plain text with no size limit.
-- First, begin the email by begin_mail(). Then, call write_text() repeatedly
-- to send email in ASCII piece-by-piece. Or, call write_mb_text() to send
-- email in non-ASCII or multi-byte character set. End the email with
-- end_mail().
FUNCTION begin_mail(sender IN VARCHAR2,
recipients IN VARCHAR2,
subject IN VARCHAR2,
mime_type IN VARCHAR2 DEFAULT 'text/plain',
priority IN PLS_INTEGER DEFAULT NULL)
RETURN utl_smtp.connection;
-- Write email body in ASCII
PROCEDURE write_text(conn IN OUT NOCOPY utl_smtp.connection,
message IN VARCHAR2);
-- Write email body in non-ASCII (including multi-byte). The email body
-- will be sent in the database character set.
PROCEDURE write_mb_text(conn IN OUT NOCOPY utl_smtp.connection,
message IN VARCHAR2);
-- Write email body in binary
PROCEDURE write_raw(conn IN OUT NOCOPY utl_smtp.connection,
message IN RAW);
-- APIs to send email with attachments. Attachments are sent by sending
-- emails in "multipart/mixed" MIME format. Specify that MIME format when
-- beginning an email with begin_mail().
-- Send a single text attachment.
PROCEDURE attach_text(conn IN OUT NOCOPY utl_smtp.connection,
data IN VARCHAR2,
mime_type IN VARCHAR2 DEFAULT 'text/plain',
inline IN BOOLEAN DEFAULT TRUE,
filename IN VARCHAR2 DEFAULT NULL,
last IN BOOLEAN DEFAULT FALSE);
-- Send a binary attachment. The attachment will be encoded in Base-64
-- encoding format.
PROCEDURE attach_base64(conn IN OUT NOCOPY utl_smtp.connection,
data IN RAW,
mime_type IN VARCHAR2 DEFAULT 'application/octet',
inline IN BOOLEAN DEFAULT TRUE,
filename IN VARCHAR2 DEFAULT NULL,
last IN BOOLEAN DEFAULT FALSE);
-- Send an attachment with no size limit. First, begin the attachment
-- with begin_attachment(). Then, call write_text repeatedly to send
-- the attachment piece-by-piece. If the attachment is text-based but
-- in non-ASCII or multi-byte character set, use write_mb_text() instead.
-- To send binary attachment, the binary content should first be
-- encoded in Base-64 encoding format using the demo package for 8i,
-- or the native one in 9i. End the attachment with end_attachment.
PROCEDURE begin_attachment(conn IN OUT NOCOPY utl_smtp.connection,
mime_type IN VARCHAR2 DEFAULT 'text/plain',
inline IN BOOLEAN DEFAULT TRUE,
filename IN VARCHAR2 DEFAULT NULL,
transfer_enc IN VARCHAR2 DEFAULT NULL);
-- End the attachment.
PROCEDURE end_attachment(conn IN OUT NOCOPY utl_smtp.connection,
last IN BOOLEAN DEFAULT FALSE);
-- End the email.
PROCEDURE end_mail(conn IN OUT NOCOPY utl_smtp.connection);
-- Extended email API to send multiple emails in a session for better
-- performance. First, begin an email session with begin_session.
-- Then, begin each email with a session by calling begin_mail_in_session
-- instead of begin_mail. End the email with end_mail_in_session instead
-- of end_mail. End the email session by end_session.
FUNCTION begin_session RETURN utl_smtp.connection;
-- Begin an email in a session.
PROCEDURE begin_mail_in_session(conn IN OUT NOCOPY utl_smtp.connection,
sender IN VARCHAR2,
recipients IN VARCHAR2,
subject IN VARCHAR2,
mime_type IN VARCHAR2 DEFAULT 'text/plain',
priority IN PLS_INTEGER DEFAULT NULL);
-- End an email in a session.
PROCEDURE end_mail_in_session(conn IN OUT NOCOPY utl_smtp.connection);
-- End an email session.
PROCEDURE end_session(conn IN OUT NOCOPY utl_smtp.connection);
END;How should I solve the above error? Can anyone help me with my query please?
If I add the code to start TLS it still gives me an error. If I add the code
utl_smtp.command(conn,'STARTTLS');
utl_smtp.helo(conn, smtp_domain);
under email_sender_help package just before under begin_session function, it gives me an error saying:
ORA-29279: SMTP permanent error: 503 5.5.1 EHLO/HELO first. s27sm2097329qbs.12
So then if i enter the same code after:
utl_smtp.command(conn,'STARTTLS');
utl_smtp.helo(conn, smtp_domain);
It gives me an error:
ORA-29278: SMTP transient error: 421 Service not available
Message was edited by:
Monk
Message was edited by:
Monklook like rely turned off on the server..
check this.. or ask your network guys..
Go to Control Panel->Add or Remove Programs->Click on
Add/Remove Wndows Components
Check IIS check box.
Select Internet Information Service (IIS) option and click on Details button
Check whether SMTP Service is checked or not.
If not selected then select SMTP check box.
This process should be done on server.
It will help out from ORA-29278: SMTP transient error: 421 Service not available problem.
thanks -
Formatting emails:I am using UTL_SMTP package foe sending them.
Hi
I created a PL/SQL job to schedule email sending. The code looks like:
DECLARE
l_mailhost VARCHAR2(64) := 'qiudubcorrel001.qa.local';
l_from VARCHAR2(64) := '[email protected]';
l_to VARCHAR2(64) := '[email protected]';
l_mail_conn UTL_SMTP.connection;
BEGIN
l_mail_conn := UTL_SMTP.open_connection(('10.253.14.240'), 25);
UTL_SMTP.helo(l_mail_conn, '10.253.14.240');
UTL_SMTP.mail(l_mail_conn, l_from);
UTL_SMTP.rcpt(l_mail_conn, l_to);
UTL_SMTP.data(l_mail_conn, 'Single string message.' || Chr(13));
UTL_SMTP.quit(l_mail_conn);
END;
The code works fine, but now I want to add colour to my email, or introduce other formatting as well. How can I do that here?
Thanks,
KamalHere you can find HTML formatting
* PROCEDURE NAME : XX_EMAIL_FILES
* DESCRIPTION:
* ==========
* Sends e-mail (text and/or html, either as a string or from a file)
* to one or more recipients (including cc and/or bcc recipients), along with
* up to 3 file attachments (text and/or binary; default is text/plain), using
* the UTL_SMTP package to send the e-mail, the DBMS_LOB package to read
* binary file attachments, and the UTL_ENCODE package to convert the binary
* attachments to BASE64 for character string (non-binary) transmission.
* BE AWARE THAT A COMMIT MAY BE DONE BY THIS ROUTINE.
* PARAMETERS:
* ==========
* The complete parameter list for the xx_email_files procedure is shown below:
* NAME TYPE DESCRIPTION
* from_name IN Name and e-mail address to put in the From field
* to_names IN Names and e-mail addresses for the To field (separated by
* commas or semicolons)
* subject IN Text string for Subject field
* message IN Text string or text file name for Message, if any
* html_message IN Html string or html file name for Message, if any
* cc_names IN Names and e-mail addresses for the Cc field, if any
* (separated by commas or semicolons)
* bcc_names IN Names and e-mail addresses for the Bcc field, if any
* (separated by commas or semicolons)
* filename1 IN First unix file pathname to attach, if any
* filetype1 IN Mime type of first file (defaults to 'text/plain')
* filename2 IN Second unix file pathname to attach, if any
* filetype2 IN Mime type of second file (defaults to 'text/plain')
* filename3 IN Third unix file pathname to attach, if any
* filetype3 IN Mime type of third file (defaults to 'text/plain')
* Sample names and e-mail addresses are: arun (attaches @),
* [email protected], , xxx , and
* "xxx"
* A sample call in PL/SQL is shown below, which sends a text and html message,
* plus a text file and two binary files (note: the slash after "end;" must be
* the first character on it's line):
* begin
* xx_email_files(from_name => '[email protected]' ,
* to_names => '[email protected]',
* subject => 'A test',
* message => 'A TEST MESSAGE',
* html_message => '
A test message
* filename1 => '/ora_appl/oracle/11.5.0/data/xxx.pdf',
* filename2 => '/usr/tmp/115apug.pdf',
* filetype2 => 'application/pdf',
* filename3 => '',
* filetype3 => 'image/jpeg'
* end
* If the message or html_message string has a file name in it (starting with
* a forward slash), the text or html file is copied into the e-mail as the
* message or html message; otherwise, the message or html_message is copied
* into the e-mail as-is.
* Attachment file types (mime types) that I've tested include:
* text/plain,
* text/html,
* image/jpeg,
* image/gif,
* application/pdf,
* application/msword
* A list of mime types can be seen at:
* http://www.webmaster-toolkit.com/mime-types.shtml
* If the mime type does not begin with "text", it is assumed to be a binary
* file that will be encoded as base64 before transmission.
CREATE OR REPLACE PROCEDURE xx_email_files (
from_name VARCHAR2,
to_names VARCHAR2,
subject VARCHAR2,
MESSAGE VARCHAR2 DEFAULT NULL,
html_message VARCHAR2 DEFAULT NULL,
cc_names VARCHAR2 DEFAULT NULL,
bcc_names VARCHAR2 DEFAULT NULL,
filename1 VARCHAR2 DEFAULT NULL,
filetype1 VARCHAR2 DEFAULT 'text/plain',
filename2 VARCHAR2 DEFAULT NULL,
filetype2 VARCHAR2 DEFAULT 'text/plain',
filename3 VARCHAR2 DEFAULT NULL,
filetype3 VARCHAR2 DEFAULT 'text/plain'
IS
-- Change the SMTP host name and port number below to your own values,
-- if not localhost on port 25:
smtp_host VARCHAR2 (256) := 'localhost';
smtp_port NUMBER := 25;
-- Change the boundary string, if needed, which demarcates boundaries of
-- parts in a multi-part email, and should not appear inside the body of
-- any part of the e-mail:
boundary CONSTANT VARCHAR2 (256) := 'CES.Boundary.DACA587499938898';
recipients VARCHAR2 (32767);
directory_path VARCHAR2 (256);
file_name VARCHAR2 (256);
crlf VARCHAR2 (2) := CHR (13) || CHR (10);
mesg VARCHAR2 (32767);
conn UTL_SMTP.connection;
l_length NUMBER;
l_sub VARCHAR2 (32767);
TYPE varchar2_table IS TABLE OF VARCHAR2 (256)
INDEX BY BINARY_INTEGER;
file_array varchar2_table;
type_array varchar2_table;
i BINARY_INTEGER;
-- Function to return the next email address in the list of email addresses,
-- separated by either a "," or a ";". From Oracle's demo_mail. The format
-- of mailbox may be in one of these:
-- someone@some-domain
-- "Someone at some domain"
-- Someone at some domain
FUNCTION get_address (addr_list IN OUT VARCHAR2)
RETURN VARCHAR2
IS
addr VARCHAR2 (256);
i PLS_INTEGER;
FUNCTION lookup_unquoted_char (str IN VARCHAR2, chrs IN VARCHAR2)
RETURN PLS_INTEGER
IS
c VARCHAR2 (5);
i PLS_INTEGER;
len PLS_INTEGER;
inside_quote BOOLEAN;
BEGIN
inside_quote := FALSE;
i := 1;
len := LENGTH (str);
WHILE (i <= len)
LOOP
c := SUBSTR (str, i, 1);
IF (inside_quote)
THEN
IF (c = '"')
THEN
inside_quote := FALSE;
ELSIF (c = '\')
THEN
i := i + 1; -- Skip the quote character
END IF;
GOTO next_char;
END IF;
IF (c = '"')
THEN
inside_quote := TRUE;
GOTO next_char;
END IF;
IF (INSTR (chrs, c) >= 1)
THEN
RETURN i;
END IF;
<>
i := i + 1;
END LOOP;
RETURN 0;
END;
BEGIN
addr_list := LTRIM (addr_list);
i := lookup_unquoted_char (addr_list, ',;');
IF (i >= 1)
THEN
addr := SUBSTR (addr_list, 1, i - 1);
addr_list := SUBSTR (addr_list, i + 1);
ELSE
addr := addr_list;
addr_list := '';
END IF;
i := lookup_unquoted_char (addr, '<');
IF (i >= 1)
THEN
addr := SUBSTR (addr, i + 1);
i := INSTR (addr, '>');
IF (i >= 1)
THEN
addr := SUBSTR (addr, 1, i - 1);
END IF;
END IF;
RETURN addr;
END;
-- Procedure to split a file pathname into its directory path and file name
-- components.
PROCEDURE split_path_name (
file_path IN VARCHAR2,
directory_path OUT VARCHAR2,
file_name OUT VARCHAR2
IS
pos NUMBER;
BEGIN
-- Separate the filename from the directory name
pos := INSTR (file_path, '/', -1);
IF pos = 0
THEN
pos := INSTR (file_path, '\', -1);
END IF;
IF pos = 0
THEN
directory_path := NULL;
ELSE
directory_path := SUBSTR (file_path, 1, pos - 1);
END IF;
file_name := SUBSTR (file_path, pos + 1);
END;
-- Procedure to append a file's contents to the e-mail
PROCEDURE append_file (
directory_path IN VARCHAR2,
file_name IN VARCHAR2,
file_type IN VARCHAR2,
conn IN OUT UTL_SMTP.connection
IS
generated_name VARCHAR2 (30)
:= 'CESDIR' || TO_CHAR (SYSDATE, 'HH24MISS');
directory_name VARCHAR2 (30);
file_handle UTL_FILE.file_type;
bfile_handle BFILE;
bfile_len NUMBER;
pos NUMBER;
read_bytes NUMBER;
line VARCHAR2 (1000);
DATA RAW (200);
my_code NUMBER;
my_errm VARCHAR2 (32767);
BEGIN
BEGIN
-- Grant access to the directory, unless already defined, and open
-- the file (as a bfile for a binary file, otherwise as a text file).
BEGIN
line := directory_path;
SELECT dd.directory_name
INTO directory_name
FROM dba_directories dd
WHERE dd.directory_path = line AND ROWNUM = 1;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
directory_name := generated_name;
END;
IF directory_name = generated_name
THEN
EXECUTE IMMEDIATE 'create or replace directory '
|| directory_name
|| ' as '''
|| directory_path
|| '''';
EXECUTE IMMEDIATE 'grant read on directory '
|| directory_name
|| ' to public';
END IF;
IF SUBSTR (file_type, 1, 4) != 'text'
THEN
bfile_handle := BFILENAME (directory_name, file_name);
bfile_len := DBMS_LOB.getlength (bfile_handle);
pos := 1;
DBMS_LOB.OPEN (bfile_handle, DBMS_LOB.lob_readonly);
ELSE
file_handle := UTL_FILE.fopen (directory_name, file_name, 'r');
END IF;
-- Append the file contents to the end of the message
LOOP
-- If it is a binary file, process it 57 bytes at a time,
-- reading them in with a LOB read, encoding them in BASE64,
-- and writing out the encoded binary string as raw data
IF SUBSTR (file_type, 1, 4) != 'text'
THEN
IF pos + 57 - 1 > bfile_len
THEN
read_bytes := bfile_len - pos + 1;
ELSE
read_bytes := 57;
END IF;
DBMS_LOB.READ (bfile_handle, read_bytes, pos, DATA);
UTL_SMTP.write_raw_data (conn, UTL_ENCODE.base64_encode (DATA));
pos := pos + 57;
IF pos > bfile_len
THEN
EXIT;
END IF;
-- If it is a text file, get the next line of text, append a
-- carriage return / line feed to it, and write it out
ELSE
UTL_FILE.get_line (file_handle, line);
UTL_SMTP.write_data (conn, line || crlf);
END IF;
END LOOP;
-- Output any errors, except at end when no more data is found
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN OTHERS
THEN
my_code := SQLCODE;
my_errm := SQLERRM;
DBMS_OUTPUT.put_line ('Error code ' || my_code || ': ' || my_errm);
END;
-- Close the file (binary or text)
IF SUBSTR (file_type, 1, 4) != 'text'
THEN
DBMS_LOB.CLOSE (bfile_handle);
ELSE
UTL_FILE.fclose (file_handle);
END IF;
IF directory_name = generated_name
THEN
EXECUTE IMMEDIATE 'drop directory ' || directory_name;
END IF;
END;
BEGIN
-- Load the three filenames and file (mime) types into an array for
-- easier handling later
file_array (1) := filename1;
file_array (2) := filename2;
file_array (3) := filename3;
type_array (1) := filetype1;
type_array (2) := filetype2;
type_array (3) := filetype3;
-- Open the SMTP connection and set the From and To e-mail addresses
conn := UTL_SMTP.open_connection (smtp_host, smtp_port);
UTL_SMTP.helo (conn, smtp_host);
recipients := from_name;
UTL_SMTP.mail (conn, get_address (recipients));
recipients := to_names;
WHILE recipients IS NOT NULL
LOOP
UTL_SMTP.rcpt (conn, get_address (recipients));
END LOOP;
recipients := cc_names;
WHILE recipients IS NOT NULL
LOOP
UTL_SMTP.rcpt (conn, get_address (recipients));
END LOOP;
recipients := bcc_names;
WHILE recipients IS NOT NULL
LOOP
UTL_SMTP.rcpt (conn, get_address (recipients));
END LOOP;
UTL_SMTP.open_data (conn);
-- Build the start of the mail message
mesg :=
'Date: '
|| TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss')
|| crlf
|| 'From: '
|| from_name
|| crlf
|| 'Subject: '
|| subject
|| crlf
|| 'To: '
|| to_names
|| crlf;
IF cc_names IS NOT NULL
THEN
mesg := mesg || 'Cc: ' || cc_names || crlf;
END IF;
IF bcc_names IS NOT NULL
THEN
mesg := mesg || 'Bcc: ' || bcc_names || crlf;
END IF;
mesg :=
mesg
|| 'Mime-Version: 1.0'
|| crlf
|| 'Content-Type: multipart/mixed; boundary="'
|| boundary
|| '"'
|| crlf
|| crlf
|| 'This is a Mime message, which your current mail reader may not'
|| crlf
|| 'understand. Parts of the message will appear as text. If the remainder'
|| crlf
|| 'appears as random characters in the message body, instead of as'
|| crlf
|| 'attachments, then you''ll have to extract these parts and decode them'
|| crlf
|| 'manually.'
|| crlf
|| crlf;
UTL_SMTP.write_data (conn, mesg);
-- Write the text message or message file, if any
IF MESSAGE IS NOT NULL
THEN
mesg :=
|| boundary
|| crlf
|| 'Content-Type: text/plain; name="message.txt"; charset=US-ASCII'
|| crlf
|| 'Content-Disposition: inline; filename="message.txt"'
|| crlf
|| 'Content-Transfer-Encoding: 7bit'
|| crlf
|| crlf;
UTL_SMTP.write_data (conn, mesg);
IF SUBSTR (MESSAGE, 1, 1) = '/'
THEN
split_path_name (MESSAGE, directory_path, file_name);
append_file (directory_path, file_name, 'text', conn);
UTL_SMTP.write_data (conn, crlf);
ELSE
UTL_SMTP.write_data (conn, MESSAGE || crlf);
END IF;
END IF;
IF html_message IS NOT NULL
THEN
mesg :=
|| boundary
|| crlf
|| 'Content-Type: text/html; name="message.html"; charset=US-ASCII'
|| crlf
|| 'Content-Disposition: inline; filename="message.html"'
|| crlf
|| 'Content-Transfer-Encoding: 7bit'
|| crlf
|| crlf;
UTL_SMTP.write_data (conn, mesg);
IF SUBSTR (html_message, 1, 1) = '/'
THEN
split_path_name (html_message, directory_path, file_name);
append_file (directory_path, file_name, 'text', conn);
UTL_SMTP.write_data (conn, crlf);
ELSE
UTL_SMTP.write_data (conn, html_message || crlf);
END IF;
END IF;
-- Append the files
FOR i IN 1 .. 3
LOOP
-- If the filename has been supplied ...
IF file_array (i) IS NOT NULL
THEN
split_path_name (file_array (i), directory_path, file_name);
-- Generate the MIME boundary line according to the file (mime) type
-- specified.
mesg := crlf || '--' || boundary || crlf;
SELECT INSTR (file_name, '.')
INTO l_length
FROM DUAL;
SELECT SUBSTR (file_name, 1, l_length - 1) || '.pdf'
INTO l_sub
FROM DUAL;
IF SUBSTR (type_array (i), 1, 4) != 'text'
THEN
mesg :=
mesg
|| 'Content-Type: '
|| type_array (i)
|| '; name="'
|| file_name
|| '"'
|| crlf
|| 'Content-Disposition: attachment; filename="'
|| file_name
|| '"'
|| crlf
|| 'Content-Transfer-Encoding: base64'
|| crlf
|| crlf;
ELSE
mesg :=
mesg
|| 'Content-Type: application/octet-stream; name="'
|| file_name
|| '"'
|| crlf
|| 'Content-Disposition: attachment; filename="'
|| file_name
|| '"'
|| crlf
|| 'Content-Transfer-Encoding: 7bit'
|| crlf
|| crlf;
END IF;
UTL_SMTP.write_data (conn, mesg);
-- Append the file contents to the end of the message
append_file (directory_path, file_name, type_array (i), conn);
UTL_SMTP.write_data (conn, crlf);
END IF;
END LOOP;
-- Append the final boundary line
mesg := crlf || '--' || boundary || '--' || crlf;
UTL_SMTP.write_data (conn, mesg);
-- Close the SMTP connection
UTL_SMTP.close_data (conn);
UTL_SMTP.quit (conn);
END;
Kamran Agayev A. (10g OCP)
http://kamranagayev.wordpress.com -
How can I email using UTL_SMTP with a csv file as an attachment?
Dear All,
It would be great if someone could help me. I am trying to use UTL_SMTP to email with a csv file as attachment. I do get an email with a message but no attachment arrives with it.
In fact the code used for attaching the csv file gets appended in the message body in the email.
CREATE OR REPLACE PROCEDURE test_mail
AS
SENDER constant VARCHAR2(80) := '[email protected]';
MAILHOST constant VARCHAR2(80) := 'mailhost.xxxx.ac.uk';
mail_conn utl_smtp.connection;
lv_rcpt VARCHAR2(80);
lv_mesg VARCHAR2(9900);
lv_subject VARCHAR2(80) := 'First Test Mail';
lv_brk VARCHAR2(2) := CHR(13)||CHR(10);
BEGIN
mail_conn := utl_smtp.open_connection(mailhost, 25) ;
utl_smtp.helo(mail_conn, MAILHOST) ;
dbms_output.put_line('Sending Email to : ' ||lv_brk||'Suhas Mitra' ) ;
lv_mesg := 'Date: '||TO_CHAR(sysdate,'dd Mon yy hh24:mi:ss')||lv_brk||
'From: <'||SENDER||'>'||lv_brk||
'Subject: '||lv_subject||lv_brk||
'To: '||'[email protected]'||lv_brk||
'MIME-Version: 1.0'||lv_brk||
'Content-type:text/html;charset=iso-8859-1'||lv_brk||
' boundary="-----SECBOUND"'||
''||lv_brk||
'-------SECBOUND'||
'Some Message'
|| lv_brk ||
'-------SECBOUND'||
'Content-Type: text/plain;'|| lv_brk ||
' name="xxxx.csv"'|| lv_brk ||
'Content-Transfer_Encoding: 8bit'|| lv_brk ||
'Content-Disposition: attachment;'|| lv_brk ||
' filename="xxxx.csv"'|| lv_brk ||
lv_brk ||
'CSV,file,attachement'|| lv_brk || -- Content of attachment
lv_brk||
'-------SECBOUND' ;
dbms_output.put_line('lv_mesg : ' || lv_mesg) ;
utl_smtp.mail(mail_conn, SENDER) ;
lv_rcpt := '[email protected]';
utl_smtp.rcpt(mail_conn, lv_rcpt) ;
utl_smtp.data(mail_conn, lv_mesg) ;
utl_smtp.quit(mail_conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
NULL ;
WHEN OTHERS THEN
dbms_output.put_line('Error Code : ' || SQLCODE) ;
dbms_output.put_line('Error Message : ' || SQLERRM) ;
utl_smtp.quit(mail_conn) ;
END;LKBrwn_DBA wrote:
Use UTL_MAIL instead.That package is an utter disappointment - and an excellent example IMO of how not to design an application programming interface. Even the source code is shoddy.. I mean, having to resort to a GOTO statement....!!?? The person(s) who wrote that package are sorely lacking in even the most basic of programming skills if structured programming is ignored and a spaghetti command used instead.
No wonder the public interface of that code is equally shabby and thoughtless... The mail demo code posted by Oracle was better written than this "+package+" they now have bundled as the official Mail API.
I dunno.. if I was in product management there would have been hell to pay over pushing cr@p like that to customers. -
How to format and send html email using utl_smtp
Hi,
I need to send email in the html format from within the database using utl_smtp. I am really not interested in creating os level file using "set markup html on spool on" and sending that as an html attachment. Can someone please give a code?
1. Output of a select needs to be emailed (for example : select empid, name from emp, salary)
2. Certian cells for example salary < 4800 needs to be higlighted in Red color
3. I need to send email which is having more than 32767 characters (I can not use varchar2 field)
I have reffered to following url on asktom:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1739411218448
However it does not help.
If someone has a sample code with any select from a table, it would be a great help.
Regards
Sudhanshu BhandariThere have been many very good threads on sending email using the UTL_SMTP on the forum including threads that deal with HTML formated mail.
The basic process is to the same as with sending regular (non HTML) mail, but you add some additional headers to the mail message as well as additional formatting to the message body (e.g. HTML tags as needed).
Specifically you need to add the two following headers:
MIME-Version: 1.0
Content-type: text/html
Maybe you are looking for
-
My Mac Pro w/ NVIDIA GeForce 8800 GT does not detect a second display from the System Preferences / Displays. Even though I select the "Detect Displays" button, nothing happens. I also have Bootcamp and Windows 7 in a partition on my drive. When I
-
Mail Read Receipt Issue I Don't See Addressed
I have a company e-mail account that I use for work along with other accounts that are all in my Mail client. I use my MacBook Pro and my iPhone to send and receive mail. Everything works fine. My employer has recently asked that I have a return rece
-
Where did all my custom EQ settings go?
Yea, where?
-
Batch saving layers as individual jpegs
I have searched the forums and haven't quite found what I was looking for. I have assembled a wholel lot of scanned pages from my sketch pad into one psd file. That file has guides which I use to register and align every page (each page is therefor
-
Have new computer, Win 7, and the blocking cookies is not working on Firefox
I have been using XP as the OS for many, many years and have been using Firefox also with never any problems with blocking cookies using that feature. As of today, when I received the new computer, this is not functional. I have attempted to block go