Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : IMAP, POP3 and NNTP Functions : imap_mail_compose

imap_mail_compose

Create a MIME message based on given envelope and body sections (PHP 4, PHP 5)
string imap_mail_compose ( array envelope, array body )

Create a MIME message based on the given envelope and body sections.

Parameters

envelope

An associative array of headers fields

body

An indexed array of bodies

A body is an associative array which can consist of the following keys: "type", "encoding", "subtype", "description" and "contents.data"

Return Values

Returns the MIME message.

Examples

Example 1054. imap_mail_compose() example

<?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));

?>


Code Examples / Notes » imap_mail_compose

prices

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  =)


freman

Since at least PHP 4.3 the $part[] hash can contain the $part['type.parameters'] which expects a hash.
Unfortunately for "hans at lintoo dot dk" there doesn't appear to have ever been $part['parameters.name'] or a $part['dparameters.filename']
So it would appear the corrected code for his function is
<?php
           $part["type"] = TYPEAPPLICATION;
           $part["encoding"] = ENCBASE64;
           $part["subtype"] = "octet-stream";
           $part["description"] = $file_name;
           $part['disposition.type'] = 'attachment';
           $part['disposition'] = array ('filename' => $file_name);
           $part['type.parameters'] = array('name' => $file_name);
           $part["contents.data"] = base64_encode(fread($file_handle,$file_size));
?>
While this correctly sets the header
Content-Type: application/octet-stream; name="file_name"
Content-Transfer-Encoding: BASE64
Content-Description: file_name
Content-Disposition: attachment; filename="file_name"
But this is still not a good idea to blanket set every attachment as a application/octet-stream so please consider using the unix command file, or Fileinfo functions (or even trusting file upload mimetype) and pay attention to the code from "derf dot m at reseaunix dot net"


thomas dot hebinck

It is a good idea to set the date header:
$envelope['date']=date('r');


prices

In regards to my earlier note about "charset", this feature seems to have been added in the 4.1.X series  I have not actually checked 4.1.0, but it is in 4.1.1 and not it 4.0.6.

thomas dot hebinck

imap_mail_compose() uses \r\n at the end of each header (which is RFC conform) - but imap_mail() and mail() just use \n when adding their headers. So you get mixed up EOLs.
When your email client gets trouble finding the beginning of your body, try this (took me half a day):
$mail=imap_mail_compose($env,$body);
list($t_header,$t_body)=split("\r\n\r\n",$mail,2);
$t_header=str_replace("\r",'',$t_header);
$result=imap_mail($to,$subject,$t_body,$t_header);


los olvidados

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.

hans

I got a lot of problems with attachments (multipart emails) and the fix directly below this note.
instead just include it in the optional headers argument as previously suggested:
<?php
$mail = str_replace("\r","",imap_mail_compose($envelope, $body));
imap_mail($_POST["to"],$_POST["subject"],'',$mail);
?>
"Nothing like a fix on a fix".
If you want to copy the message to ie: a sent folder do:
<?php
//take note of the link: $this->mbox, and the constant: self::$imapStream
$envelope["to"]  = $_POST["to"]; //included with imap_mail
$envelope["subject"]  = $_POST["subject"]; //included with imap_mail
$mail = imap_mail_compose($envelope, $body); //note no problems with \r
imap_append($this->mbox,self::$imapStream."INBOX.Sent",$mail ,"\\Seen");
?>
See imap_append for more info.... Enjoy!


as1

How can I do if I have multi mime headers?
By example:
Content-Type: multipart/related;
                  type="multipart/alternative";
or:
Content-Type: multipart/mixed;
                  type="multipart/alternative";
or both methods: nested and multi mime headers:
Content-Type: multipart/mixed;
       boundary="147147"
--147147
Content-Type: multipart/related;
       type="multipart/alternative";
       boundary="247247"
These headers are used when you need attach files and
insert files in a body into mime mails.


hans

Here is a little something for attaching files... This is already documented in the manual, but my solution involves some changes. This is based on testing and suggestions from others notes on this page.
<?php
//snip
if (count($_FILES) > 0) {
$multipart["type"] = TYPEMULTIPART;
$multipart["subtype"] = "mixed";
$body[] = $multipart; //add multipart stuff
}
//snip
$uploaddir = ini_get("upload_tmp_dir"); //Get tmp upload dir from PHP.ini
foreach ($_FILES as $fieldName => $file) {
for ($i=0;$i < count($file['tmp_name']);$i++) {
if (is_uploaded_file($file['tmp_name'][$i])) {
$file_handle = fopen($file["tmp_name"][$i], "rb");
$file_name = $file["name"][$i];
$file_size = filesize($file["tmp_name"][$i]);

$part["type"] = TYPEAPPLICATION;
$part["encoding"] = ENCBASE64;
$part["subtype"] = "octet-stream";
$part["description"] = $file_name;
$part['disposition.type'] = 'attachment';
$part['disposition'] = array ('filename' => $file_name);
$part['dparameters.filename'] = $file_name;
$part['parameters.name'] = $file_name;
$part["contents.data"] = base64_encode(fread($file_handle,$file_size));

$body[] = $part;
unlink($file["tmp_name"][$i]);
}
}
}
//snip
?>
hope someone can use this...
Regards, Hans @ http://lintoo.dk/


