email commands

Using Email Commands

Although you may never have thought of email as a system interfacing tool, this is in fact what it is, because email allows users to send and receive messages from a local machine to recipients on destination hosts, regardless of platform. Even if the email message is only textual, and is only meant to be read by the recipient and not cause any direct system action, the delivery and receipt of the email constitute a system interface.

A standard email message without attachments is simply a text file, made up of header lines that tell an email server how to deliver the message, and of the message content.

SMTP is an acronym for Simple Mail Transfer Protocol and POP3 for Post Office Protocol 3; they are the standard TCP/IP protocols for sending email and receiving email, respectively. CobolScript uses these protocols in its email commands, which enable the sending and receiving of simple email messages.

To use CobolScript to build programs that send email messages, you will need access to an SMTP server. Once you have this, you can use the CobolScript SENDMAIL command to send email. Here’s an example:

COPY `tcpip.cpy`.

1 to_addresses.

5 `<nobody1@ttttt.com>`.

5 `Nobody <nobody2@ttttt.com>`.

5 `nobody3@ttttt.com`.

1 from_address PIC X(n) VALUE `youremail@yourhost.com`.

1 subject PIC X(n) VALUE `mail.cbl test`.

1 message.

5 `This is a test message from mail.cbl.`.

5 FILLER PIC X VALUE LINEFEED.

5 `Sent from me to you.`.

1 smtp_server PIC X(n) VALUE `yoursmptserver.com`.

SENDMAIL USING to_addresses

from_address

subject

message

smtp_server.

DISPLAY `TCPIP-RETURN-CODES: ` & TCPIP-RETURN-CODES.

Of course, you would substitute your addresses and message for the above addresses and message.

With CobolScript there are two commands for retrieving email messages, GETMAILCOUNT and GETMAIL. The GETMAILCOUNT command connects to your mail server and determines the number of messages in your inbox. The GETMAIL command retrieves a copy of a specific email message and saves its contents to a text file. GETMAIL does not remove the email message from the server. Here is an example of how to use these commands:

MOVE `youremail@yourhost.com` TO email_address.

MOVE `yourpassword` TO email_password.

MOVE 0 TO email_count.

GETMAILCOUNT USING email_address

email_password

email_count

smtp_server.

DISPLAY `Email count: ` & email_count.

DISPLAY `TCPIP-RETURN-CODES: ` & TCPIP-RETURN-CODES.

MOVE `youremail@yourhost.com` TO email_address.

MOVE `yourpassword` TO email_password.

MOVE 1 TO email_number.

MOVE `mymail.txt` TO email_file_name.

GETMAIL USING email_address

email_password

email_number

email_file_name

smtp_server.

DISPLAY`TCPIP-RETURN-CODES: ` & TCPIP-RETURN-CODES.

Ü

When the GETMAIL command retrieves an email message from a server, it appends the message to the specified text file. This means that if you want to retrieve a copy of all of your email messages, you should use GETMAILCOUNT to find out how many messages there are, and then perform a loop that retrieve each message. If you want each message to be in a separate text file, you should use a new text file name each time you call GETMAIL.


Important Note: When you are sending emails it is important to use a valid SMTP server. Generally it works best if your applications send all emails through your SMTP server, and then your SMTP server delivers the email to the user.