bvstone

MAILTOOL v7 Integrates Easy to use ILE Functions and Configuration Files - Send Email from your IBM i (AS/400)

Posted:

MAILTOOL v7 Integrates Easy to use ILE Functions and Configuration Files - Send Email from your IBM i (AS/400)

The latest update to our MAILTOOL Software includes two major updates to make the software even more functional as useful:

ILE Functions
MAILTOOL now has ILE functions available that make sending emails from ILE Programs simple, compared to the past when building a command and calling QCMDEXC was required.  This means sending an email is as simple as the following few lines of code:

 /COPY QCOPYSRC,P.MAILTOOL                                                   

 /free
  if (mailtool_init() >= 0);   
    mailtool_setValue('from_email':'publisher@booksrus.com'); 
    mailtool_setValue('subject':'test email');
    mailtool_setValue('message':'this is the message nn newline.');  
    mailtool_addTORecipient('james@thegiantpeach.com'); 
    mailtool_addAttachment('/myfiles/pdfs/salesreport.pdf');
    rc = mailtool_sendMail();  

    if (rc < 0);
      //handle error
    endif; 

  endif;                
                        
  *INLR = *ON;          
                        
 /end-free 

See this article if you're needing assistance compiling your program to use the F.MAILTOOL ILE Service Program.

 

Configuration Files
Configuration Files are now used to set the values of most parameters and values of a MAILTOOL call, either from the command (when *DFT or a configuration file is specified) or from the ILE functions (when a configuration file is specified).

When using the MAILTOOL command and specifying *DFT for the configuration file the file that is used will be /bvstools/mailtool/config/userid/defaults.json (where userid is the user's User ID).  If this file doesn't exist, then the supplied default configuration file /bvstools/mailtool/config/defaults.json will be used.

This can be used to set default values such as the mail server to use, the FROM address, subject, as well as a host of other values.  

The configuration files are standard JSON files so a simple text editor is all that is needed to change the values.

When using the MAILTOOL command you can also override all the parameters (except for the TO, CC, BCC, Subject, Message and Attachment parameters) by specifying a configuration file to use on the MAILTOOL command.  

MAILTOOL TOADDR('bvstone@bvstools.com') 
SUBJECT('Testing config file') 
MESSAGE('This is a test of the config file') 
CONFIG('/bvstools/mailtool/config/myconfig.json')

The From address, as well as any other parameters (such as SENDWITH, MAILRTR, USERTR, SSL, DEBUG, etc) will use the values in the Configuration file specified. 

If a configuration file is used with the MAILTOOL command, the parameters on the MAILTOOL command will be overridden with the values from the configuration file EXCEPT for the recipients (TO, CC and BCC) and Attachments or if the value of the default is blank or doesn't exist in the configuration file used.

This ability has also been added to the SPL2EMAIL and SPL2EMAILB commands for compatibility.

Configuration Files can also be used in the ILE functions.  The only difference is any of the values that once the #mailtool_loadDefaults() function is called any values in the configuration file will override any values already set by #mailtool_setValue().  The opposite is also true, so be sure to do things in the correct order for your project.

 /COPY QCOPYSRC,P.MAILTOOL                                                   

 /free                                                                       
  if (mailtool_init() >= 0);                                                
    mailtool_setValue('configuration_file':'/bvstools/mailtool/config/myconfig.json');
    mailtool_loadDefaults();
    mailtool_addTORecipient('james@thegiantpeach.com');                      
    rc = mailtool_sendMail();

    if (rc < 0);
      //handle error
    endif; 
   
  endif;                
                        
  *INLR = *ON;          
                        
 /end-free 

In this example the only value we are setting is the TO address.  Everything else, including the FROM address, Subject and Message are set using the values in the configuration file.

So, what does one of these configuration files look like?  Here's an example of the one that is shipped with MAILTOOL:

{
	"variables": [
	{
		"name":"from_email",
		"default":" "
	},
	{
		"name":"from_name",
		"default":" "
	},
	{
		"name":"subject",
		"default":" "
	},
	{
		"name":"message",
		"default":" "
	},
	{
		"name":"reply_to_email",
		"default":" "
	},
	{
		"name":"reply_to_name",
		"default":" "
	},
	{
		"name":"mdn_email",
		"default":" "
	},
	{
		"name":"mdn_name",
		"default":" "
	},
	{
		"name":"sender_email",
		"default":" "
	},
	{
		"name":"sender_name",
		"default":" "
	},
	{
		"name":"footer_stream_file",
		"default":" "
	},
	{
		"name":"message_id",
		"default":" "
	},
	{
		"name":"message_id_domain",
		"default":" "
	},
	{
		"name":"send_with_server_type",
		"default":"*IBMSMTP"
	},
	{
		"name":"mail_router",
		"default":"*NONE"
	},
	{
		"name":"use_mail_router",
		"default":"*NO"
	},
	{
		"name":"perform_mx_lookup",
		"default":"*YES"
	},
	{
		"name":"use_ssl",
		"default":"*NO"
	},
	{
		"name":"smtp_port",
		"default":"25"
	},
	{
		"name":"smtp_auth_user",
		"default":" "
	},
	{
		"name":"smtp_auth_password",
		"default":" "
	},
	{
		"name":"bind_to_ip_address",
		"default":" "
	},
	{
		"name":"timeout",
		"default":"30"
	},
	{
		"name":"ccsid",
		"default":"819"
	},
	{
		"name":"no_attachment_action",
		"default":" "
	},
	{
		"name":"body_content_type",
		"default":"text/plain"
	},
	{
		"name":"mime_ccsid",
		"default":"819"
	},
	{
		"name":"temporary_directory",
		"default":"/tmp"
	},
	{
		"name":"importance",
		"default":"normal"
	},
	{
		"name":"priority",
		"default":" "
	},
	{
		"name":"recipient_separator",
		"default":","
	},
	{
		"name":"close_connection",
		"default":"*YES"
	},
	{
		"name":"save_email",
		"default":"*DFT"
	},
	{
		"name":"debug",
		"default":"*NO"
	},
	{
		"name":"debug_file",
		"default":"mailtooldebug_/%id%/.txt"
	},
	{
		"name":"debug_smtp_file",
		"default":"mailtoolsmtp_/%id%/.txt"
	}
	]
}

As you can see, the values are simple name/value pairs that can be easily edited and copied!

For most people, they will only use certain information in the configuration file, for example just the information for the mail router and credentials:

{
	"variables": [
    {
		"name":"from_email",
		"default":"bvstone@bvstools.onmicrosoft.com"
	},
	{
		"name":"from_name",
		"default":"Brad Stone @ Office 365"
	},
	{
		"name":"send_with_server_type",
		"default":"*MAILTOOL"
	},
	{
		"name":"mail_router",
		"default":"smtp.office365.com"
	},
	{
		"name":"use_mail_router",
		"default":"*ONLY"
	},
	{
		"name":"use_ssl",
		"default":"*TLS"
	},
	{
		"name":"smtp_port",
		"default":"587"
	},
	{
		"name":"smtp_auth_user",
		"default":"bvstone@bvstools.onmicrosoft.com"
	},
	{
		"name":"smtp_auth_password",
		"default":"mypassword"
	}	]
}


More information can be found in the MAILTOOL documentation at http://docs.bvstools.com.

MAILTOOL can be downloaded from https://www.bvstools.com/mailtool.html


Last edited 10/10/2022 at 11:57:16




Reply




© Copyright 1983-2024 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, TinyMCE and running on the IBM i (AKA AS/400, iSeries, System i).