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
Parameter | Type | Description | |
---|---|---|---|
server | object | → | Mail server information |
Result | 4D.SMTPTransporter | ← | SMTP 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.
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:
In the server parameter, pass an object containing the following properties:
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
Parameter | Type | Description | |
---|---|---|---|
object | → | Email to send | |
Result | object | ← | SMTP 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 the4D.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:
Property | Type | Description |
---|---|---|
success | boolean | True if the send is successful, false otherwise |
status | number | Status code returned by the SMTP server (0 in case of an issue unrelated to the mail processing) |
statusText | string | Status 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:
Property | Value |
---|---|
success | false |
status | 0 |
statusText | "Failed to send email" |