home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 February / PCWorld_2003-02_cd.bin / Komunik / sambar / sambar53b3.exe / samples / source / sendmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-09  |  5.5 KB  |  233 lines

  1. /*
  2. ** SENDMAIL
  3. **
  4. **      HTTP Wrapper for the Sendmail Utilities.
  5. **
  6. **        Confidential Property of Tod Sambar
  7. **        (c) Copyright Tod Sambar 1997
  8. **        All rights reserved.
  9. **
  10. **
  11. ** Public Functions:
  12. **
  13. **        sendmail_init
  14. **
  15. **
  16. ** History:
  17. ** Chg#    Date    Description                                                Resp
  18. ** ----    -------    -------------------------------------------------------    ----
  19. **         6APR97    Created                                                    sambar
  20. */
  21.  
  22. #include    <stdio.h>
  23. #include    <memory.h>
  24. #include    <string.h>
  25. #include    <stdlib.h>
  26. #include    <sendmail.h>
  27.  
  28. /*
  29. ** Sendmail parameters.
  30. */
  31. static SA_RPCPARAM        sendmailp [] =
  32. {
  33.     { "recipient",        1,    "Mail recipient." },
  34.     { "message",        1,    "Body of the mail." },
  35.     { "messagemime",    0,    "Mime type of message (text/plain by default)" },
  36.     { "subject",        1,    "Mail subject." },
  37.     { "email",            1,    "Mail address of the sender." },
  38.     { "attachment",        0,    "File to send as an attachment; must start with a slash (/) and be relative to the document directory." },
  39.     { "attachmime",        0,    "Mime type of attachment (text/plain by default)" },
  40.     { "username",        0,    "Optional username for SMTP authentication." },
  41.     { "password",        0,    "Optional password for SMTP authentication." }
  42. };
  43.  
  44. /*
  45. **  SENDMAIL_INIT
  46. **
  47. **    Initialize the Sendmail RPC utilities.
  48. **
  49. **  Parameters:
  50. **    sactx        Sambar Server context
  51. **
  52. **  Returns:
  53. **    SA_SUCCEED | SA_FAIL
  54. */
  55. SA_RETCODE SA_PUBLIC
  56. sendmail_init(sactx)
  57. SA_CTX        *sactx;
  58. {
  59.     /*
  60.     ** Register the Sendmail form RPC                                 
  61.     ** Note: This can only be called from RCXsendmail, register
  62.     **         sendmail_init2 to allow sendmail to be called via 
  63.     **         /session/sendmail
  64.     */
  65.     if (sa_cmd_init(sactx, "sendmail", sendmailp, 
  66.         sizeof(sendmailp) / sizeof(SA_RPCPARAM), SA_AUTHORIZATION_SCRIPT,
  67.         "Sendmail Form Utility", sendmail_form) != SA_SUCCEED)
  68.     {
  69.         sa_log2(sactx, SA_LOG_ERROR, "Unable to initialize Sendmail Form RPC");
  70.         return (SA_FAIL);
  71.     } 
  72.  
  73.     sa_log2(sactx, SA_LOG_TRACE, "Sendmail Utilities Initialized");
  74.  
  75.     return (SA_SUCCEED);
  76. }
  77.  
  78. /*
  79. **  SENDMAIL_INIT2
  80. **
  81. **    Initialize the Sendmail RPC utilities (open to all use).
  82. **
  83. **    WARNING!  Initialzing this interface allows you sendmail
  84. **    script to be used as a SPAM relay point. 
  85. **
  86. **  Parameters:
  87. **    sactx        Sambar Server context
  88. **
  89. **  Returns:
  90. **    SA_SUCCEED | SA_FAIL
  91. */
  92. SA_RETCODE SA_PUBLIC
  93. sendmail_init2(sactx)
  94. SA_CTX        *sactx;
  95. {
  96.     /* Register the Sendmail form RPC                                 */
  97.     if (sa_cmd_init(sactx, "sendmail", sendmailp, 
  98.         sizeof(sendmailp) / sizeof(SA_RPCPARAM), SA_AUTHORIZATION_ALL,
  99.         "Sendmail Form Utility", sendmail_form) != SA_SUCCEED)
  100.     {
  101.         sa_log2(sactx, SA_LOG_ERROR, "Unable to initialize Sendmail Form RPC");
  102.         return (SA_FAIL);
  103.     } 
  104.  
  105.     sa_log2(sactx, SA_LOG_TRACE, "Sendmail Utilities Initialized");
  106.  
  107.     return (SA_SUCCEED);
  108. }
  109.  
  110. /*
  111. **  SENDMAIL_FORM
  112. **
  113. **    Process a sendmail form and send the appropriate mail.
  114. **
  115. **  Parameters:
  116. **    sactx        Sambar Server context
  117. **    saconn        Sambar Server connection
  118. **    saparams    RPC Parameters
  119. **    infop        Error parameters
  120. **
  121. **  Returns:
  122. **    SA_SUCCEED | SA_FAIL
  123. */
  124. SA_RETCODE SA_PUBLIC
  125. sendmail_form(sactx, saconn, saparams, infop)
  126. SA_CTX        *sactx;
  127. SA_CONN        *saconn;
  128. SA_PARAMS    *saparams;
  129. SA_INT        *infop;
  130. {
  131.     SA_INT        emaillen;
  132.     SA_CHAR        *email;
  133.     SA_INT        tolen;
  134.     SA_CHAR        *to;
  135.     SA_INT        subjlen;
  136.     SA_CHAR        *subj;
  137.     SA_INT        msglen;
  138.     SA_CHAR        *msg;
  139.     SA_INT        msgmimelen;
  140.     SA_CHAR        *msgmime;
  141.     SA_INT        attachlen;
  142.     SA_CHAR        *attach;
  143.     SA_INT        attachmimelen;
  144.     SA_CHAR        *attachmime;
  145.     SA_CHAR        *username;
  146.     SA_CHAR        *password;
  147.  
  148.     /* Get the mail recipient                                            */
  149.     if ((sa_param(sactx, saparams, "recipient", &to, &tolen) != SA_SUCCEED) 
  150.         || (tolen == 0) || (tolen > 2048))
  151.     {
  152.         *infop = SA_E_INVALIDDATA;
  153.         return (SA_FAIL);
  154.     }
  155.  
  156.     /* Get the mail message                                             */
  157.     if (sa_param(sactx, saparams, "message", &msg, &msglen) != SA_SUCCEED)
  158.     {
  159.         *infop = SA_E_INVALIDDATA;
  160.         return (SA_FAIL);
  161.     }
  162.  
  163.     /* If there is no message, just return                                */
  164.     if (msglen == 0)
  165.         return (SA_SUCCEED);
  166.  
  167.     /* Get the message mime type (optional)                                */
  168.     if (sa_param(sactx, saparams, "messagemime", &msgmime, &msgmimelen) 
  169.         != SA_SUCCEED)
  170.     {
  171.         msgmime = (SA_CHAR *)NULL;
  172.     }
  173.  
  174.     if (msgmimelen == 0)
  175.         msgmime = (SA_CHAR *)NULL;
  176.  
  177.     /* Get the subject (optional)                                        */
  178.     if ((sa_param(sactx, saparams, "subject", &subj, &subjlen) != SA_SUCCEED)
  179.         || (subjlen > 1024))
  180.     {
  181.         *infop = SA_E_INVALIDDATA;
  182.         return (SA_FAIL);
  183.     }
  184.  
  185.     /* Get the "email"                                                     */
  186.     if ((sa_param(sactx, saparams, "email", &email, &emaillen) != SA_SUCCEED) 
  187.         || (emaillen == 0) || (emaillen > 1024))
  188.     {
  189.         *infop = SA_E_INVALIDDATA;
  190.         return (SA_FAIL);
  191.     }
  192.  
  193.     /* Get the attachment (optional)                                    */
  194.     if (sa_param(sactx, saparams, "attachment", &attach, &attachlen) 
  195.         != SA_SUCCEED)
  196.     {
  197.         attach = (SA_CHAR *)NULL;
  198.     }
  199.  
  200.     if (attachlen == 0)
  201.         attach = (SA_CHAR *)NULL;
  202.  
  203.     /* Get the attachment mime type (optional)                            */
  204.     if (sa_param(sactx, saparams, "attachmime", &attachmime, &attachmimelen) 
  205.         != SA_SUCCEED)
  206.     {
  207.         attachmime = (SA_CHAR *)NULL;
  208.     }
  209.  
  210.     if (attachmimelen == 0)
  211.         attachmime = (SA_CHAR *)NULL;
  212.  
  213.     if (sa_param(sactx, saparams, "username", &username, (SA_INT *)NULL)
  214.         != SA_SUCCEED)
  215.     {
  216.         username = (SA_CHAR *)NULL;
  217.     }
  218.  
  219.     if (sa_param(sactx, saparams, "password", &password, (SA_INT *)NULL)
  220.         != SA_SUCCEED)
  221.     {
  222.         password = (SA_CHAR *)NULL;
  223.     }
  224.  
  225.     if (sa_smtpmail(sactx, saconn, username, password, email, to, 
  226.         NULL, NULL, subj, msg, msgmime, attach, attachmime) != SA_SUCCEED)
  227.     {
  228.         return (SA_FAIL);
  229.     }
  230.  
  231.     return (SA_SUCCEED);
  232. }
  233.