Sage X3 How to send an e-mail

Sometimes X3 developers needs to send e-mail messages at the end of a batch procedure or from a custom form.
To facilitate these tasks I’ve written a simple Subprog that can be use where you need.

That’s the Subprog

For Sage X3 version 1.4.x

Subprog SEND_MAIL(PTO,PCC,PSUBJECT,PTEXT,PNUMLIN,PJOIFIC)
Value Char PSUBJECT
Value Char PTEXT
Value Integer PNUMLIN
Value Char PJOIFIC

  Local Integer XTOTDIM : XTOTDIM = 3+PNUMLIN
  Local Integer XCONTA

  If PCC <> "" Then
    XTOTDIM += 1
  Endif

  Local Char ENVOI(250)(1..XTOTDIM)
  Local Integer XN : XN = 0

  XN += 1
  ENVOI(XN) = "__TO="+chr$(1)+PTO
  If PCC <> "" Then
    XN += 1
    ENVOI(XN) = "__CC="+chr$(1)+PCC
  Endif
  XN += 1
  ENVOI(XN) = "__SUJET="+chr$(1)+PSUBJECT

  For XCONTA = 0 To PNUMLIN - 1
    ENVOI(XCONTA+XN+1) = "__NOTE="+chr$(XCONTA+1)+PTEXT(XCONTA)
    If XCONTA < PNUMLIN - 1 Then
      ENVOI(XCONTA+XN+1)+=chr$(10)
    Endif
  Next

  XN = XN + PNUMLIN

  If PJOIFIC <> "" Then
    XN += 1
    ENVOI(XN) = "__JOINT="+chr$(1)+PJOIFIC
  Endif

  Call SEND_MESS(2,ENVOI(1..XN)) From SUBAMSC

End


 

For Sage X3 version 5 and 6

Subprog SEND_EMAIL(PTO,PCC,PSUBJECT,PBODY,PJOINFIC)
Value Char PTO()(0..)
Value Char PCC()(0..)
Value Char PSUBJECT
Value Clbfile PBODY
Value Char PJOINFIC()

  #YTYPMES: 1 = use the global parameter GTYPMES, 2 = sent by server, 3 = sent by client
  Local Integer YTYPMES : YTYPMES = 1
  Local Integer YREQREC : YREQREC = 2
  Local Integer YREQIMP : YREQIMP = 2
  Local Char YUSER : YUSER = GUSER
  Local Char YMAIL : YMAIL = ""
  Local Char YOBJJOI : YOBJJOI = ""
  Local Char YCLEOBJ : YCLEOBJ = ""
  Local Char YCHRONO : YCHRONO = ""

  # 1 = Write trace
  Local Integer YERD : YERD = 0
  Local Char YUSR_EMAIL(250)(0..999)
  Local Integer YUSR_ENVOI(0..999)
  Local Integer YUSR_SUIVI(0..999)
  Local Integer YUSR_NB
  Local Clbfile YTEXCLB : YTEXCLB = PBODY
  Local Char YTEXOBJ(250) : YTEXOBJ = PSUBJECT
  Local Char YPCEJOI(250)(0..99)
  Local Integer YNBJOI
  Local Char YTYPJOI(250) : YTYPJOI(0) = ""
  Local Integer YCATJOI : YCATJOI = 0
  Local Char YCONTXT(250) : YCONTXT(0) = ""

  #
  #Recipients
  #

  YUSR_NB = 0

  Local Integer I
  For I = 0 To dim(PTO) - 1
    If vireblc(PTO(I),2) <> "" Then
      YUSR_EMAIL(YUSR_NB) = vireblc(PTO(I),2)
      YUSR_ENVOI(YUSR_NB) = 2
      #YUSR_SUIVI(YUSR_NB) = 0
      YUSR_NB += 1
    Endif
  Next

  For I = 0 To dim(PCC) - 1
    If vireblc(PCC(I),2) <> "" Then
      YUSR_EMAIL(YUSR_NB) = vireblc(PCC(I),2)
      YUSR_ENVOI(YUSR_NB) = 3
      #YUSR_SUIVI(YUSR_NB) = 2
      YUSR_NB += 1
    Endif
  Next

  #
  #Attachments !! It works only with 1 attachement !!
  #
  #YNBJOI = 0
  #For I = 0 To dim(PJOINFIC) - 1
  # If vireblc(PJOINFIC(I),2) <> "" Then
  # YPCEJOI(YNBJOI) = vireblc(PJOINFIC(I),2)
  # YNBJOI += 1
  # Endif
  #Next

  YNBJOI = 1
  YPCEJOI(0) = vireblc(PJOINFIC(0),2)

  Call ENVOI_MAIL(YTYPMES,YREQREC,YREQIMP,YUSER,YMAIL,YOBJJOI,YCLEOBJ
& 		,YCHRONO,YERD,YUSR_EMAIL,YUSR_ENVOI,YUSR_SUIVI,YUSR_NB
& 		,YTEXCLB,YTEXOBJ
& 		,YPCEJOI,YNBJOI,YTYPJOI,YCATJOI,YCONTXT) From AWRKMEL

