Figure 13: Snippets from program IFS3MAIL2 |
. . .
P CreateMsg B
D CreateMsg PI
D NameTo 256A varying const
D AddrTo 256A varying const
D NameFrom 256A varying const
D AddrFrom 256A varying const
D Subject 256A varying const
D Message 32767A varying value
|
D AttFile 256A varying const
|
A |
D MsgFile 256A varying
. . .
/free
//
// Open the attachment. This is done first so that we know
// that the attachment is there before we do any further work.
//
|
att = open(AttFile: O_RDONLY);
if (att = -1);
ReportError();
endif;
|
B |
// When the O_EXCL flag is passed, open() fails
// and sets errno to EEXIST if the file already exists.
//
// Here, we use that to create a unique filename.
dou (fd <> -1);
FileNo += 1;
MsgFile = '/tmp/email_demo_'+ %editc(FileNo:'X') +'.txt';
fd = open( MsgFile
: O_CREAT + O_EXCL + O_WRONLY + O_CCSID
+ O_TEXTDATA + O_TEXT_CREAT
: M_RDWR
: 367
: 0 );
if (fd = -1);
ptrToErrno = get_errno();
if (errno <> EEXIST);
callp close(att);
ReportError();
endif;
endif;
enddo;
|
//
// *** calculate a boundary **
//
boundary = '--=.=' + %char(%timestamp());
|
C |
//
// *** Write e-mail header to text file ***
//
|
hdr = 'To: ' + NameTo + ' <' + AddrTo + '>' + CRLF
+ 'From: ' + NameFrom + ' <' + AddrFrom + '>' + CRLF
+ 'Date: ' + MailDate() + CRLF
+ 'Subject: ' + Subject + CRLF
+ 'MIME-Version: 1.0' + CRLF
+ 'Content-Type: multipart/mixed; boundary="'
+ Boundary + '"' + CRLF
+ CRLF;
callp write(fd: %addr(hdr)+2: %len(hdr));
|
D |
. . .
|
hdr = '--' + Boundary + CRLF
+ 'Content-Type: text/plain' + CRLF
+ CRLF;
callp write(fd: %addr(hdr)+2: %len(hdr));
callp write(fd: %addr(Message)+2: %len(Message));
callp write(fd: %addr(newline): %len(newline));
|
E |
|
//
// The header for an attachment requires some different
// fields.
//
hdr = '--' + Boundary + CRLF
+ 'Content-Type: application/octet-stream; '
+ 'name="' + basename(AttFile) + '"' + CRLF
+ 'Content-Disposition: attachment;'
+ 'filename="' + basename(AttFile) + '"' + CRLF
+ 'Content-Transfer-Encoding: base64' + CRLF
+ CRLF;
callp write(fd: %addr(hdr)+2: %len(hdr));
//
// Read the attachment stream file and base64 encode
// each piece, then write it to the text file
//
len_bin = read(att: %addr(bindata): %size(bindata));
dow (len_bin > 0);
len_text = base64_encode( %addr(bindata)
: len_bin
: %addr(textdata)
: %size(textdata) );
callp write(fd: %addr(textdata): len_text);
callp write(fd: %addr(newline): %size(newline));
len_bin = read(att: %addr(bindata): %size(bindata));
enddo;
hdr = CRLF + '--' + Boundary + '--' + CRLF;
callp write(fd: %addr(hdr)+2: %len(hdr));
|
F |
callp close(fd);
callp close(att);
/end-free
P E
. . .
|