3 ways to send a mail from code in Sage X3, with more attachments too

In this post I’ll show you how to send a mail from adonix code in Sage X3.
The points we will face are:

1) Meladx/Send introduction
2) Sending through meladx (the best way for me: at the end with just a single code line you send a mail!)
3) Sending through Workflow
4) Sending through ENVOI_MAIL(…) From AWRKMEL

Sending mail from code in Sage X3: Send vs meladx

Sage X3 has two native ways for sending mails.
The first way is meladx executable file, that you can find in runtime\bin directory.
The seconde one is Send instruction, that was used to send mail through an application of the client station (for more information on “Send GSERMES” go here).

The Meladx executable

Meladx send messages through SMTP/POP3 protocols by means of the mail server specified in the command.
When sending is launched by a workflow or other Sage X3 predisposed systems (for instance via batch),
the server envolved is defined in SERMES variable of supervisor chapter (ADPVAL function).

The Send instruction

The Send instruction send the mail messages with MAPI protocol through the client station that is executing Sage X3.
It’s not usable by web. It serve for sending from client application.
Client station must have a mail client MAPI, and this client must be in execution.
It has 500 byte limit foe message (excluding attachments; meladz limite is 64kb).

Also a Seldest function exist, and it was allowing to make final user insert the recipients and other informations.
In this post we will not be talking about this client call.

Sending a mail through meladx command

The executable meladx is a native command of Sage X3 system; it uses SMTP/POP3 protocols
and in practice it uses a structured file with needed data for sending the mail.

Here an example that you can launch from both command prompt and a SYSTEME call.

meladx -v -s mail.server.com -r from@test.com < C:\file.txt

The file content could be:

From: from@test.com
To: recipient@example.net
Subject: My mail title

Mail message sended by program
Best regards

Once the operation has been understood write a function for writing runtime the file is easy:

Subprog SENDMAIL(NAMEFROM, ADRFROM, ADRTO, ADRCC, SUBJECT, TEXT, SERVER)
# sagedev.it
Value Char NAMEFROM()     #sender name
Value Char ADRFROM()      #sender mail address
Value Char ADRTO()        #recipient mail address (for multiple addresses separate them with  ";")
Value Char ADRCC()        #in copy mail address
Value Char SUBJECT()      #mail title
Value Char TEXT()         #mail message
Value Char SERVER()       #mail server: i.e. mail.dominio.com

  Local Char NAMEFILE(30)
  Local Char PATH(255)
  Local Char EXTENSION(5)
  NAMEFILE = "mail_text_"+num$(adxpid)
  PATH = "YSAGEDEV\TMP"
  EXTENSION="TXT"

  NAMEFROM= vireblc(NAMEFROM,2)
  If NAMEFROM=""
    NAMEFROM=ADRFROM
  Endif

  Openo filpath(PATH,NAMEFILE,EXTENSION),0 Using [XOUT]
  Wrseq "From: "+ NAMEFROM Using [XOUT]
  Wrseq "to: "+ADRTO Using [XOUT]
  Wrseq "cc: "+ADRCC Using [XOUT]
  Wrseq "Subject: "+ SUBJECT  Using [XOUT]
  Wrseq  "" Using [XOUT]
  Wrseq  TEXT Using [XOUT]

  Local Integer XSTAT
  Openo Using [XOUT]
  XMAIL = "meladx"-"-s"-SERVER-"-r"-ADRFROM+" -v <"- filpath(PATH, NAMEFILE, EXTENSION)
  Call SYSTEME(adxmac(0),XMAIL ,"",XSTAT) From ORDSYS
End

Just recall it like this::

Call SENDMAIL("Sales Office", "sales@sagedev.it", "tom@test.com;dick@blabla.it", "test@example.net", "TITLE mail", "Message mail from program", "mail.sagedev.it")

In fact, once this function is inserted in an our library, with a simple call we do all!

meladx and attachments

For sending attachments with meladx, just add similar lines to following to the end of the file:

# Application/others ; name="TEST1.pdf"[TEST1.pdf] "C:\temp\TEST1.pdf" 
# Application/others ; name="TEST2.doc"[TEST2.doc] "C:\temp\TEST2.doc"

Sending through Workflow

These are the steps for implementing a simple example:

  1. create a YMA event in 908 table of Miscellaneous tables (funzione GESADI); we can put “Sending mail by program” as description;
    create always a new event code to avoid interfering with preexisting other uses;
    (note that activity code can not be put in a single item, so this is one of that activities the you have to annotate for checking it in case of Sage X3 updating).

    Miscellaneous tables 908 Event type Workflow (Sage X3 v9))

    Miscellaneous tables 908 Event type Workflow (Sage X3)

     

  2. create a YMAIL workflow (GESAWA function)
    • set event code as YMA
    • Event type: “Miscellaneous”,
    • in the “Management” box of “General” tab check “Trigger Mail field”

      Sage v9 - GESAWA Sending mail Workflow

      Sage v9 – GESAWA Sending mail Workflow

    • in the “Recipient” tab insert a line with “User” as Type and GYRECIPIENT as Recipient (GYRECIPIENT is a global variable that will be defined by program), Send mail “Yes” and the other fields “No”

      Sage v9 - Workflow recipient tab

      Sage v9 – Workflow recipient tab

    • in the “Message” tab
      1. in the object field write GYOBJECT
      2. in the text field write |GYMAILTEXT| (between two vertical bars, to tell the system that this is a variable in the middle of a fixed text)

        Sage v9 Workflow Message Tab sending mail

        Sage v9 Workflow Message Tab sending mail

  3. create the rule

For sending a message mail you just call the following code

Global Char GYRECIPIENT(20),GYOBJTMP(20),GYBODYTMP(50)
GYRECIPIENT = "from@example.net"
GYOBJECT = "TEST MAIL SENDING"
GYMAILTEXT = "Text - example message"
Call WORKFLOW (1,"YMA","",GUSER) From AWRK
Kill    GYRECIPIENT
Kill    GYOBJECT
Kill    GYMAILTEXT

Kill instructions are necessary for deleting the temporary global variables.

Workflow and attachments

Sending mail through workflow allows you to attach files in different ways.

Attached document” field

The first option is inserting in “Attached document” field in the “Message” tab of workflow the absolute path of file to attach.

You can also insert a global variable name valorized opportunely by program.

For inserting more files just separate the paths with a “;” (semicolon); here same examples:

"filename1.txt;filename2.txt"

GFILPATH+";"+GYFILE2

Attaching a file attached to the object

As you can see in the standars windows of Sage x3 (for instance invoices or orders windows),
in the right menu there is an icon that allow the user to attach any files to current object.
The path of these file is assciated with a type and a category.

Attachment icon in object management

Attachment icon in object management

These files attached to the object can also be attached to eventual mail using a workflow with “Object” as Event type.
In the “Message” tab (in workflow configuration) you can choose:

  1. if attach to mail the attached files to the object (“Attachment” flag)
  2. if attach all type of files attached to the object (“All types” flag) or what types to attach
  3. if attach all categories of attachments or what categories (“All categories” flag)
Configuration attachments in a objetc type workflow with mail sending

Configuration attachments in a objetc type workflow with mail sending

These attachments are added to that indicated in the “Attached document” field.

 

Sending mail through ENVOI_MAIL(…) From AWRKMEL

A third way is directly calling the function used by workflows to sending mail.
There is an example in the blog of the expert Matteo Carminati,
to which I refer you and where you will found many useful things: go to Matteo Carminati blog

 

See you next time, and remember to subscribe to the newsletter!

You may also like...

Leave a Reply