Sending a True Email Attachment from OpenVMS Using MAIL and MIME
After spending three days figuring this out, I thought I'd document the solution for the next OpenVMS administrator who needs it.
My goal was simple:
Send an e-mail from a command procedure
Include a short message body
Attach a .LOG file as a real attachment
Use only native OpenVMS tools
Sounds easy. It wasn't.
Environment
OpenVMS V8.4-2L3
TCP/IP Services V5.7-13ECO5F
SYS$SYSTEM:MIME.EXE
First Discovery
This works:
$ MAIL/SUBJECT="Refresh PROMSYS1 Log" REFRESH_PROMSYS1.LOG smtp%"user@company.com"
However, the contents of the log file become the body of the e-mail.
You do not get an attachment.
MIME Is Required
To send a true attachment, OpenVMS provides the MIME utility:
$ MIME
Interactive Method That Works
Start MIME:
$ MIME
Create a new message:
MIME> NEW BODY.TXT
When the editor opens, enter:
Refresh completed successfully.
Press:
Ctrl/Z
You'll see:
*
Then:
EXIT
Back at the MIME prompt:
MIME> ADD REFRESH_PROMSYS1.LOG
Verify:
MIME> LIST
You should see:
Attachment: 1
Content-Disposition: inline
Attachment: 2
Content-Disposition: attachment
Save:
MIME> SAVE
MIME> EXIT
Send:
$ MAIL/NOSELF BODY.TXT smtp%"user@company.com" /SUBJECT="Refresh PROMSYS1 Log"
Result:
E-mail body contains:
Refresh completed successfully.
REFRESH_PROMSYS1.LOG arrives as a real attachment.
Success.
The Problems Encountered
The MIME documentation suggests:
MIME> NEW/NOEDIT BODY.TXT
Unfortunately, on my system, this did not behave the same way as:
MIME> NEW BODY.TXT
Typical symptoms:
- Body text missing
- Attachment present
- MIME structure looks valid
- Generated e-mail arrives with a blank body
I spent quite a bit of time trying:
NEW/NOEDIT
DCL-created text files:
$ OPEN/WRITE BODY BODY.TXT
$ WRITE BODY "Refresh completed successfully."
$ CLOSE BODY
Command files:
$ MIME < MIME.CMD
Variable substitution:
ADD 'LOGFILE'
Most combinations failed in one way or another.
The Key Discovery
A MIME draft is not the same thing as a text file.
The file created by:
MIME> NEW BODY.TXT
contains MIME structure that is preserved when reopened later using:
OPEN/DRAFT
That turned out to be the key.
One-Time Setup
Create a reusable MIME template.
Start MIME:
$ MIME
Create:
MIME> NEW BODY_BASE.TXT
Enter:
Refresh completed successfully.
Then:
Ctrl/Z
EXIT
Save:
MIME> SAVE
MIME> EXIT
Keep this file permanently.
Working Automated Solution
This is the command procedure I ended up using.
$ OLD_VERIFY = F$VERIFY(1)
$ WRITE SYS$OUTPUT "Sending out notification mail..."
$ DELETE/LOG BODY.TXT;*
$ COPY BODY_BASE.TXT BODY.TXT
$ MIME
OPEN/DRAFT BODY.TXT
ADD REFRESH_PROMSYS1.LOG
SAVE
EXIT
$ MAIL/NOSELF BODY.TXT smtp%"user@company.com" /SUBJECT="Refresh PROMSYS1 Log"
$ WRITE SYS$OUTPUT "Mail sent."
$ DUMMY = F$VERIFY(OLD_VERIFY)
$ EXIT
Result:
- E-mail body contains:
Refresh completed successfully.
- REFRESH_PROMSYS1.LOG arrives as an attachment.
Exactly what I needed.
Lessons Learned
What Works
Create a MIME draft once:
BODY_BASE.TXT
Reuse it:
COPY BODY_BASE.TXT BODY.TXT
OPEN/DRAFT BODY.TXT
ADD logfile
SAVE
Send:
MAIL/NOSELF BODY.TXT
What Doesn't Work Reliably
Creating BODY.TXT directly from DCL:
$ OPEN/WRITE BODY BODY.TXT
$ WRITE BODY "Refresh completed successfully."
$ CLOSE BODY
This creates a text file, but not a MIME draft.
Most Important Lesson
After three days of testing, I came to the following conclusion:
If it works, stop improving it.
Summary
The solution that finally worked was:
BODY_BASE.TXT
|
+--> COPY BODY.TXT
|
+--> OPEN/DRAFT
+--> ADD REFRESH_PROMSYS1.LOG
+--> SAVE
The key was understanding that MIME draft files created by MIME are special and cannot simply be replaced by ordinary text files.
Hopefully this saves the next OpenVMS administrator three days of trial and error. 😊