End

Here an example of how to call the Subprog

Local Char XMSG(250)(8)
Local Char XSUJET(250)
Local Integer XXCONTA : XXCONTA = 0

XXCONTA += 1: XMSG(XXCONTA) = "Test email body, row1" + num$(XXCONTA)
XXCONTA += 1: XMSG(XXCONTA) = "row 2 " + num$(XXCONTA)
XSUJET = "E-mail subject"

Call SEND_MAIL("xxx.xxx@xxxxx.xxx","",XSUJET,XMSG,dim(XMSG),"")
#For V5+ version use:
#Call SEND_MAIL("xxx.xxx@xxxxx.xxx","",XSUJET,XMSG,"")

 

Here how to send the current trace

Local Char XMSG(250)(6)
Local Char XSUJET(250)
Local Integer XXCONTA : XXCONTA = 0

XXCONTA += 1: XMSG(XXCONTA) = "Test email body, row1" + num$(XXCONTA)
XXCONTA += 1: XMSG(XXCONTA) = "row 2 " + num$(XXCONTA)
XSUJET = "E-mail subject"
If GTRACE <> "" Then
  XFTRACE = func F_FIC_TRACE(GTRACE)
  XMSG(6) = "----------"
  XMSG(7) = XFTRACE
Endif

Call SEND_MAIL("xxx.xxxxx@xxxxx.xx","xxxx_cc@xxxxx.xx",XSUJET,XMSG,dim(XMSG),XFTRACE)

#For V5+ version use:
#Call SEND_MAIL("xxx.xxxxx@xxxxx.xx","xxxx_cc@xxxxx.xx",XSUJET,XMSG,XFTRACE)

The F_FIC_TRACE it’s a function tha return the full path of the trace

Funprog F_FIC_TRACE(XGTRACE)
Value Char XGTRACE

  Local Char WNAME(250)
  If left$(XGTRACE,2)="#@" & (GBROWS | GSERVEUR)
    WNAME = ""
  Elsif max(instr(1,XGTRACE,"/"),instr(1,XGTRACE,"\"),instr(1,XGTRACE,"@"))
    WNAME = XGTRACE
  Else
    WNAME = filpath("TRA",XGTRACE,"tra")
  Endif
  If filinfo(WNAME,0)=0
    WNAME = ""
  Endif

End WNAME

 

8 Replies to “Sage X3 How to send an e-mail”

  1. Hi,

    Your code is really helpful. Except while compiling it, I am getting the following error message:

    SEND_MESS: Label in existent.

    It is actually referring to the line 42 of your SEND_MAIL subprogram above. Because I am unable to access the SUBAMSC process since I guess its reserved by X3.

    Could you please help?

    Thank you in advance for your response.

    1. Hi,

      you are right it works only with 14x version.

      Let me the time to investigate how to doing the same in newer versions

      Matteo

  2. Hello,

    I have implemented the mail generation through the workflow functionality on X3 V6.
    The parameters are defined in the workflow rules.

    Regards,
    Joshita

Leave a Reply

Your email address will not be published. Required fields are marked *