Quantcast
Channel: Visual COBOL - Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4356

RE: smtp mail with cURL (was in wrong forum)

$
0
0

Michael Wojcik posted the following reply in the general COBOL forum to this same question:

There's no simple answer, I'm afraid.

The cURL example uses a callback function. You can implement that in COBOL, with a separate PROGRAM or ENTRY in the same process. Then in your callback you could implement something like what the C code does, which is to deliver the next line of header or body text each time the callback is invoked. (The "upload_ctx" stuff is how the C example keeps track of how many lines it's processed. If your program isn't reentrant, you can just do this with global data, i.e. a working-storage item.)

Obviously this isn't a common COBOL idiom, but the COBOL example you're working from has a data-sending callback already - it's everything after "Program-Termination". I haven't looked at the cURL API closely, so I don't know how suitable this callback is when using cURL for SMTP.

The alternative mentioned in the cURL example is using a FILE pointer, which is a C runtime library construct. To do that with COBOL, you could OPEN a line-sequential file, write all of your header and body lines to it, CLOSE it, then call the C library function fopen to get a FILE pointer to it. Something like this:

OPEN temp-file

... *> write header and body

CLOSE temp-file

CALL "fopen" USING

  BY REFERENCE z"/path/to/tempfile"

  BY REFERENCE z"rb"

  RETURNING some-pointer-item

Then you pass some-pointer-item to cURL using CURLOPT_READDATA, and don't worry about a callback. Per the cURL documentation, this does not work on Windows if cURL is a DLL (due to the broken design of the Microsoft C runtime) - you didn't say what platform you're running on. And it's really not much easier than the first option.

There are other difficulties:

• Lines need to be terminated with CRLF, which means adding "& x'0d' & x'0a'" to the end of each string literal you write for a header or body text.

• You need to set To, From, and possibly other headers using the same values you're using for 78TO, etc. The cURL example interpolates those constants directly into the header lines. You can't do the same in your COBOL example, because you've declared 78TO and the others as nul-terminated strings (with z"..."). That makes it more complicated to construct those header lines correctly.

• You need to set headers like Date and Message-ID with values computed at runtime.

There may be other issues; those are just the ones that spring to mind.

Why are you doing SMTP directly? Most platforms have scriptable MUAs (email clients) or MTAs (email servers) that will do most of the work for you.


Viewing all articles
Browse latest Browse all 4356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>