derf dot m

class ImapCompose
{
var $DATA;
var $MIME;
var $TYPE;
var $BASENAME;

function ImapCompose()
{}

function LECTURE_FICHIER($filename,$mime,$filetmp)
{
$filetmp = "/tmp/".$filetmp;
$fp=fopen($filetmp,"r");
$this->DATA     = fread($fp,filesize($filetmp));
fclose($fp);
//unlink($filetmp);
$mime = explode("/",$mime);
$this->BASENAME = basename($filename);
$this->TYPE = $mime[1];

switch($mime[0])
{
case "image":
$this->MIME = TYPEIMAGE;
break;

case "text":
$this->MIME = TYPETEXT;
break;

case "audio":
$this->MIME = TYPEAUDIO;
break;

case "autre":
default:
$this->MIME = TYPEAPPLICATION;
break;
}
}
function LECTURE_FILE_MAIL($file,$data,$mime,$type)
{
$this->BASENAME = $file;
$this->MIME = $mime;
$this->DATA     = $data;
$this->TYPE = $type;
}

function ATTACHER_FICHIER()
{
$part["type"] =$this->MIME."
";
$part["encoding"] =ENCBINARY."
";
$part["subtype"] =$this->TYPE."
";
$part["description"]=$this->BASENAME."
";
$part["contents.data"]=$this->DATA."
";
return $part;
}

function ATTACHER_MSG($msg)
{
$part["type"]=TEXT;
$part["subtype"]="plain";
$part["description"]="message mode texte";
$part["contents.data"]="$msg\n\n\n\t";
return $part;
}
function ENVELLOPE_MAIL($from,$to,$cc,$sujet="")
{
$envelope["from"]=$from;
$envelope["to"]=$to;
$envelope["cc"]=$cc;

if($sujet != "")
$envelope["subject"]=$sujet;

return $envelope;
}

function ENETE_MAIL()
{
$part["type"]=TYPEMULTIPART;
$part["subtype"]="mixed";
return $part;
}
}
?>


trionon

At least the following items in $part[] hashes are parsed:
type
encoding
charset
subtype
id
description
disposition.type
disposition
contents.data
lines
bytes
md5
Note: $part['disposition'] is a hash:
$part['disposition.type'] = 'attachment';
$part['disposition'] = array ('filename'=>'file.txt');
This will transform in such part header:
Content-disposition: attachment; filename="file.txt"


vlad

... and the headers (elements of the envelope) are case-sensitive and are listed below:
remail
return_path
date
from
reply_to
in_reply_to
subject
to
cc
bcc
message_id
custom_headers
If you can't find a header you need in this list, you can use 'custom_headers'. It is just an array of lines to be appended to the header without any formatting, like this:
$envelope["custom_headers"] = Array("User-Agent: My Mail Client", "My-Header: My Value");
TODO: This should really migrate from a note to documentation


Change Language


Follow Navioo On Twitter
imap_8bit
imap_alerts
imap_append
imap_base64
imap_binary
imap_body
imap_bodystruct
imap_check
imap_clearflag_full
imap_close
imap_createmailbox
imap_delete
imap_deletemailbox
imap_errors
imap_expunge
imap_fetch_overview
imap_fetchbody
imap_fetchheader
imap_fetchstructure
imap_get_quota
imap_get_quotaroot
imap_getacl
imap_getmailboxes
imap_getsubscribed
imap_header
imap_headerinfo
imap_headers
imap_last_error
imap_list
imap_listmailbox
imap_listscan
imap_listsubscribed
imap_lsub
imap_mail_compose
imap_mail_copy
imap_mail_move
imap_mail
imap_mailboxmsginfo
imap_mime_header_decode
imap_msgno
imap_num_msg
imap_num_recent
imap_open
imap_ping
imap_qprint
imap_renamemailbox
imap_reopen
imap_rfc822_parse_adrlist
imap_rfc822_parse_headers
imap_rfc822_write_address
imap_savebody
imap_scanmailbox
imap_search
imap_set_quota
imap_setacl
imap_setflag_full
imap_sort
imap_status
imap_subscribe
imap_thread
imap_timeout
imap_uid
imap_undelete
imap_unsubscribe
imap_utf7_decode
imap_utf7_encode
imap_utf8
eXTReMe Tracker