Skip to Main Content
  • Questions
  • Attachment with the e-mail using utl_smtp

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Rajashekar H.S.

Asked: April 16, 2001 - 8:52 am UTC

Last updated: January 04, 2011 - 9:28 am UTC

Version: 8.1.7

Viewed 10K+ times! This question is

You Asked

Hi,

I am using utl_smtp for sending mail in my function now i want to attach the text file with every mail along with the message.

I have following problem i am able to get only message(i.e.body of email) / attachment but not both.

i have forwarded the code please suggest me how do i do that using the utl_smtp.


Regards
Raj
The code is as follows,

FUNCTION deliver (
in_from IN VARCHAR2 DEFAULT NULL,
in_to IN VARCHAR2 DEFAULT NULL,
in_subject IN VARCHAR2 DEFAULT NULL,
in_content IN VARCHAR2 DEFAULT NULL
) RETURN VARCHAR2
IS
mailhost VARCHAR2(30) := 'cob353.dn.net';
-- mailhost VARCHAR2(30) := 'mail.finhost.fi';
mail_conn utl_smtp.connection;
response utl_smtp.reply;
status VARCHAR2(2000):='MAIL ID FORMAT ERROR';

mesg varchar2(4000);
attach1 varchar2(4000);
crlf VARCHAR2( 2 ):= CHR( 13 ) || CHR( 10 );
BEGIN

IF instr(in_to,'@',1,1) > 0 AND instr(in_from,'@',1,1) > 0 THEN
status := 'OK';
mail_conn := utl_smtp.open_connection(mailhost, 25);

response:=utl_smtp.helo(mail_conn, mailhost);
IF response.code<>250 AND response.code<>221 THEN
status:='error';
END IF;

response:=utl_smtp.mail(mail_conn, in_from);
IF response.code<>250 AND response.code<>221 THEN
status:='error';
END IF;

response:=utl_smtp.rcpt(mail_conn,in_to);
IF response.code<>250 AND response.code<>221 AND response.code<>421 THEN
status:='error';
END IF;

if v_filename is null then
response:=utl_smtp.data(mail_conn, 'Subject: '||in_subject||chr(10)||in_content);
IF response.code<>250 AND response.code<>221 THEN
status:='error';
END IF;
else

--filehandle:=UTL_FILE.FOPEN(v_path,v_filename,'r');
attach1:='Client Name|Address|Product|Campaign Name|Publication Date|Insertion Order No|Media Name|Size/Color of Ad|List Price|Discount|Net Price|Fee';
mesg:= 'Subject: '|| in_subject || ': Completed ' ||in_content|| crlf ||
'Your query has been completed. The output can be viewed below. '||crlf||''||crlf||
'MIME-Version: 1.0'||crlf||
'Content-Type: text/plain'||crlf||
'Content-Transfer-Encoding: 7bit'||crlf||
'Content-Disposition: attachment; filename=output.log'||crlf||''|| crlf ||
'Your query results have exc
eeded the maximum output size. The first block is below. The entire output can b
e viewed here.'||
attach1; -- Data to appear in attachment.

response:=utl_smtp.data(mail_conn,mesg);
IF response.code<>250 AND response.code<>221 THEN
status:='error';
END IF;
end if;


response:=utl_smtp.quit(mail_conn);
IF response.code<>250 AND response.code<>221 THEN
status:='error';
END IF;
END IF;

RETURN status;

EXCEPTION
WHEN OTHERS THEN
-- Handle cookie exceptions
IF SQLCODE=-20101 OR SQLCODE=-20102 THEN
com360.err_msg.print(in_address=>'login.main_index',in_errmsg=>'User sessio error, please login ('||SQLCODE||')');
ELSE
com360engine.logs.error_log(in_sqlcode=>SQLCODE,in_errm=>SQLERRM);
END IF;
END;



and Tom said...

In order to use UTL_SMTP, you would have to fully understand the format of the MIME email you must send and encode the entire thing.

I am not a MIME expert -- won't be able to help you out with this one.

I can suggest (this is the way I send attachments) the use of the Java MAIL api instead. Example that allows for sending attachments is at:

</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:255615160805 <code>

it currently supports a BLOB but it is easy enough to change to clob type.



Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

UTL_SMTP

Seetharaman, June 04, 2001 - 3:55 pm UTC

Its really good and clarifies many a doubts within me on this package

but...

Ricardo, October 01, 2001 - 4:37 pm UTC

Where (in wish directory) must be the file to realy be attached??

Thanks.

Text attachments are not on a specified directory

Carlos Gongora, August 01, 2008 - 8:04 pm UTC

Hi All,

I have been breaking my head for long minutes thinking that the attachments should be files that would be called or sent from an specified directory on the server but email does not work that way!!!

What the SMTP server does in order to send an email with a text file attached, it's to get the text from the parameters of the SMTP commands and encode or decode the text on the fly. So when arrives to your machine it's when it gets encoded into a certain type of file.

This is the way it works to send attachments in TXT format. Usin one of the SMTP parameters you pass the text (contents of the attached file) and not the file itself. That's why you don't need to specify a path. You just specify the contents which are encoded or decoded on the fly when receiving or sending the email...

I don't know why is that NOBODY explains this simple but very useful thing... All the sites that I have seen only bother to put the code of the functions needed to send email with attachments using UTL_SMTP but nobody explains anything, the 'know-how' it's kept as a secret.

It's a real shame!!!

Now you know how this works... and how did I found it? By simple deduction... thinking for long minutes... this call learning the hard way...

If you have come into this looking for a way to send email with attachments using UTL_SMTP now you know how this works...



UTL_SMTP

Anju, January 04, 2011 - 9:16 am UTC

Hi,
Am using the procedure html_email for sending the mail in a html format ,this procedure works fine but for some cases when if the data is more (> 32KB) This procedure fails.

can you please advice what are the necessary changes to be done to send send more amount of data .

Thanks in Advance.
Tom Kyte
January 04, 2011 - 9:28 am UTC

I don't understand the logic behind asking a question about html_email which is on this page:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1739411218448

over here? It just doesn't make sense.


change the input types to LOBS for the message bodies. It would be simple enough to accomplish.

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here