DataBasic Email API Program Example

The following example includes two alternate methods of setting the email configuration.

*
* The required INCLUDE
*
	INCLUDE #EM_INCLUDES FROM /SYSFILES/SYS.BASLIB
*
* Define an error handler
*
	ECATCH Error_Handler
*
* Start the email
*
* Method 1 - Using presets
*  Define the location of the presets and use items
*  for email server configuration and delivery options
*
*      EM_DEFINE_CONFIG("EM_CONFIG")
*      EM_START_EMAIL("EH_SERVER","")
*      EM_ADD_DELIVERY_DETAILS("ED_CLIENTS")
*
* Method 2 - Variables (using defaults)
*
	Host.Config = "":@AM:"":@AM:"localhost"
	Delivery.Config = "":@AM: ...
		"This is the subject":@AM: ...
		"receiver@example.com":@AM: ...
		"sender@localhost"
	EM_START_EMAIL("",Host.Config)
	EM_ADD_DELIVERY_DETAILS(Delivery.Config)
*  
* Set an optional parameter.
* Wait up to 20,000ms (20 seconds) for a connection
*
	EM_SET_PARAM("connect_timeout",20000)
*
* Attach a dynamic item from the results of a LISTU
*
	PERFORM "LISTU (N" CAPTURING Item.Content
	Item.Content = CHANGE(Item.Content,@AM, EM_CRLF)
	EM_ADD_ATTACHMENT(Item.Content, ...
		"LISTU.txt":@AM:"The LISTU results", ...
		EM_CONTENT_ATTACHED)
*
* Add an attachment from a Reality file (a PDF)
*
	EM_ADD_ATTACHMENT_ITEM("CONTENT":@AM: ...
		"example.pdf", ...
		"attached_example.pdf":@AM:"An attached pdf", ...
		EM_CONTENT_ATTACHED)
*
* Add content from a file to embed within the email
* body (an image) and record the generated link
*
	EM_ADD_ATTACHMENT_ITEM("CONTENT":@AM: ...
		"example.jpg","embedded_example.jpg":@AM: ...
		"An embedded image", EM_CONTENT_INLINE)
	Logo.Link = EM_LINK_DATA
*
* Add the plain text version of the email body
*
	EM_ADD_PLAIN_TEXT("This is the plain text version of the email.")
*
* Add the HTML version and an embedded attachment
*
	EM_ADD_HTML_TEXT( ...
		"<p>This is the <b>HTML</b> version with an embedded image. ": ...
         Logo.Link:"</p>")
*
* Enable output of debug information whilst sending if required
*
*    EM_ENABLE_DEBUG()
*
* Send the email and test for success
*
	IF (EM_SEND_EMAIL()) THEN
		PRINT "Successfully sent."
	END ELSE
		PRINTERR EM_ERROR_DETAILS
	END
*
* End of main program
*
	STOP
*
* Error handler
*
Error_Handler:
	PRINT "An error was encountered."
	PRINT "Number:  ":EXCEPT.NUM()
	PRINT "Message: ":EXCEPT.MSG()
	PRINT "Stack:   ":EXCEPT.STK()
	PRINTERR EM_ERROR_DETAILS
	STOP
*
* End of program
*
	END