Friday 26 July 2019

Email from ERP LN using eMessage Connector

#define                                EMAIL_ID_SEP  chr$(124)
                #include               <bic_cmf>
                #include               <bic_text>

                table      tttaad121
                table      tttaad200
                table      tttadv111
                table      tttadv330

                domain tsmdm.page      hold.provider                                                                         
function extern                domain tcbool ppmmm.dll0800.send.mail(
                                                                domain ttcmf.prov          i.service,
                                                                domain tcmcs.st80           i.from.addr,
                                                                domain tcmcs.s999m      i.to.addr,
                                                                domain tcmcs.s999m      i.cc.addr,
                                                                domain tcmcs.s999m      i.subject,
                                                                domain tcmcs.s999m      i.email.body,
                                                                domain tcbool                   i.email.body.is.file,
                                                                domain tcmcs.s999m      i.att.path,
                                                                domain tcmcs.s999m      i.attachment,
                                                                domain tcbool                   i.interactive,
                                                ref          domain tcmcs.s999m      o.err.msg,             
                                                        ...)                 
{
                DllUsage
                Expl:      This function can be used to send email.
                Pre:        NA
                Post:      NA
                Input:
                                i.service               -> Service to be used to send the email.
                                                                   eg. "OUTLOOK", "SMTP".
                                i.to.addr               -> Email ID of the TO recipient.
                                                                   If email is to be sent to multiple
                                                                   persons send  the IDs  "|" seperated.
                                i.cc.addr               -> Email ID of the CC recipient.
                                                                   If email is to be sent to multiple
                                                                   persons send  the IDs  "|" seperated.
                                i.subject               -> Subject for the email.
                                i.email.body       -> Contains actual text   OR the file path which
                                                                   contains text to be sent as mail body. File
                                                                   must   be present on the server.
                                i.email.body.is.file
                                                                -> Boolean variable, if
                                                                   true    = varibale i.email.body.is.file      must
                                                                                contain the file path for the text to be
                                                                                sent as  the body for the mail.
                                                                   false = varibale i.email.body.is.file must
                                                                                contain actual text to be sent as the
                                                                                mail body.
                                i.attachment      -> Path of the file which is to      be sent as
                                                                   attachment. If there    are multiple attachments
                                                                   to be sent then sent    the file path pipe
                                                                   seperated.
                                i.interactive        -> If true = a message box opens.
                                                                   false = user      has no interaction.


                Output:
                                o.err.msg            -> Error message if any.
                Returns:
                                true                       -> If successfull.
                                false                      -> If failed.
                EndDllUsage

                                boolean                               flag
                                long                       fp, ret
                                long                       comp
                                long                       stat
                                long                       i.att.format                                   
                                long                       message.ID
                                long                       attachment.ID
                                long                       hold.curr.company
                                string                     sourcefile(1024)
                string          sept(1)                                       
                long            add.att.name                                   
                domain tcmcs.st80           l.frm.addr

                hold.provider = i.service
                if isspace(i.to.addr) and isspace(i.cc.addr) then
                                |* True is returned because there is no need of                sending                any
                       |* message. Not an error.
                                return(true)
                endif

                comp = get.compnr()
        i.att.format = 0
        if get.argc() > 11 then
                i.att.format = get.long.arg(12)
        endif
                                                                               
        sept = ""
        if get.argc() > 12 then
                sept = trim$(get.string.arg(13))
        endif

        add.att.name = 0
        if get.argc() > 13 then
                add.att.name = get.long.arg(14)
        endif

                |* Set the body                for the  email
                if i.email.body.is.file       then
                                sourcefile = i.email.body
                else
                                sourcefile = creat.tmp.file$(bse.tmp.dir$())
                                fp = seq.open(sourcefile, "w")
                                seq.puts(i.email.body, fp)
                                seq.close(fp)
                endif

                flag = compnr.check(0)

                |* Create the message connector            object
                message.ID = cmf.create()
                ret = cmf.setClass(message.ID, "CMF.NOTE.EMAIL")
                ret = cmf.setSubject(message.ID, i.subject)

                |* Set FROM recipient
                if not isspace(i.from.addr) then
                                create.recipient(message.ID, i.from.addr, ttcmf.role.from)
                endif

                |* Set TO recipient
                divide.and.add.recipient.to.xml(message.ID, i.to.addr, ttcmf.role.to)

                |* Set CC recipient
                divide.and.add.recipient.to.xml(message.ID, i.cc.addr, ttcmf.role.cc)

                |* Set email message boby
                attachment.ID =               cmf.createAttachment(message.ID)
                ret = cmf.setAttachmentBody(attachment.ID, ttyeno.yes)

                |* If ttyeno.no  & text/plain,rtf or images/gif then attachment is sent
                |* If ttyeno.yes & text/plain then text   is sent   in body; if images/gif
                |* or text/rtf gives error
                ret = cmf.setAttachmentMIME(attachment.ID, "text/plain")
                ret = cmf.setAttachmentFileName(attachment.ID,sourcefile)

                |* Linking the attachment.         
                link.attachments(message.ID,i.att.path,i.attachment,i.att.format,sept,add.att.name)
                ret = cmf.setPriority(message.ID, ttcmf.prio.high)
                ret = cmf.setSensitivity(message.ID,ttcmf.sens.secret)
                ret = cmf.setNotification(message.ID,ttcmf.noti.nondelivery)


                if message.ID <> 0 then
                                stat = cmf.startService(i.service,2)
                                if stat     <> 0 then
                                                o.err.msg = form.text$("tcwflk0012")
                                                |* Message could not be             sent (Infor ERP  LN eMessage
                                                |* Connector service did not start)
                                                cleanup.and.revert(       comp,
                                                                                                sourcefile,
                                                                                                (not i.email.body.is.file))
                                                return(false)
                                endif
                                if i.interactive = true       then
                                                cmf.setdisplay(message.ID, ttyeno.yes)
                                endif
                                stat = cmf.send(message.ID,i.service)
                                if stat     <> 0 then
                                                o.err.msg = form.text$("tcwflk0013")
                                                |* Message could not be             sent (Infor ERP  LN eMessage
                                                |* Connector service refused message)
                                                cmf.stopService(i.service,2)
                                                cleanup.and.revert(       comp,
                                                                                                sourcefile,
                                                                                                (not i.email.body.is.file))
                                                return(false)
                                endif
                                stat = cmf.stopService(i.service,2)
                endif
                cleanup.and.revert(comp, sourcefile, (not i.email.body.is.file))
                return(true)
}
function domain               tcbool divide.and.add.recipient.to.xml(long   i.message.ID,
                                                                                                 domain                tcmcs.s999m i.addr,
                                                                                                 domain                ttcmf.role  i.role)
{
                boolean               recipient.set
                long       process.var.addr
                domain tcmcs.st80           hold.addr


                recipient.set =   false
                while(not isspace(i.addr))
                                process.var.addr = pos(i.addr, EMAIL_ID_SEP)

                                if process.var.addr > 0   then
                                                hold.addr = i.addr(1; process.var.addr - 1)
                                                i.addr = i.addr(process.var.addr + 1)
                                else
                                                hold.addr = i.addr
                                                i.addr = ""
                                endif

                                if not isspace(hold.addr) then
                                                create.recipient(i.message.ID, hold.addr, i.role)
                                                recipient.set =   true
                                endif
                endwhile

                return(recipient.set)
}

