2
0

SMTPSRCMBR 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. * Curl SMTP send source member as attachment
  2. *
  3. h DFTACTGRP(*NO) ACTGRP(*NEW)
  4. h OPTION(*NOSHOWCPY)
  5. h BNDDIR('CURL')
  6. *
  7. **************************************************************************
  8. * _ _ ____ _
  9. * Project ___| | | | _ \| |
  10. * / __| | | | |_) | |
  11. * | (__| |_| | _ <| |___
  12. * \___|\___/|_| \_\_____|
  13. *
  14. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  15. *
  16. * This software is licensed as described in the file COPYING, which
  17. * you should have received as part of this distribution. The terms
  18. * are also available at https://curl.se/docs/copyright.html.
  19. *
  20. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  21. * copies of the Software, and permit persons to whom the Software is
  22. * furnished to do so, under the terms of the COPYING file.
  23. *
  24. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
  25. * ANY KIND, either express or implied.
  26. *
  27. * SPDX-License-Identifier: curl
  28. *
  29. **************************************************************************
  30. *
  31. /include H,CURL.INC
  32. *
  33. * Example to SMTP send source member as attachment via SMTP.
  34. *
  35. fRPGXAMPLESif e disk extmbr(program_name)
  36. f rename(RPGXAMPLES: record)
  37. d pi
  38. d url 60 SMTP server URL
  39. d recipient_mail 40 Recipient mail addr
  40. *
  41. d program_name c 'SMTPSRCMBR' Member name to send
  42. d sender_name c 'Curl' Sender name
  43. d sender_mail c 'curl@example.com' Sender e-mail
  44. d recipient_name c 'WIMC' Recipient name
  45. d crlf c X'0D25'
  46. *
  47. d urllen s 10u 0 URL length
  48. d rcptmlen s 10u 0 Recipient mail len
  49. *
  50. **************************************************************************
  51. urllen = trimmed_length(url: %len(url));
  52. rcptmlen = trimmed_length(recipient_mail: %len(recipient_mail));
  53. // Do the curl stuff.
  54. curl_global_init(CURL_GLOBAL_ALL);
  55. main();
  56. curl_global_cleanup();
  57. *inlr = *on; // Exit
  58. *
  59. **************************************************************************
  60. * Main procedure: do the curl job.
  61. **************************************************************************
  62. *
  63. p main b
  64. d main pi
  65. *
  66. d h s * Easy handle
  67. d result s like(CURLcode) Curl return code
  68. d inz(CURLE_OUT_OF_MEMORY)
  69. d errmsgp s * Error string pointer
  70. d response s 52 For error display
  71. d headers s * inz(*NULL) Mail headers
  72. d rcpts s * inz(*NULL) List of recipients
  73. d mime s * Mail MIME structure
  74. d mimepart s * Mail part
  75. // Create and fill curl handle.
  76. h = curl_easy_init();
  77. if h <> *NULL;
  78. rcpts = curl_slist_append_ccsid(rcpts:
  79. %subst(recipient_mail: 1: rcptmlen): 0);
  80. headers = curl_slist_append_ccsid(headers: 'From: ' + sender_name +
  81. ' <' + sender_mail + '>':
  82. 0);
  83. headers = curl_slist_append_ccsid(headers: 'To: ' + recipient_name +
  84. ' <' + %subst(recipient_mail: 1: rcptmlen) + '>': 0);
  85. headers = curl_slist_append_ccsid(headers: 'Subject: An ILE/RPG ' +
  86. 'source program': 0);
  87. headers = curl_slist_append_ccsid(headers: 'Date: ' + mail_date():
  88. 0);
  89. curl_easy_setopt_ccsid(h: CURLOPT_URL: %subst(url: 1: urllen): 0);
  90. curl_easy_setopt_ccsid(h: CURLOPT_MAIL_FROM: sender_mail: 0);
  91. curl_easy_setopt(h: CURLOPT_MAIL_RCPT: rcpts);
  92. curl_easy_setopt(h: CURLOPT_HTTPHEADER: headers);
  93. mime = curl_mime_init(h);
  94. mimepart = curl_mime_addpart(mime);
  95. curl_mime_data_ccsid(mimepart: 'Please find the ILE/RPG program ' +
  96. program_name + ' source code in ' +
  97. 'attachment.' + crlf:
  98. CURL_ZERO_TERMINATED: 0);
  99. mimepart = curl_mime_addpart(mime);
  100. curl_mime_data_cb(mimepart: -1: %paddr(out_data_cb): *NULL: *NULL:
  101. *NULL);
  102. curl_mime_filename_ccsid(mimepart: program_name: 0);
  103. curl_mime_encoder_ccsid(mimepart: 'quoted-printable': 0);
  104. curl_easy_setopt(h: CURLOPT_MIMEPOST: mime);
  105. // Perform the request.
  106. setll *start RPGXAMPLES;
  107. result = curl_easy_perform(h);
  108. // Cleanup.
  109. curl_mime_free(mime);
  110. curl_slist_free_all(headers);
  111. curl_slist_free_all(rcpts);
  112. curl_easy_cleanup(h); // Release handle
  113. endif;
  114. // Check for error and report if some.
  115. if result <> CURLE_OK;
  116. errmsgp = curl_easy_strerror_ccsid(result: 0);
  117. response = %str(errmsgp);
  118. dsply '' '*EXT' response;
  119. else;
  120. response = 'Mail sent';
  121. dsply '' '*EXT' response;
  122. endif;
  123. p main e
  124. *
  125. **************************************************************************
  126. * Attachment data callback procedure.
  127. **************************************************************************
  128. *
  129. p out_data_cb b
  130. d out_data_cb pi 10u 0
  131. d ptr * value Output data pointer
  132. d size 10u 0 value Data element size
  133. d nmemb 10u 0 value Data element count
  134. d userdata * value User data pointer
  135. *
  136. d buffer s 9999999 based(ptr) Output buffer
  137. d line s 9999999 based(lineptr) ASCII line pointer
  138. d linelen s 10u 0
  139. d i s 10u 0 Buffer position
  140. *
  141. size = size * nmemb; // The size in bytes.
  142. i = 0;
  143. dow size - i >= %len(SRCDTA) + %len(crlf) and not %eof(RPGXAMPLES);
  144. read record;
  145. lineptr = curl_from_ccsid(%trimr(SRCDTA) + crlf: 0);
  146. linelen = %scan(X'00': line) - 1;
  147. %subst(buffer: i + 1: linelen) = %str(lineptr);
  148. curl_free(lineptr);
  149. i = i + linelen;
  150. enddo;
  151. return i;
  152. p out_data_cb e
  153. *
  154. **************************************************************************
  155. * Mail-formatted date procedure.
  156. **************************************************************************
  157. *
  158. p mail_date b
  159. d mail_date pi 50 varying
  160. *
  161. d sysval ds qualified To retrieve timezone
  162. d numsysval 10u 0
  163. d offset 10u 0
  164. d 100
  165. *
  166. d get_sysval pr extpgm('QWCRSVAL')
  167. d outdata likeds(sysval)
  168. d outsize 10u 0 const
  169. d numsysval 10u 0 const
  170. d name 10 const
  171. d errcode 10000 options(*varsize)
  172. *
  173. d now ds qualified
  174. d ts z
  175. d year 4s 0 overlay(ts: 1)
  176. d month 2s 0 overlay(ts: 6)
  177. d day 2s 0 overlay(ts: 9)
  178. d hour 2s 0 overlay(ts: 12)
  179. d minute 2 overlay(ts: 15)
  180. d second 2 overlay(ts: 18)
  181. *
  182. d sysvalinfo ds qualified based(sysvalinfoptr)
  183. d name 10
  184. d type 1
  185. d status 1
  186. d length 10u 0
  187. d value 99999
  188. *
  189. d qusec ds qualified
  190. d 10u 0 inz(0)
  191. *
  192. d weekday s 10u 0
  193. *
  194. now.ts = %timestamp(*SYS);
  195. get_sysval(sysval: %len(sysval): 1: 'QUTCOFFSET': qusec);
  196. sysvalinfoptr = %addr(sysval) + sysval.offset;
  197. weekday = %rem(%diff(now.ts: %timestamp('2001-01-01-00.00.00.000000'):
  198. *DAYS): 7);
  199. return %subst('MonTueWedThuFriSatSun': 3 * weekday + 1: 3) + ', ' +
  200. %char(now.day) + ' ' +
  201. %subst('JanFebMarAprMayJunJulAugSepOctNovDec':
  202. 3 * now.month - 2: 3) + ' ' +
  203. %char(now.year) + ' ' +
  204. %char(now.hour) + ':' + now.minute + ':' + now.second + ' ' +
  205. %subst(sysvalinfo.value: 1: sysvalinfo.length);
  206. p mail_date e
  207. *
  208. **************************************************************************
  209. * Get the length of right-trimmed string
  210. **************************************************************************
  211. *
  212. p trimmed_length b
  213. d trimmed_length pi 10u 0
  214. d string 999999 const options(*varsize)
  215. d length 10u 0 value
  216. *
  217. d addrdiff s 10i 0
  218. d len s 10u 0
  219. *
  220. len = %scan(X'00': string: 1: length); // Limit to zero-terminated string
  221. if len = 0;
  222. len = length + 1;
  223. endif;
  224. if len <= 1;
  225. return 0;
  226. endif;
  227. return %checkr(' ': string: len - 1); // Trim right
  228. p trimmed_length e