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,PJOIFIC)
Value Char PSUBJECT
Value Char PTEXT
Value Char PJOIFIC

  Local Integer PNUMLIN : PNUMLIN = dim(XMSG)
  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, 6 and 9

Subprog SEND_MAIL(PTO,PCC,PSUBJECT,PTEXT,PJOINFIC)
Value Char PTO
Value Char PCC
Value Char PSUBJECT
Value Char PTEXT
Value Char PJOINFIC

  #YTYPMES: 1 = use the global parameter GTYPMES, 2 = sent by server, 3 = sent by client
  Local Integer YTYPMES : YTYPMES = 1

  #2 = Retrun receipt
  Local Integer YREQREC : YREQREC = 2

  #Importance 1=Low  3=High
  Local Integer YREQIMP : YREQIMP = 2

  Local Char YUSER(20) : YUSER = GUSER
  Local Char YMAIL(250) : YMAIL = ""
  Local Char YOBJJOI(250) : YOBJJOI = ""
  Local Char YCLEOBJ(250) : YCLEOBJ = ""
  Local Char YCHRONO(250) : YCHRONO = ""

  # 0 -> No detailed trace. >0 -> Write detailed trace
  Local Integer YERD
  YERD = val(func AFNC.PARAMDOS(nomap,"TRAMEL"))-1  #Read the dossier's param

  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
  Local Char YTEXOBJ(250) : YTEXOBJ = PSUBJECT
  Local Char YPCEJOI(250)(1..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_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_NB += 1
    Endif
  Next


  #
  # Attachment
  #

  YNBJOI = 0
  For I = 0 To dim(PJOINFIC) - 1
   If vireblc(PJOINFIC(I),2) <> "" Then
     YNBJOI += 1
     YPCEJOI(YNBJOI) = vireblc(PJOINFIC(I),2)
   Endif
  Next

  Setlob YTEXCLB With PTEXT

  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,"")

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,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

 

9 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.

  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

    1. Bonjour,

      j’ai mise a jour le Subprog SEND_MAIL avec un nouvelle version pour X3 version 5,6 et 9. Où j’ai corrigez le bug relatif a le message d’erreur ‘PTO trop de dimensions’

      Matteo

Leave a Reply