function create.recipient(   long                     i.message.ID,
                                domain      tcmcs.st80           i.recipient.address,
                                domain      ttcmf.role            i.role)
{
                long       recipient.ID
                long       ret

                if isspace(i.recipient.address)     then
                                return
                endif

                recipient.ID = cmf.createRecipient(i.message.ID, i.role)
                ret = cmf.setRecipientName(recipient.ID, i.recipient.address)
                ret = cmf.setRecipientAddress(recipient.ID, i.recipient.address)
                ret = cmf.setRecipientType(recipient.ID, "SMTP")                       
                if i.role = ttcmf.role.to then
                                ret = cmf.setRecipientResponsibility(recipient.ID, "TRUE")
                endif
}

function domain               tcbool link.attachments(long   i.message.ID,
                                                                                domain tcmcs.s999m i.att.path,
                                                                                domain tcmcs.s999m i.att,   
                                        long i.att.format,
                                        string sept(1),
                                        long   add.att.name)
                                                                     
{
                boolean               att.set
                long       att.ID
                long       ret
                long       process.var
                domain tcmcs.s999m      hold.att
        long    slash.pos                                                     

                att.set   = false
                while(not isspace(i.att.path))
                                process.var = pos(i.att.path, EMAIL_ID_SEP)

                                if process.var > 0 then
                                                hold.att = i.att.path(1; process.var - 1)
                                                i.att.path = i.att.path(process.var + 1)
                                else
                                                hold.att = i.att.path &    i.att
                                                i.att.path = ""
                                endif

                                if not isspace(hold.att) then
                                                att.ID = cmf.createAttachment(i.message.ID)
                                                ret = cmf.setAttachmentBody(att.ID, ttyeno.no)
                        on case i.att.format
                        case 0:
                                ret = cmf.setAttachmentMIME(att.ID, "application/pdf")
                                break
                        case 1:
                                ret = cmf.setAttachmentMIME(att.ID, "application/x-msexcel")
                                break
                        endcase
                        if add.att.name = 1 and not isspace(sept) and isspace(i.att) then
                                slash.pos = rpos(hold.att,sept)
                                i.att = hold.att(slash.pos + 1)
                                ret = cmf.setAttachmentFileName(att.ID,            hold.att, i.att)
                                i.att = ""
                        else
                                ret = cmf.setAttachmentFileName(att.ID,            hold.att, i.att)
                        endif
                                                att.set   = true
                                endif
                endwhile

                return(att.set)
}

function cleanup.and.revert(long   i.comp,
                                                    domain tcmcs.s999m i.tmp.file,
                                                    domain tcbool      i.del.file)
{
                compnr.check(i.comp)
                if i.del.file then
                                seq.unlink(i.tmp.file)
                endif
}