伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: imap_mail_compose

来源:网络转载 浏览:59次 时间:2023-06-22
imap_mail_copy » « imap_lsub PHP 手册 函数参考 邮件相关扩展 IMAP IMAP 函数

imap_mail_compose

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

imap_mail_compose — Create a MIME message based on given envelope and body sections

说明

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

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

参数

envelope

An associative array of header fields. Valid keys are: "remail", "return_path", "date", "from", "reply_to", "in_reply_to", "subject", "to", "cc", "bcc" and "message_id", which set the respective message headers to the given string. To set additional headers, the key "custom_headers" is supported, which expects an array of those headers, e.g. ["User-Agent: My Mail Client"].

bodies

An indexed array of bodies. The first body is the main body of the message; only if it has a type of TYPEMULTIPART, further bodies are processed; these bodies constitute the bodies of the parts.

Body Array Structure Key Type Description type int The MIME type. One of TYPETEXT (default), TYPEMULTIPART, TYPEMESSAGE, TYPEAPPLICATION, TYPEAUDIO, TYPEIMAGE, TYPEMODEL or TYPEOTHER. encoding int The Content-Transfer-Encoding. One of ENC7BIT (default), ENC8BIT, ENCBINARY, ENCBASE64, ENCQUOTEDPRINTABLE or ENCOTHER. charset string The charset parameter of the MIME type. type.parameters array An associative array of Content-Type parameter names and their values. subtype string The MIME subtype, e.g. 'jpeg' for TYPEIMAGE. id string The Content-ID. description string The Content-Description. disposition.type string The Content-Disposition, e.g. 'attachment'. disposition array An associative array of Content-Disposition parameter names and values. contents.data string The payload. lines int The size of the payload in lines. bytes int The size of the payload in bytes. md5 string The MD5 checksum of the payload.

返回值

Returns the MIME message as string, 或者在失败时返回 false.

范例

示例 #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));

?>
add a note

User Contributed Notes 10 notes

up down 3 Los Olvidados19 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 down 3 thomas dot hebinck at digionline dot de18 years ago It is a good idea to set the date header:
$envelope['date']=date('r');
up down 1 jvandeweghe at kipsu dot com5 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 down 2 Guido13 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;
up down 2 prices at dflytech dot com20 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 down 0 Freman14 years ago 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"
up down 0 trionon at mail dot ru20 years ago 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"
up down -1 coxsterdillon at hotmail dot com8 years ago So following on from their example, once you've composed your email with attachment or whatever, you'll probably want to send it.

since imap_mail(...) takes the message as body and re-encodes it, ignoring all the hard work you've done already.

Only real solution I found is to post it yourself:

// open connect to mail server
$socket=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if ($socket===false) {
    die('Error: socket_create, '.socket_strerror(socket_last_error()));
}

$server="myserver.com"; // say 10.0.0.1 or whatever your mail sever is
$port=25;  // again your mail server port, here generic smtp
if (socket_connect($socket,$server,$port)) {
    print "Connection successful\n";
}else {
    die('Error: socket_connect, '.socket_strerror(socket_last_error()));
}

// send it...
socket_write($socket,"HELO {$envelope["from"]}\n");
socket_write($socket,"MAIL FROM: {$envelope["from"]}\n");
socket_write($socket,"RCPT TO: {$envelope["to"]}\n");
socket_write($socket,"DATA\n");
socket_write($socket,"$message\n");
socket_write($socket,".\n");
socket_write($socket,"QUIT\n");

// done...
socket_close($socket);

Ofcourse the shell may work for you?

echo "Hello World" | mail -s "Greeting..." -a "attachSomeFile.txt" gspot@gmail.com
up down -2 hans at lintoo dot dk16 years ago 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/
up down -2 hans at lintoo dot dk16 years ago 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!
add a note

官方地址:https://www.php.net/manual/en/function.imap-mail-compose.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net