ConFoo 2025

imap_mail_compose

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_mail_composeCrée un message MIME

Description

imap_mail_compose(array $envelope, array $bodies): string|false

Crée un message MIME basé sur l'enveloppe envelope et les sections bodies.

Liste de paramètres

envelope

Un tableau associatif contenant les champs des en-têtes. Les clés valides sont : "remail", "return_path", "date", "from", "reply_to", "in_reply_to", "subject", "to", "cc", "bcc" et "message_id", qui définie les en-têtes respectifs à la chaîne de caractères donnée. Pour définir des en-têtes additionnels, la clé "custom_headers" est supporté, qui attend un tableau de ces en-têtes, par exemple ["User-Agent: My Mail Client"].

bodies

Un tableau indexé des corps. Le premier corps est le corps principal du message ; seulement s'il est du type TYPEMULTIPART, les corps suivant seront traité ; ces corps constitue les corps des parties.

Structure d'un Tableau d'un Corps
Clé Type Description
type int Le type MIME. Un de TYPETEXT (par défault), TYPEMULTIPART, TYPEMESSAGE, TYPEAPPLICATION, TYPEAUDIO, TYPEIMAGE, TYPEMODEL ou TYPEOTHER.
encoding int Le Content-Transfer-Encoding. Un de ENC7BIT (par défault), ENC8BIT, ENCBINARY, ENCBASE64, ENCQUOTEDPRINTABLE or ENCOTHER.
charset string Le jeu d'encodage du paramètre du type MIME.
type.parameters array Un tableau associatif de nom de paramètre Content-Type et leur valeurs.
subtype string Le sous-type MIME, e.g. 'jpeg' pour TYPEIMAGE.
id string Le Content-ID.
description string Le Content-Description.
disposition.type string Le Content-Disposition, e.g. 'attachment'.
disposition array Un tableau associatif de nom de paramètre Content-Disposition et leur valeurs.
contents.data string La charge utile.
lines int La taille de la charge utile en lignes.
bytes int La taille de la charge utile en octets.
md5 string La checksum MD5 de la charge utile.

Valeurs de retour

Retourne le message MIME en tant que chaîne de caractères, ou false si une erreur survient.

Exemples

Exemple #1 Exemple avec imap_mail_compose()

<?php

$envelope
["from"]= "joe@example.com";
$envelope["to"] = "foo@example.com";
$envelope["cc"] = "bar@example.com";

$part1["type"] = TYPEMULTIPART;
$part1["subtype"] = "mixed";

$filename = "/tmp/imap.c.gz";
$fp = fopen($filename, "r");
$contents = fread($fp, filesize($filename));
fclose($fp);

$part2["type"] = TYPEAPPLICATION;
$part2["encoding"] = ENCBINARY;
$part2["subtype"] = "octet-stream";
$part2["description"] = basename($filename);
$part2["contents.data"] = $contents;

$part3["type"] = TYPETEXT;
$part3["subtype"] = "plain";
$part3["description"] = "description3";
$part3["contents.data"] = "contents.data3\n\n\n\t";

$body[1] = $part1;
$body[2] = $part2;
$body[3] = $part3;

echo
nl2br(imap_mail_compose($envelope, $body));

?>

add a note

User Contributed Notes 5 notes

up
5
jvandeweghe at kipsu dot com
7 years ago
The custom_headers envelope documentation is misleading. Its not actually an "associative array", its a regular array of headers.

This is wrong:

<?php
$envelope
= [
//...
"custom_headers" => [
"X-SES-CONFIGURATION-SET" => "example",
"X-SES-MESSAGE-TAGS" => "emailType=example"
]
];
?>

This is right:

<?php
$envelope
= [
//...
"custom_headers" => [
"X-SES-CONFIGURATION-SET: example",
"X-SES-MESSAGE-TAGS: emailType=example"
]
];
?>
up
3
prices at dflytech dot com
22 years ago
The documentation above does not mention that you can use the index ["charset"] to set the character set of the messsage part.

Example:

$part1["type"]= "TEXT";
$part1["subtype"]="PLAIN";
$part1["charset"] = "koi8-r";

to send a message in Russian-koi8.

Scott =)
up
3
Los Olvidados
21 years ago
If you wish to send the output of this function, simply use it for the headers argument of imap_mail() or mail(). Keep in mind that those functions set the To: and Subject: headers, so including them in the envelope will create double entries.
up
2
thomas dot hebinck at digionline dot de
21 years ago
It is a good idea to set the date header:
$envelope['date']=date('r');
up
2
Guido
16 years ago
For some email clients its necessary first to start with the body text and end with the attachment(s). Otherwise all the parts end up in attachments, also the body text (took a while to find this).

So example #1 (above) should be switched over, like:

$body[1] = $part1;
$body[2] = $part3;
$body[3] = $part2;
To Top