This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v18.4
SMTP_transporter.send( )

SMTP_transporter.send( ) 


 

SMTP_transporter.send ( mail ) -> Result 
Parameter Type   Description
mail  Object in Email to send
Result  Object in SMTP status

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

Note: The transporter object must have already been created using the SMTP New transporter command.

The method 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 SMTP_transporter.send( ), otherwise it stays alive until the transporter object is destroyed. For more information, please refer to the SMTP New transporter command description.

In mail, pass a valid mail 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. The properties supported are:

 

PropertyTypeDescription
fromText | Object | Collection(*) Originating address(es)(**)
ccText | Object | Collection(*)Carbon Copy (CC). Additional email recipient(s)
bccText | Object | Collection(*)Blind Carbon Copy (BCC). Hidden email recipient(s)
toText | Object | Collection(*)Primary recipient(s)
senderText | Object | Collection(*)Email source address(es)(**)
replyToText | Object | Collection(*)Address(es) for responses
subjectText Description of topic
textBodyText Plain text representation of the email message (default charset is UTF-8)(***)
htmlBodyText HTML representation of the email message (default charset is UTF-8)(***)
sendAtTextEmail timestamp (UTCDate) 
attachmentsCollection of objectsCollection of attachment object(s), see MAIL New attachment
messageIdTextMessage identifier header ("message-id"). Usually "lettersOrNumbers@domainname", e.g. "abcdef.123456@4d.com". This unique ID is used in particular on forums or public mailing lists. In general, mail servers automatically add this header to the messages they send. 
inReplyToTextMessage identifier(s) of the original message(s) to which the current message is a reply (****). 
referencesCollection of textCollection of all message-ids of messages in the preceding reply chain (****).
keywordsObject"keywords" header. Contains a set of keywords as an object, where each property name is a keyword and each value is true. See RFC#4021.
.<keyword>BooleanKeyword to set (value must be true). Ex:
 $mail.keywords["$flagged"]:=True
 $mail.keywords["4d"]:=True
