PHP Conference Kansai 2025

imap_mail_compose

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

imap_mail_composeCrea un mensaje MIME

Descripción

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

Crea un mensaje MIME basado en el sobre envelope y las secciones bodies.

Parámetros

envelope

Un array asociativo que contiene los campos de los encabezados. Las claves válidas son: "remail", "return_path", "date", "from", "reply_to", "in_reply_to", "subject", "to", "cc", "bcc" y "message_id", que definen los encabezados respectivos al string dado. Para definir encabezados adicionales, la clave "custom_headers" es soportada, que espera un array de estos encabezados, por ejemplo ["User-Agent: My Mail Client"].

bodies

Un array indexado de los cuerpos. El primer cuerpo es el cuerpo principal del mensaje; solo si es del tipo TYPEMULTIPART, los cuerpos siguientes serán tratados; estos cuerpos constituyen los cuerpos de las partes.

Estructura de un Array de un Cuerpo
Clave Tipo Descripción
type int El tipo MIME. Uno de TYPETEXT (por omisión), TYPEMULTIPART, TYPEMESSAGE, TYPEAPPLICATION, TYPEAUDIO, TYPEIMAGE, TYPEMODEL o TYPEOTHER.
encoding int El Content-Transfer-Encoding. Uno de ENC7BIT (por omisión), ENC8BIT, ENCBINARY, ENCBASE64, ENCQUOTEDPRINTABLE o ENCOTHER.
charset string El juego de caracteres del parámetro del tipo MIME.
type.parameters array Un array asociativo de nombre de parámetro Content-Type y sus valores.
subtype string El subtipo MIME, e.g. 'jpeg' para TYPEIMAGE.
id string El Content-ID.
description string El Content-Description.
disposition.type string El Content-Disposition, e.g. 'attachment'.
disposition array Un array asociativo de nombre de parámetro Content-Disposition y sus valores.
contents.data string La carga útil.
lines int El tamaño de la carga útil en líneas.
bytes int El tamaño de la carga útil en bytes.
md5 string La checksum MD5 de la carga útil.

Valores devueltos

Devuelve el mensaje MIME como un string, o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplo con 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
23 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
22 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