Chapter 3 DynaScript Predefined Objects


attachment object

Object

Represents file, data, or document attachments to incoming mail pieces.

Syntax

attachment.propertyName

Description

Use the attachment object to manipulate incoming mail piece attachments. You can view and save attachments in different formats.

Example

This script saves a mail piece file attachment to a hard drive:

<!--SCRIPT 
   mlist = new MailList ("mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com");
   mlistitem = mlist[12];
   mlistitem.Retrieve();
   document.WriteLn( "subject: " + mlistitem.subject );
   document.WriteLn( "size: " + mlistitem.size );
   document.WriteLn( "body: " + mlistitem.body );
   document.WriteLn( "sender: " + mlistitem.sender );

   for( i in mlistitem.attachments ) {
       attach = mlistitem.attachments[i];
       filesave = attach.SaveAsFile( "g:\\temp\\Dynamo\\" + attach.name );
   }
   if (filesave) {
       document.WriteLn( "The file has been saved." );
   }
   mlist.Disconnect();
-->

See also

"mailList object"

"mailPiece object (incoming)"

"mailPiece object (outgoing)"

"recipient object"

mimeType property

Syntax

attachment.mimeType 

Attributes

This property is read-only.

Description

The MIME type of the attachment, for example "image/gif".

Return

String

Example

This example retrieves a mail piece with a subject heading of "The attachment object" and displays the attachment name and MIME type.

<!--SCRIPT
   mlist = new MailList ("mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com");
   for (i in mlist) {
       inPiece=mlist[i];
       if (inPiece.subject == "The attachment object") {
           inPiece.Retrieve();
           document.WriteLn( "body: " + inPiece.body );
           document.WriteLn( "sender: " + inPiece.sender );
           document.WriteLn( "attachment name: " + inPiece.attachments[0].name);
           document.WriteLn( "attachment Mime type: " + inPiece.attachments[0].mimeType);
       }
   }    
   mlist.Disconnect();
-->

name property

Syntax

attachment.name 

Attributes

This property is read-only.

Description

The name or description of the attachment, if present. This is extracted from MIME headers.

Return

String.

Example

This example uses the name property to save the file with the same name as the attached file.

<!--SCRIPT 
   mlist = new MailList ("mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com");
   mlistitem = mlist[12];
   mlistitem.Retrieve();
   document.WriteLn( "subject: " + mlistitem.subject );
   document.WriteLn( "size: " + mlistitem.size );
   document.WriteLn( "body: " + mlistitem.body );
   document.WriteLn( "sender: " + mlistitem.sender );

   for( i in mlistitem.attachments ) {
       attach = mlistitem.attachments[i];
       filesave = attach.SaveAsFile( "g:\\temp\\Dynamo\\" + attach.name );
   }
   if (filesave) {
       document.WriteLn("The file has been saved.");
   }
   mlist.Disconnect();
-->

GetContents method

Syntax

attachment.GetContents( ) 

Description

Returns the content of the attachment.

Return

String or binary

Example

This example retrieves a mail piece with an attachment and displays the attachment:

<!--SCRIPT 
    mlist = new MailList ("mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com ", "dynamo7@dbsrv.sybase.com");
    mlistitem = mlist[6];
    mlistitem.Retrieve();
      
    attach = mlistitem.attachments[0];
    contents = attach.GetContents();
    if( attach.GetErrorCode() != 0 ) {
        document.WriteLn( "Error: " + attach.GetErrorInfo() );
    } else {
        document.contentType = attach.mimeType;
        document.writeln( contents );
    }
        
    mlist.Disconnect();

-->

See also

"autoCommit property"

"connected property"

GetErrorCode method

Syntax

attachment.GetErrorCode( ) 

Description

Returns a code representing the last error that occurred.

Return

Integer

Example

This example retrieves a mail piece with an attachment and displays the attachment:

<!--SCRIPT 
    mlist = new MailList ("mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com ");
    mlistitem = mlist[6];
    mlistitem.Retrieve();
      
    attach = mlistitem.attachments[0];
    contents = attach.GetContents();
    if( attach.GetErrorCode() != 0 ) {
        document.WriteLn( "Error: " + attach.GetErrorInfo() );
    } else {
        document.contentType = attach.mimeType;
        document.writeln( contents );
    }
        
    mlist.Disconnect();
-->

GetErrorInfo method

Syntax

attachment.GetErrorInfo( ) 

Description

Returns a string containing a meaningful error message.

Return

String

Example

This example retrieves a mail piece with an attachment and displays the attachment:

<!--SCRIPT 
    mlist = new MailList ("mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com ");
    mlistitem = mlist[6];
    mlistitem.Retrieve();
      
    attach = mlistitem.attachments[0];
    contents = attach.GetContents();
    if( attach.GetErrorCode() != 0 ) {
        document.WriteLn( "Error: " + attach.GetErrorInfo() );
    } else {
        document.contentType = attach.mimeType;
        document.writeln( contents );
    }
        
    mlist.Disconnect();
-->

SaveAsDocument method

Syntax

attachment.SaveAsDocument ( document_name [, fail_if_document_exists] )

Description

Saves the content of the attachment as a PowerDynamo Web site document. If fail_if_document_exists is set to true (the default), SaveAsDocument fails if the document already exists. If set to false the document is overwritten.

Return

Boolean

Example

This example saves an attachment as a Dynamo document:

<!--SCRIPT
   mlist = new MailList ( "mail.sybase.com", "elmo", "dynamo", "elmo@mail.sybase.com" );
   mlistitem = mlist[12];
   mlistitem.Retrieve();
   document.WriteLn( "subject: " + mlistitem.subject );
   document.WriteLn( "size: " + mlistitem.size );
   document.WriteLn( "body: " + mlistitem.body );
   document.WriteLn( "sender: " + mlistitem.sender );

   for( i in mlistitem.attachments ) {
      attach = mlistitem.attachments[i];
      filesave = attach.SaveAsDocument( "g:\\temp\\Dynamo\\file.txt" );
   }
   if (filesave) {
      document.WriteLn( "File has been saved" );
   }
   document.writeln( mlistitem.attachments );
   mlist.Disconnect();
-->

SaveAsFile method

Syntax

attachment.SaveAsFile ( file_path [, fail_if_file_exists] )

Description

Saves the content of the attachment to the file system. If fail_if_file_exists is set to true (the default), then SaveAsDocument fails if the document already exists. If set to false, the file is overwritten.

Return

Boolean

Example

This example retrieves a mail piece and saves the attachments as files:

<!--SCRIPT
   mlist = new MailList ( "dbsrv.sybase.com", "dynamo7", "dynamo", "dynamo7@dbsrv.sybase.com" );
   num = mlist.count
   document.WriteLn("There are " + num + " messages in the mail box");
   mlistitem = mlist[12];
   mlistitem.Retrieve();
   document.WriteLn( "subject: " + mlistitem.subject );
   document.WriteLn( "size: " + mlistitem.size );
   document.WriteLn( "body: " + mlistitem.body );
   document.WriteLn( "sender: " + mlistitem.sender );
   for( i in mlistitem.attachments ) {
      attach = mlistitem.attachments[i];
      filesave = attach.SaveAsFile( "g:\\temp\\Dynamo\\" + att.name );
   }
   if (filesave) {
      document.WriteLn( "It should have worked" );
   }
   document.writeln( mlistitem.attachments );
   mlist.Disconnect();
-->

 


Copyright © 2001 Sybase, Inc. All rights reserved.