commentsTextAdditional comments header. Comments only appear within the header section of the message (keeping the message's body untouched) (****).
headersCollection of objectsCollection of EmailHeader objects, in the order they appear in the message. Allows users to add extended (registered) headers or user-defined (not registered, starting with "X") headers. Note: If an EmailHeader object property defines a header such as "from" or "cc" which is already set as a property at the mail level, the EmailHeader property is ignored.
[ ].nameText(mandatory) Header field name as defined in RFC#5322. If null or undefined, the header field is not added to the MIME header.
[ ].valueTextHeader field name as defined in RFC#5322

Note: Header fields starting with "Content-" must NOT be specified in the mail object.

The following additional properties are used when the mail object is built from a MIME document, e.g. when generated by the MAIL Convert from MIME command. In this case, both bodyStructure and bodyValues properties must be passed together, and it is not recommended to use htmlBody and textBody

bodyStructureObject(optional) Full MIME structure of the message body (EmailBodyPart object)
partIDTextIdentifies the part uniquely within the email
typeText(mandatory) Value of the Content-Type header field of the part
charsetTextValue of the charset parameter of the Content-Type header field
encodingTextif isEncodingProblem=true, the Content-Transfer-Encoding value is added (by default undefined)
dispositionTextValue of the Content-Disposition header field of the part
languageCollection of textsList of language tags, as defined in RFC3282, in the Content-Language header field of the part, if present.
locationTextURI, as defined in RFC2557, in the Content-Location header field of the part, if present.
subPartsCollection of objectsBody parts of each child (collection of EmailBodyPart objects)
headersCollection of objectsList of all header fields in the part, in the order they appear in the message (collection of EmailHeader objects, see header property)
bodyValuesObject(optional) EmailBodyValue object, containing an object for each partID of bodyStructure
<partID>.valueTextValue of the body part
<partID>.isEncodingProblemBooleanTrue if malformed sections are found while decoding the charset, or unknown charset, or unknown content transfer-encoding. False by default

Example of bodyStructure and bodyValues objects:

"bodyStructure": {
  "type": "multipart/mixed",
  "subParts": [
    {
      "partId": "p0001",
      "type": "text/plain"
    },
    {
      "partId": "p0002",
      "type": "text/html"
    }
  ]
},
"bodyValues": {
  "p0001": {
    "value": "I have the most brilliant plan. Let me tell you all about it."
  },
  "p0002": {
    "value": "<!DOCTYPE html><html><head><title></title><style type=\"text/css\">div{font-size:16px}</style></head><body><div>I have the most brilliant plan. Let me tell you all about it.</div></body></html>"
  }
}

(*) All properties that contain email addresses (from, cc, bcc, to, sender, replyTo) accept a value of text, object, or collection type.

  • Text:
    • single email: "somebody@domain.com"
    • single display name+email: "Somebody <somebody@domain.com>"
    • several emails: "Somebody <somebody@domain.com>,me@home.org"
  • Object:
    An object with two properties:
    PropertyTypeDescription
    nameTextDisplay name (can be null)
    emailTextEmail address
  • Collection:
    A collection of address objects.

(**) Each email you send out has both the sender and from addresses:

  • the sender domain is what the receiving email server gets when opening the session,
  • the from address is what the recipient(s) will see.
    For better deliverability, it is recommended to use the same from and sender addresses.

(***) If textBody and htmlBody are both filled, the MIME content-type multipart/alternative is used. The email client should then recognize the multipart/alternative part and display the text part or html part as necessary.
It is recommended that these properties be omitted if bodyStructure and bodyValues are used. 

(****) For specific formatting requirements, please consult RFC#5322.

Note: 4D follows the JMAP specification to format the mail object.

 

Returned object

SMTP_transporter.send( ) returns an object describing the SMTP status of the operation. This object can contain the following properties:

PropertyTypeDescription
successBooleanTrue if the send operation is successful, False otherwise
statusNumberStatus code returned by the SMTP server
statusTextTextMessage returned by the SMTP server

Note: For information about SMTP status codes, please go to this page.

 

Error handling

In case of an issue unrelated to the SMTP processing (e.g. a mandatory property is missing in mail), 4D generates an error that you can intercept using a method installed by the ON ERR CALL command. Use the GET LAST ERROR STACK command for information about the error. 

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

  • success: false
  • status: 0
  • statusText: "Failed to send email"

Example  

This code builds and sends an email:

 C_OBJECT($server)
 $server:=New object
 $server.host:="mail.smtp.host" // Mandatory
 $server.port:=25 // Optional, default value: 587
 
 $transporter:=SMTP New transporter($server)
 
 C_COLLECTION($headers//add user-defined headers
 $headers:=New collection
 $headers.push(New object("name";"X-MAILER";"value";"MySoftware"))
 $headers.push(New object("name";"X-4D-VERSION";"value";"17Rx"))
 
 C_OBJECT($email)
 $email:=New object
 $email.subject:="my first mail"
 $email.to:="test.mail@4d.com"
 $email.from:="test.noreply@4d.com"
 $email.textBody:="this is a test"
 $email.attachments:=New collection(MAIL New attachment("schedule.xlsx"))
 $email.headers:=$headers
 $email.keywords:=New object("technical";True;"advanced";True;"useless";False)
 
 $status:=$transporter.send($email)
 If(Not($status.success))
    ALERT("An error occurred sending the mail: "+$status.statusText)
 End if



See also 

MAIL Convert from MIME
MAIL Convert to MIME
MAIL New attachment
SMTP New transporter
SMTP_transporter.checkConnection( )

 
PROPERTIES 

Product: 4D
Theme: Mail

This command modifies the Error system variable

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v17 R4
Modified: 4D v17 R5

 
ARTICLE USAGE

4D Language Reference ( 4D v18)
4D Language Reference ( 4D v18.4)
4D Language Reference ( 4D v18.6)