MailAttachment
Attachment objects allow referencing files within a Email
object. Attachment objects are created using the MAIL New attachment
command.
Objet Attachment
Les objets Attachment fournissent les propriétés et fonctions suivantes en lecture seule :
.cid : Text the ID of the attachment |
.disposition : Text the value of the Content-Disposition header |
.getContent() : 4D.Blob returns the contents of the attachment object in a 4D.Blob object |
.name : Text the name and extension of the attachment |
.path : Text the POSIX path of the attachment file, if it exists |
.platformPath : Text the path of the attachment file expressed with the current platform syntax |
.size : Integer the value of the size header of the attachment file |
.type : Text the content-type of the attachment file |
MAIL New attachment
Historique
Release | Modifications |
---|---|
19 R2 | Accepte 4D.File, 4D.ZipFile, 4D.Blob |
MAIL New attachment( file : 4D.File { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
MAIL New attachment( zipFile : 4D.ZipFile { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
MAIL New attachment( blob : 4D.Blob { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
MAIL New attachment( path : Text { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
Paramètres | Type | Description | |
---|---|---|---|
file | 4D.File | -> | Fichier joint |
zipFile | 4D.ZipFile | -> | Fichier Zip joint |
blob | 4D.Blob | -> | Blob contenant la pièce jointe |
path | Text | -> | Chemin de la pièce jointe |
name | Text | -> | Nom + extension utilisés par le client de messagerie pour désigner la pièce jointe |
cid | Text | -> | ID de la pièce jointe (messages HTML uniquement) ou " " si aucun cid n'est requis |
type | Text | -> | Valeur de l'en-tête content-type |
disposition | Text | -> | Valeur de l'en-tête content-disposition : "inline" ou "attachment" |
Résultat | 4D.MailAttachment | <- | Objet pièce jointe |
Description
The MAIL New attachment
command allows you to create an attachment object that you can add to an Email object.
Pour définir l'objet attachment, vous pouvez utiliser :
- 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 text value containing the path of the attachment file, expressed with the system syntax. Vous pouvez passer un nom de chemin complet ou un simple nom de fichier (auquel cas 4D recherchera le fichier dans le même répertoire que le fichier du projet).
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:
- vous avez passé un chemin d'accès au fichier, le nom et l'extension du fichier sont utilisés,
- vous avez passé un BLOB, un nom aléatoire sans extension est automatiquement généré.
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">
. Cela signifie que le contenu de la pièce jointe (par exemple, une image) doit être affiché dans le message sur le client de messagerie. Le résultat final peut varier en fonction du client de messagerie. 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. Par exemple, vous pouvez passer une chaîne définissant un type MIME ("video/mpeg"). Cette valeur de content-type sera définie pour la pièce jointe, quelle que soit son 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. Les règles suivantes sont appliquées pour les principaux types MIME :
Extension | Content Type |
---|---|
jpg, jpeg | image/jpeg |
png | image/png |
gif | image/gif |
application/pdf | |
doc | application/msword |
xls | application/vnd.ms-excel |
ppt | application/vnd.ms-powerpoint |
zip | application/zip |
gz | application/gzip |
json | application/json |
js | application/javascript |
ps | application/postscript |
xml | application/xml |
htm, html | text/html |
mp3 | audio/mpeg |
other | application/octet-stream |
The optional disposition parameter lets you pass the content-disposition
header of the attachment. Vous pouvez passer l'une des constantes suivantes du thème de constantes "Mail" :
Constante | Valeur | Commentaire |
---|---|---|
mail disposition attachment | "attachment" | Définissez la valeur de l'en-tête Content-disposition sur "attachment", ce qui signifie que le fichier joint doit être fourni sous forme de lien dans le message. |
mail disposition inline | "inline" | Définissez la valeur de l'en-tête Content-disposition sur "inline", ce qui signifie que la pièce jointe doit être rendue dans le contenu du message, à l'emplacement du "cid". Le rendu dépend du client de messagerie. |
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".
Exemple 1
Vous souhaitez envoyer un e-mail avec un fichier sélectionné par l'utilisateur comme pièce jointe et une image intégrée dans le corps HTML :
$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
Exemple 2
Vous voulez envoyer un e-mail avec une zone 4D Write Pro en pièce jointe :
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)
4D.MailAttachment.new()
Historique
Release | Modifications |
---|---|
19 R2 | Accepte 4D.File, 4D.ZipFile, 4D.Blob |
4D.MailAttachment.new( file : 4D.File { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( zipFile : 4D.ZipFile { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( blob : 4D.Blob { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
4D.MailAttachment.new( path : Text { ; name : Text {; cid : Text{ ; type : Text { ; disposition :Text } } } } ) : 4D.MailAttachment
Paramètres | Type | Description | |
---|---|---|---|
file | 4D.File | -> | Fichier joint |
zipFile | 4D.ZipFile | -> | Fichier Zip joint |
blob | 4D.Blob | -> | Blob contenant la pièce jointe |
path | Text | -> | Chemin de la pièce jointe |
name | Text | -> | Nom + extension utilisés par le client de messagerie pour désigner la pièce jointe |
cid | Text | -> | ID de la pièce jointe (messages HTML uniquement) ou " " si aucun cid n'est requis |
type | Text | -> | Valeur de l'en-tête content-type |
disposition | Text | -> | Valeur de l'en-tête content-disposition : "inline" ou "attachment" |
Résultat | 4D.MailAttachment | <- | Objet pièce jointe |
Description
The 4D.MailAttachment.new()
function creates and returns a new object of the 4D.MailAttachment
type. It is identical to the MAIL New attachment
command (shortcut).
.cid
.cid : Text
Description
The .cid
property contains the ID of the attachment. Cette propriété est utilisée uniquement dans les messages HTML. Si cette propriété est manquante, le fichier est géré comme une simple pièce jointe (lien).
.disposition
.disposition : Text
Description
The .disposition
property contains the value of the Content-Disposition
header. .
- "inline" : la pièce jointe est rendue dans le contenu du message, à l'emplacement "cid". Le rendu dépend du client de messagerie.
- "attachment" : la pièce jointe est fournie sous forme de lien dans le message.
.getContent()
.getContent() : 4D.Blob
Paramètres | Type | Description | |
---|---|---|---|
Résultat | 4D.Blob | <- | Contenu de la pièce jointe |
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 MAIL Convert from MIME
command.
.name
.name : Text
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 MAIL New attachment
command.
.path
.path : Text
Description
The .path
property contains the POSIX path of the attachment file, if it exists.
.platformPath
Historique
Release | Modifications |
---|---|
19 | Ajout |
.platformPath : Text
Description
The .platformPath
property returns the path of the attachment file expressed with the current platform syntax.
.size
.size : Integer
Description
The .size
property contains the value of the size
header of the attachment file. The .size
property is returned when the MIME message defines a size header in the attachment part.
.type
.type : Text
Description
The .type
property contains the content-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.