4D v18

MAIL New attachment

Home

 
4D v18
MAIL New attachment

MAIL New attachment 


 

MAIL New attachment ( value {; name {; cid {; type {; disposition}}}} ) -> Function result 
Parameter Type   Description
value  Text, BLOB in Path of the attachment file (text), or
Blob containing the attachment (BLOB)
name  Text in Name + extension used by the mail client to designate the attachement
cid  Text in ID of attachment (HTML messages only), or " " if no cid is required
type  Text in Value of the content-type header
disposition  Text in Value of the content-disposition header: "inline" or "attachment".
Function result  Object in Attachment object

The MAIL New attachment command allows you to create an attachment object that you can add to an Email object (see SMTP_transporter.send( )).

In value, pass the contents of the attachment. You can pass either:

  • a text 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 database structure file).
  • or a BLOB value containing the attachment itself.

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:

  • value is a file path, the name and extension of the file is used,
  • value is 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 this 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 from the "Mail" constant theme:

Constant Value Comment
mail disposition attachment attachment Set the Content-disposition header value to "attachment", which means that the attachment file must be provided as a link in the message.
mail disposition inline 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".

MAIL New attachment returns a new attachment object that you can add to the attachments collection of the mail object handled by the transporter.send( ) method. The object contains the following read-only properties and methods: 

 

PropertyTypeDescription
attachment.pathTextFull path of the attachment (if it exists)
attachment.nameTextName and extension of the attachment. By default, the name of the file.
attachment.dispositionTextValue of the Content-Disposition header: "inline" or "attachment".
attachment.typeTextContent type of the attachment file. If this type is not explicitly passed to the MAIL New attachment command, the content-type is based on its file extension. 
attachment.cidTextID of the attachment (used in HTML messages only). If this property is missing, the file is handled as a simple attachment (link)

 

MethodDescription
attachment.getContent( )Returns attachment contents

 

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

 $doc:=Select document("";"*";"Please select a file to attach";0)
 If(OK=1) //If a document was selected
 
    C_OBJECT($email;$server;$transporter)
 
    $server:=New object
    $server.host:="smtp.mail.com"
    $server.user:="test_user@mail.com"
    $server.password:="p@ssw@rd"
    $transporter:=SMTP New transporter($server)
 
    $email:=New object
    $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:=New collection(MAIL New attachment(Document))
  //insert an inline picture (use a cid)
    $email.attachments[1]:=MAIL New attachment("c:\\Pictures\\4D.jpg";"";"4D")
 
    $email.htmlBody:="<html>"+\
    "<body>Hello World!"+\
    "<img src='cid:4D' >"+\
    "</body>"+\
    "</head>"+\
    "</html>"
 
    $transporter.send($email//send mail
 
 End if

You want to send an email with a 4D Write Pro area as an attachment:

 C_BLOB($blob)
 WP EXPORT VARIABLE(WPArea;$blob;wk docx)
 
 C_OBJECT($email;$server;$transporter)
 
 $server:=New object
 $server.host:="smtp.mail.com"
 $server.user:="user@mail.com"
 $server.password:="p@ssw@rd"
 $transporter:=SMTP New transporter($server)
 
 $email:=New object
 $email.from:="user@mail.com"
 $email.to:="customer@mail.com"
 $email.subject:="New annual report"
 $email.textBody:="Please find enclosed our latest annual report."
 $email.attachments:=New collection(MAIL New attachment($blob;"Annual report.docx"))
 
 $transporter.send($email)



See also 

attachment.cid
attachment.disposition
attachment.getContent( )
attachment.name
attachment.path
attachment.type
SMTP_transporter.send( )

 
PROPERTIES 

Product: 4D
Theme: Mail
Number: 1644

This command can be run in preemptive processes

 
HISTORY 

Created: 4D v17 R4

 
ARTICLE USAGE

4D Language Reference ( 4D v18)