Ask Reuben

Sending Mail

How can I send e-mails from a Genero application?

You won’t find many  references to e-mail in the Genero documentation or any e-mail library in the Genero extension packages, that does not mean you cannot send e-mail from a Genero application.

To send e-mail from a Genero application you need to ask yourself how do applications written in other languages send email?  To refine the answer, you should also ask yourself the question, do you want any human intervention in the sending of the e-mail?, and what from address do you want on the e-mail?, the users e-mail address or a role based e-mail address.

A number of the techniques I am going to describe are available in this ex_mail example in our Github repository.  I won’t list code in this article but will point you at this repository where appropriate.

The first technique to note is suitable for when you want to use the users e-mail account and you want the user to check or edit the mail before they press the Send button.  This technique is what you see in a typical web page.  This involves create a link on your form that uses a mailto Uniform Resource Indicator (URI).  When this link is clicked, your default mail application will start with a new e-mail ready to send with some of the fields such as To, CC, Bcc, and the e-mail subject and body populated.  You implement this by

  1. adding a LABEL to your form (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.per#L80)
  2. use Presentation Styles to set the Style Attributes textFormat=”html” and sanitize=”no” for this LABEL. (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4st#L6-L9)
  3. display to this LABEL, a string that contains an a element that has the href attribute set to a valid mailto URI. (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4gl#L58)

The mailto URI value in the href attribute conforms to a standard.  You can populate the To, Cc, Bcc, as well as the subject and body e.g. mailto:%1?subject=%2&cc=%3&bcc=%4&body=%5 and expect your default mail tool to populate the new e-mail ready to send with these values. (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4gl#L140-L142)

A variation on this technique is to use a launchUrl front-call that has as the argument, a mailto URI.  This technique allows your 4gl code to create the mail ready to send rather than expecting the user to click a link on the form.  This means you can determine the values for To, Cc, Bcc, and the email subject and body when the user triggers the action, rather than having to pre-populate the LABEL on the form with the desired values.  When the front-call is executed it will launch the users default mail tool populating an e-mail ready to send. (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4gl#L60)

The above techniques will send an e-mail out of the users e-mail account.  They also require the user to press Send, so there is never a guarantee that an e-mail was sent.  The mailto URI also does not cater for attachments, the user has to manually attach them before they press Send.  If you want to send out of a different, perhaps a role based e-mail address e.g. accounts@yourcompany.com, OR if you want to send the e-mail without human confirmation, OR if you want to record fact an e-mail was sent OR if you want to add attachments to the e-mail e.g. a PDF then you have to use different techniques.  There techniques occur on the server side whereas the previous techniques were sent from the front-end.

Your application server may have a mail application.  Type man mail and you may see that it is possible from the command line on your server to send an e-mail.  If it is possible from your command line, then it is also possible to use that command in a RUN statement.  So the code can be simple  (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4gl#L65-L66),  what will vary is the appropriate command (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4gl#L42).

If you have a dedicated e-mail server, it likely has an SMTP interface.   The Simple Mail Transfer Protocol (SMTP) is an Internet standard communication protocol for electronic mail transmission.   Using the base.Channel class and the openClientSocket method you can write code to communicate with this e-mail server using the SMTP protocol.  (https://github.com/FourjsGenero/ex_mail/blob/master/ex_mail.4gl#L69-L90)   The ex_mail example uses a SMTP server available at smtp2go which at time of writing is free for less than 1000 e-mails a month.

Java developers also have the ability to send e-mail.  Using IMPORT JAVA you have access to the same Java libraries that Java developers do.  w3schools has a good Java Mail Tutorial around the JavaMail API.  If there is demand I’ll flesh out the ex_mail example one day to use the JavaMail API.

In summary, if you want to send e-mail out of your Genero application you have the same techniques available to developers in other development languages.  That can be through the mailto URI on the front-end or via protocols such as SMTP on the application server.