Skip to main content
Version: 1.0.0

SMTPTransporter

The SMTPTransporter class allows you to configure SMTP connections and send emails.

Functions and properties

SMTP Transporter objects provide the following properties and functions:















4D.SMTPTransporter.new()

4D.SMTPTransporter.new*( server : object ) : 4D.SMTPTransporter

ParameterTypeDescription
serverobjectMail server information
Result4D.SMTPTransporterSMTP transporter object

Description

The 4D.SMTPTransporter.new() function configures a new SMTP connection according to the server parameter and returns a new transporter object. The returned transporter object will then usually be used to send emails.

note

This function does not open any connection to the SMTP server. The SMTP connection is actually opened when the .send() function is executed.

The SMTP connection is automatically closed:

  • when the transporter object is destroyed if the keepAlive property is true (default),
  • after each .send() function execution if the keepAlive property is set to false.

In the server parameter, pass an object containing the following properties:

serverDefault value (if omitted)

False
.accessTokenOAuth2: string
.accessTokenOAuth2: object
Text string or token object representing OAuth2 authorization credentials. Used only with OAUTH2 authenticationMode. If accessTokenOAuth2 is used but authenticationMode is omitted, the OAuth 2 protocol is used (if allowed by the server). Not returned in the SMTP transporter object.
none

the most secure authentication mode supported by the server is used

mail mode UTF8 (US-ASCII_UTF8_QP)

30

mail mode UTF8 (US-ASCII_UTF8_QP)

mandatory

True

none
password : string
User password for authentication on the server. Not returned in the SMTP transporter object.
none

587

100

none

Result

The function returns a SMTP transporter object. All returned properties are read-only.

Example

var server : object
var transporter : 4D.SMTPTransporter
var status : object
var info : string

server = newObject()
server.host = "smtp.gmail.com" //Mandatory
server.port = 465
server.user = "qodly@gmail.com"
server.password = "XXXX"
server.logFile = "LogTest.txt" //Log to save in the Logs folder

transporter = 4D.SMTPTransporter.new (server)

email = newObject()
email.subject = "my first mail"
email.from = "qodly@gmail.com"
email.to = "qodly@qodly.com , test@qodly.com"
email.stringBody = "Hello World"
email.htmlBody = "<h1>Hello World</h1><h4>'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...'</h4>\
<p>There are many variations of passages of Lorem Ipsum available."\
+"The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>"

status = transporter.send(email)
if(not(status.success))
info = "An error occurred: "+status.message
end

For information about SMTP status codes, please refer to this page.

Example

var options : object
var transporter : 4D.SMTPTransporter
var info : string

options = newObject()

options.host = "smtp.gmail.com"
options.user = "test@gmail.com"
options.password = "XXXXXX"

transporter = 4D.SMTPTransporter.new (options)

status = transporter.checkConnection()
if(status.success == true)
info = "SMTP connection check successful!"
else
info = "Error # "+string(status.status) + ", " + status.statusText)
end

.keepAlive

.keepAlive* : boolean

Description

The .keepAlive property contains true if the SMTP connection must be kept alive until the transporter object is destroyed, and false otherwise. By default, if the keepAlive property has not been set in the server object (used to create the transporter object), it is true.

The SMTP connection is automatically closed:

  • when the transporter object is destroyed if the .keepAlive property is true,
  • after each .send() function execution if the .keepAlive property is set to false.

.send()

.send*( mail : object ) : object

ParameterTypeDescription
mailobjectEmail to send
ResultobjectSMTP status

Description

The .send() function sends the mail object to the SMTP server defined in the transporter object and returns a status object.

The transporter object must have already been created using the 4D.SMTPTransporter.new() constructor.

The function creates the SMTP connection if it is not already alive. If the .keepAlive property of the transporter object is false, the SMTP connection is automatically closed after the execution of .send(), otherwise it stays alive until the transporter object is destroyed. For more information, please refer to the 4D.SMTPTransporter.new() constructor description.

In mail, pass a valid Email object to send. The origination (where the email is coming from) and destination (one or more recipients) properties must be included, the remaining properties are optional.

Returned object

The function returns an object describing the SMTP status of the operation. This object can contain the following properties:

PropertyTypeDescription
successbooleanTrue if the send is successful, false otherwise
statusnumberStatus code returned by the SMTP server (0 in case of an issue unrelated to the mail processing)
statusTextstringStatus message returned by the SMTP server

In case of an issue unrelated to the SMTP processing (e.g. a mandatory property is missing in mail), Qodly generates an error that you can intercept.

In this case, the resulting status object contains the following values:

PropertyValue
successfalse
status0
statusText"Failed to send email"