imap_mail_compose
(PHP 4, PHP 5)
imap_mail_compose — Create a MIME message based on given envelope and body sections
Description
$envelope
, array $body
)
Create a MIME message based on the given envelope
and body
sections.
Parameters
-
envelope
-
An associative array of headers fields. Valid keys are: "remail", "return_path", "date", "from", "reply_to", "in_reply_to", "subject", "to", "cc", "bcc", "message_id" and "custom_headers" (which contains associative array of other headers).
-
body
-
An indexed array of bodies
A body is an associative array which can consist of the following keys: "type", "encoding", "charset", "type.parameters", "subtype", "id", "description", "disposition.type", "disposition", "contents.data", "lines", "bytes" and "md5".
Return Values
Returns the MIME message.
Examples
Example #1 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));
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с почтой
- IMAP, POP3 и NNTP
- imap_8bit
- imap_alerts
- imap_append
- imap_base64
- imap_binary
- imap_body
- imap_bodystruct
- imap_check
- imap_clearflag_full
- imap_close
- imap_create
- imap_createmailbox
- imap_delete
- imap_deletemailbox
- imap_errors
- imap_expunge
- imap_fetch_overview
- imap_fetchbody
- imap_fetchheader
- imap_fetchmime
- imap_fetchstructure
- imap_fetchtext
- imap_gc
- 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_rename
- imap_renamemailbox
- imap_reopen
- imap_rfc822_parse_adrlist
- imap_rfc822_parse_headers
- imap_rfc822_write_address
- imap_savebody
- imap_scan
- 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
Коментарии
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 =)
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.
It is a good idea to set the date header:
$envelope['date']=date('r');
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;
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"
]
];
?>