home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a108 / 6.ddi / SAMPLES / BULK / BULK.PRG < prev    next >
Encoding:
Text File  |  1993-08-27  |  6.4 KB  |  238 lines

  1. * BULK.PRG
  2. *
  3. * The bulk mailing program illustrates a common use
  4. * for integrating FoxPro and MS MAIL. This sample program
  5. * allows for two types of mailings. The first is a 'General'
  6. * type which sends the same message to a selected group of
  7. * individuals, which in this example is all the names in
  8. * the members database. The second scenario sends messages
  9. * to all the people in the database, but sends them separately
  10. * so that they can be personalized with information unique
  11. * to that individual.
  12. *
  13. * Important: the address aliases used in this example were
  14. * made using a network at Microsoft. You will need to modify
  15. * them in the Members database to make the sample program work.
  16.  
  17. #DEFINE mailtype     'IPM.'
  18. #DEFINE nonmailtype 'IPC.'
  19.  
  20. PRIVATE mptype,bulkbtn,getlib,islogon,subject,oldtalk;
  21.     notetext,getaddress,messtext,tempstr,validid,retval
  22.  
  23.     IF SET("TALK") = "ON"
  24.         SET TALK OFF
  25.         m.oldtalk= "ON"
  26.     ELSE
  27.         m.oldtalk= "OFF"
  28.     ENDIF
  29.  
  30.     STORE 1 TO mptype,bulkbtn
  31.     subject =SPACE(50)
  32.     notetext=""
  33.  
  34.     * Check if certain files exist and set path if needed
  35.     ON ERROR *
  36. *    IF !FILE('MAPILIB.PRG')
  37. *        getlib=LOCFILE('MAPILIB.PRG','PRG','Locate MAPILIB.PRG:')
  38. *        IF !'MAPILIB'$UPPER(getlib)
  39. *            ON ERROR
  40. *            WAIT WINDOW 'User aborted.' TIMEOUT 1
  41. *            SET TALK &oldtalk
  42. *            RETURN
  43. *        ENDIF
  44. *    ENDIF
  45.     IF !FILE('BULK.PRG')
  46.         getlib=LOCFILE('BULK.PRG','PRG','Locate BULK.PRG:')
  47.         IF !'BULK'$UPPER(getlib)
  48.             ON ERROR
  49.             WAIT WINDOW 'User aborted.' TIMEOUT 1
  50.             SET TALK &oldtalk
  51.             RETURN
  52.         ENDIF
  53.     ENDIF
  54.     ON ERROR
  55.  
  56.     * Call screen here to select type of bulk mailing
  57.     DO bulk.spr
  58.     IF m.bulkbtn = 2  &&Cancel pressed
  59.         SET TALK &oldtalk
  60.         RETURN
  61.     ENDIF
  62.  
  63.     * Now do logon, use session if already open
  64.     islogon=.F.
  65.     DO CASE
  66.         CASE TYPE('mailsession')#'N'
  67.             PUBLIC mailsession
  68.             mailsession = 0
  69.             mailsession=mapilib('LOGON')
  70.         CASE m.mailsession=0
  71.             mailsession=mapilib('LOGON')
  72.         OTHERWISE
  73.             islogon=.T.
  74.     ENDCASE
  75.  
  76.     IF m.mailsession=0  &&failed to logon
  77.         RETURN
  78.     ENDIF
  79.  
  80.     * Open databases needed for application
  81.     IF !USED('members')
  82.         USE members IN 0
  83.     ENDIF
  84.     IF !USED('sales')
  85.         USE sales IN 0
  86.     ENDIF
  87.     * Create cursors for needed MAPI calls. These
  88.     * will be filled later with data.
  89.     =mapilib('newcursor','mapiFile')
  90.     =mapilib('newcursor','mapiMesg')
  91.     =mapilib('newcursor','mapiRecip')
  92.  
  93.     * handle bulk mailing type here
  94.     DO CASE
  95.         CASE m.mptype = 1    &&general
  96.             DO genmail
  97.         CASE m.mptype = 2    &&personalized
  98.             DO permail
  99.     ENDCASE
  100.  
  101.     * Clean up things here.
  102.     IF !islogon
  103.         =mapilib('LOGOFF',m.mailsession)
  104.         mailsession = 0
  105.     ENDIF
  106.  
  107.     USE IN members
  108.     USE IN sales
  109.     USE IN mapimesg
  110.     USE IN mapifile
  111.     USE IN mapirecip
  112.     SET TALK &oldtalk
  113.  
  114.  
  115.     *!*********************************************************************
  116.     *!
  117.     *!      FUNCTION: genmail
  118.     *!
  119.     *!*********************************************************************
  120.     * General mailing option.
  121.     * This option makes a single call to the MPSendMail function
  122.     * using a mapiRecips cursor called bulklist containing all
  123.     * of the names receiving the messages.
  124. PROCEDURE genmail
  125.     =mapilib('newcursor','mapiRecip','bulklist')  &&create cursor
  126.     SELECT members
  127.     * Must check and validate each recipient through the
  128.     * MPResolve MAPI function. Valid names are added to
  129.     * bulklist cursor. Invalid names are not added and
  130.     * will not be sent message.
  131.     SCAN
  132.         WAIT WINDOW 'Verifying valid MS MAIL address ... '+;
  133.             ALLT(members.name) NOWAIT
  134.         IF checkid()
  135.             SELECT mapirecip
  136.             SCATTER MEMVAR MEMO
  137.             INSERT INTO bulklist FROM MEMVAR
  138.             SELECT members
  139.         ELSE
  140.             LOOP
  141.         ENDIF
  142.     ENDSCAN
  143.     WAIT CLEAR
  144.     * Now handle the mail sending here. Note that no files
  145.     * are sent here, but could if you chose to add it.
  146.     IF RECCOUNT('bulklist')#0
  147.         INSERT INTO mapimesg VALUES(0,m.subject,m.notetext,mailtype,;
  148.             mapilib('getdate'),'',0,RECCOUNT('bulklist'),0)
  149.         retval=mapilib("sendmail",m.mailsession,"mapiMesg",;
  150.             "bulklist","mapiFile",8)
  151.         IF m.retval
  152.             WAIT WINDOW 'Message sent successfully...' TIMEOUT 1
  153.         ENDIF
  154.     ENDIF
  155.     * Close cursor used to store all recipients
  156.     USE IN bulklist
  157.     RETURN
  158.  
  159.  
  160.     *!*********************************************************************
  161.     *!
  162.     *!      FUNCTION: permail
  163.     *!
  164.     *!*********************************************************************
  165.     * Personalized mailing option
  166.     * This option makes multiple calls to the MPSendMail function
  167.     * for each name in the members database which can resolve.
  168. PROCEDURE permail
  169.     * now create message which is used as basis for
  170.     * each new message.
  171.     INSERT INTO mapimesg VALUES(0,m.subject,m.notetext,mailtype,;
  172.         mapilib('getdate'),'',0,1,0)
  173.     SELECT members
  174.     SCAN
  175.         validid=.T.
  176.         * verify correct address
  177.         WAIT WINDOW 'Verifying valid MS MAIL address ... '+;
  178.             ALLT(members.name) NOWAIT
  179.         IF !checkid()
  180.             LOOP
  181.         ENDIF
  182.         WAIT CLEAR
  183.         * Add information specific to individual from
  184.         * the sales database to the notetext field.
  185.         m.address=members.address
  186.         SELECT sales,YEAR FROM sales ;
  187.             WHERE address = m.address;
  188.             INTO ARRAY temparr
  189.         IF _TALLY=0
  190.             messtext = m.notetext
  191.         ELSE
  192.             tempstr=''
  193.             FOR i=1 TO _TALLY
  194.                 tempstr=tempstr+'   '+ALLT(STR(temparr[i,2]))+': ';
  195.                     +ALLT(STR(temparr[i,1]))+' units'+CHR(13)
  196.             ENDFOR
  197.             messtext = m.notetext+CHR(13)+CHR(13)+tempstr
  198.         ENDIF
  199.         REPLACE mapimesg.notetext WITH m.messtext
  200.         retval=mapilib("sendmail",mailsession,"mapiMesg",;
  201.             "mapiRecip","mapiFile",0)
  202.         IF m.retval
  203.             WAIT WINDOW 'Message sent successfully...' TIMEOUT .5
  204.         ENDIF
  205.         SELECT members
  206.     ENDSCAN
  207.     RETURN
  208.  
  209.  
  210.     *!*********************************************************************
  211.     *!
  212.     *!      FUNCTION: checkid
  213.     *!
  214.     *!*********************************************************************
  215.     * This function is used to validate the mail name/alias
  216.     * entered in MPResolve function. If name cannot be
  217.     * resolved, then a Resolve dialog is brought forward. The
  218.     * user is then prompted to accept name or try again. If the
  219.     * name is resolved without a dialog, no alert is presented.
  220. FUNCTION checkid
  221.     validid=.T.
  222.     DO WHILE .T.
  223.         IF !mapilib('resolve',m.mailsession,ALLT(members.address))
  224.             validid=.F.
  225.             EXIT
  226.         ENDIF
  227.         IF ALLT(members.name) $ mapirecip.name
  228.             EXIT
  229.         ELSE
  230.             IF mapilib('mpalert','The name selected ('+ALLT(mapirecip.name)+;
  231.                     ') does not match the one in the database ('+ALLT(members.name)+;
  232.                     '). Use this name (Yes) or try again (No)?')
  233.                 EXIT
  234.             ENDIF
  235.         ENDIF
  236.     ENDDO
  237.     RETURN validid
  238.