Skip to main content
Version: Next

MailAttachment

Attachment objects allow referencing files within a Email object.

Functions and properties

Attachment objects are created using the 4D.MailAttachment.new() function. They provide the following functions and read-only properties:

4D.MailAttachment.new( file : 4D.File { , name : string {, cid : string{ , type : string { , disposition : string } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( zipFile : 4D.ZipFile { , name : string {, cid : string{ , type : string { , disposition :string } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( blob : 4D.Blob { , name : string {, cid : string{ , type : string { , disposition :string } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( path : string { , name : string {, cid : string{ , type : string { , disposition :string } } } } ) : 4D.MailAttachment
    allows you to create an attachment object that you can add to an Email object
.cid : string     the ID of the attachment
.disposition : string    the value of the Content-Disposition header
.getContent() : 4D.Blob    returns the contents of the attachment object in a 4D.Blob object
.name : string    the name and extension of the attachment
.path : string    the POSIX path of the attachment file, if it exists
.platformPath : string    the path of the attachment file expressed with the current platform syntax
.type : string    the content-type of the attachment file

4D.MailAttachment.new()

4D.MailAttachment.new( file : 4D.File { , name : string {, cid : string{ , type : string { , disposition : string } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( zipFile : 4D.ZipFile { , name : string {, cid : string{ , type : string { , disposition :string } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( blob : 4D.Blob { , name : string {, cid : string{ , type : string { , disposition :string } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( path : string { , name : string {, cid : string{ , type : string { , disposition :string } } } } ) : 4D.MailAttachment

ParameterTypeDescription
file4D.FileAttachment file
zipFile4D.ZipFileAttachment Zipfile
blob4D.BlobBLOB containing the attachment
pathstringPath of the attachment file
namestringName + extension used by the mail client to designate the attachment
cidstringID of attachment (HTML messages only), or " " if no cid is required
typestringValue of the content-type header
dispositionstringValue of the content-disposition header: "inline" or "attachment".
Result4D.MailAttachmentAttachment object

Description

The 4D.MailAttachment.new function allows you to create an attachment object that you can add to an Email object.

To define the attachment, you can use:

  • a file, pass a 4D.File object containing the attachment file.
  • a zipfile, pass a 4D.ZipFile object containing the attachment file.
  • a blob, pass a 4D.Blob object containing the attachment itself.
  • a path, pass a string value containing the path of the attachment file, expressed with the system syntax. You can pass a full path name or a simple file name (in which case 4D will search for the file in the same directory as the project file).

The optional name parameter lets you pass the name and extension to be used by the mail client to designate the attachment. If name is omitted and:

  • you passed a file path, the name and extension of the file is used,
  • you passed a BLOB, a random name without extension is automatically generated.

The optional cid parameter lets you pass an internal ID for the attachment. This ID is the value of the Content-Id header, it will be used in HTML messages only. The cid associates the attachment with a reference defined in the message body using an HTML tag such as \<img src = "cid:ID">. This means that the contents of the attachment (e.g., a picture) should be displayed within the message on the mail client. The final result may vary depending on the mail client. You can pass an empty string in cid if you do not want to use this parameter.

You can use the optional type parameter to explicitly set the content-type of the attachment file. For example, you can pass a string defining a MIME type ("video/mpeg"). This content-type value will be set for the attachment, regardless of its extension. For more information about MIME types, please refer to the MIME type page on Wikipedia.

By default, if the type parameter is omitted or contains an empty string, the content-type of the attachment file is based on its extension. The following rules are applied for the main MIME types:

ExtensionContent Type
jpg, jpegimage/jpeg
pngimage/png
gifimage/gif
pdfapplication/pdf
docapplication/msword
xlsapplication/vnd.ms-excel
pptapplication/vnd.ms-powerpoint
zipapplication/zip
gzapplication/gzip
jsonapplication/json
jsapplication/javascript
psapplication/postscript
xmlapplication/xml
htm, htmltext/html
mp3audio/mpeg
otherapplication/octet-stream

The optional disposition parameter lets you pass the content-disposition header of the attachment. You can pass one of the following constants:

ConstantValueComment
kMailDispositionAttachment"attachment"Set the Content-disposition header value to "attachment", which means that the attachment file must be provided as a link in the message.
kMailDispositionInline"inline"Set the Content-disposition header value to "inline", which means that the attachment must be rendered within the message contents, at the "cid" location. The rendering depends on the mail client.

By default, if the disposition parameter is omitted:

  • if the cid parameter is used, the Content-disposition header is set to "inline",
  • if the cid parameter is not passed or empty, the Content-disposition header is set to "attachment".

Example

You want to send an email with a file as an attachment and an image embedded in the HTML body:

var attachment : 4D.File
var email,server : object
var transporter : 4D.SMTPTransporter

attachment = file("/PACKAGE/Docs/cv-john-smith.pdf")

server = newObject
server.host = "smtp.mail.com"
server.user = "test_user@mail.com"
server.password = "p@ssw@rd"
transporter = 4D.SMTPTransporter.new(server)

email = newObject
email.from = "test_user@mail.com"
email.to = "test_user@mail.com"
email.subject = "This is a test message with attachments"

//add a link to download file
email.attachments = newCollection(4D.MailAttachment.new(attachment))
//insert an inline picture (use a cid)
email.attachments[1] = 4D.MailAttachment.new(file("/PACKAGE/Docs/photo.jpg"),"","Qodly")

email.htmlBody = "<html>"+\
"<body>Hello World!"+\
"<img src = 'cid:Qodly' >"+\
"</body>"+\
"</head>"+\
"</html>"

transporter.send(email) //send mail

.cid

.cid : string

Description

The .cid property contains the ID of the attachment. This property is used in HTML messages only. If this property is missing, the file is handled as a simple attachment (link).

.disposition

.disposition : string

Description

The .disposition property contains the value of the Content-Disposition header. Two values are available:

  • "inline": the attachment is rendered within the message contents, at the "cid" location. The rendering depends on the mail client.
  • "attachment": the attachment is provided as a link in the message.

.getContent()

.getContent() : 4D.Blob

ParameterTypeDescription
Result4D.BlobContent of the attachment

Description

The .getContent() function returns the contents of the attachment object in a 4D.Blob object. You can use this method with attachment objects received by the mailConvertFromMIME command.

.name

.name : string

Description

The .name property contains the name and extension of the attachment. By default, it is the name of the file, unless another name was specified in the 4D.MailAttachment.new() command.

.path

.path : string

Description

The .path property contains the POSIX path of the attachment file, if it exists.

.platformPath

.platformPath : string

Description

The .platformPath property returns the path of the attachment file expressed with the current platform syntax.

.type

.type : string

Description

The .type property contains the content-type of the attachment file. If this type is not explicitly passed to the 4D.MailAttachment.new() command, the content-type is based on its file extension.