project BLF > class Mail > method Send

Description

This is the main procedure to Send Mails with.


Parameters


icFrominputcharacterThe sending mail adres
icToinputcharacterPipe separated list of recipients.
icSubjectinputcharacterThe subject of the mail
icBodyinputcharacterThe Body of the mail. Use chr(10) for line feeds.
oiReturnStatusoutputinteger0: ok
-22: SMTP server not connected: check server.ini
-23: None of the available Mail Servers of the recipient domain could be reached.


Internal usage


BLF
method BSODLog.PostSave
method BWorkObject.PostSave
method Mail.SendWithAttachments

QadFinancials
method BBudget.ApiCheckBudget
method BCInvoice.PostSave
method BPosting.PreSaveAutoBalNotifMailSend
method MfgNotification.ProcessMfgNotification


program code (program3/mail.p)

assign oiReturnStatus = -98.

/* ======================================================================= */
/* copy all input parameters to variables:                                 */
/* input parameters are not know outside this method                       */
/* ======================================================================= */

if (icTo   = "":U or icTo   = ?)
then do:
    <M-14 run SetMessage
       (input  #T-15'Unable to send e-mail: no receipient specified.':100(5196)T-15# (icMessage), 
        input  '' (icArguments), 
        input  '' (icFieldName), 
        input  '' (icFieldValue), 
        input  'D':U (icType), 
        input  3 (iiSeverity), 
        input  '' (icRowid), 
        input  'BLF-344':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in Mail>

    assign oiReturnStatus = -1.
    return.
end.

if (icFrom = "":U or icFrom = ?)
then do:
    icFrom = <M-18 GetDefaultSender  () in Mail>.
end.

if (icFrom = "":U or icFrom = ?)
then do:
    <M-16 run SetMessage
          (input  #T-17'Unable to send e-mail: no sender available.':100(12)T-17# (icMessage), 
           input  '' (icArguments), 
           input  '' (icFieldName), 
           input  '' (icFieldValue), 
           input  'D':U (icType), 
           input  3 (iiSeverity), 
           input  '' (icRowid), 
           input  'BLF-345':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in Mail>
                     
    assign oiReturnStatus = -1.
    return.
end.

assign vcFrom    = icFrom
       vcTo      = icTo  
       vcDate    = ENTRY(WEEKDAY(today),"Sun,Mon,Tue,Wed,Thu,Fri,Sat":U) + " ":U +
                   STRING(DAY(today)) + " ":U + 
                   ENTRY(MONTH(today),"Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec":U) + " ":U + 
                   STRING(YEAR(today)) + " ":U +
                   STRING(time,"HH:MM:SS":U) + " ":U +
                   string(timezone(now) * 5 / 3,"+9999")
       vcSubject = (if icSubject = ? then "":U else icSubject)
       vcBody    = (if icBody    = ? then "":U else icBody).

/* ======================================================================= */
/* Sort the To list so that all domains are separated: This class makes a  */
/* Connection per domain to the correct smtp server.                       */
/* ======================================================================= */

do  vicount = 1 to num-entries(icTo,"|":u):
    assign vcRecipient     = entry(vicount,icTo,"|":u)
           vcDomain        = substring(vcRecipient,index(vcRecipient,'@':U) + 1,-1,"CHARACTER":U)
           vcUniquedomains = vcUniquedomains + (if lookup(vcDomain,vcUniquedomains,"|":u) = 0
                                                then "|":u + vcDomain
                                                else "":u).
end.
assign vcUniquedomains = substring(vcUniquedomains,2,-1,"CHARACTER":U).
do viCount3 = 1 to num-entries(vcUniquedomains,"|":u):
    assign vcRecipientsPerDomain = "":u.
    do viCount2 = 1 to num-entries(icTo,"|":u):
        assign vcRecipient = entry(vicount2,icTo,"|":u).
        if substring(vcRecipient,index(vcRecipient,'@':U) + 1,-1,"CHARACTER":U) = entry(vicount3,vcUniquedomains,"|":u)
        then assign vcRecipientsPerDomain = vcRecipientsPerDomain + "|":u + vcRecipient.
    end.
    assign vcRecipientsPerDomain = substring(vcRecipientsPerDomain,2,-1,"CHARACTER":U)
           viRecipients          = 0.

    <M-1 run Connect (input  vcRecipientsPerDomain (icRecipients), 
                  output oiReturnStatus (oiReturnStatus)) in Mail>

    if oiReturnStatus <> 0 then return.

    assign vlSendComplete = no.
    repeat transaction on stop undo, leave on quit undo, leave:
        if vlSendComplete
        or viStep < 0
        then leave.
        assign vlResponded = no.
        wait-for read-response of vhSocket pause 60.
        if vlResponded = no
        then do:
            <M-5 run SetMessage
          (input  #T-7'Mail server is not responding':100(13)t-7# (icMessage), 
           input  '' (icArguments), 
           input  '' (icFieldName), 
           input  '' (icFieldValue), 
           input  'D':U (icType), 
           input  3 (iiSeverity), 
           input  '' (icRowid), 
           input  'BLF-343':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in Mail>
            leave.
        end.
    end.
    if vlSendComplete = no
    then do:
        if viRecipients = 0
        then viRecipients = 1.
        <M-3 run SetMessage
          (input  #T-8'Cannot send e-mail to $1.':100(14)t-8# (icMessage), 
           input  entry(viRecipients,vcRecipientsPerDomain,'|':u) (icArguments), 
           input  '' (icFieldName), 
           input  '' (icFieldValue), 
           input  'D':U (icType), 
           input  3 (iiSeverity), 
           input  '' (icRowid), 
           input  'BLF-342':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in Mail>
        <M-4 run Disconnect () in Mail>
        delete object vhsocket.
        assign oiReturnStatus = -3.
        return.
    end.
end.
delete object vhsocket.

assign oiReturnStatus = 0.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "Mail".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "Send".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/mail.send.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icFrom = <parameter value>
       vhParameter::icTo = <parameter value>
       vhParameter::icSubject = <parameter value>
       vhParameter::icBody = <parameter value>.

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.