函式名: imap_append()
適用版本: PHP 4 >= 4.0.2, PHP 5, PHP 7
用法: imap_append(resource $imap_stream, string $mailbox, string $message, string $options = null, string $internal_date = null): bool
解釋:imap_append() 函式用於將郵件訊息追加到指定郵箱中。該函式可以將已經存在於郵件訊息中的頭部資訊和正文部分追加到指定郵箱。
引數:
- $imap_stream: 必需,IMAP 連線資源。
- $mailbox: 必需,目標郵箱的名稱。
- $message: 必需,要追加的郵件訊息。
- $options: 可選,附加選項。可以是 "/novalidate-cert",用於禁用 SSL 證書驗證。
- $internal_date: 可選,指定郵件的內部日期。如果沒有提供該引數,將使用當前日期和時間。
返回值:如果成功追加了郵件訊息,則返回 true。如果失敗,則返回 false。
示例:
// 連線到 IMAP 伺服器
$imap_server = "{imap.example.com:993/imap/ssl}";
$imap_user = "[email protected]";
$imap_password = "your_password";
$imap_stream = imap_open($imap_server, $imap_user, $imap_password);
// 建立一個新的郵件訊息
$message = "Subject: Test Email\r\n";
$message .= "From: [email protected]\r\n";
$message .= "To: [email protected]\r\n";
$message .= "\r\n";
$message .= "This is a test email.";
// 將郵件訊息追加到指定郵箱
$mailbox = "INBOX";
if (imap_append($imap_stream, $mailbox, $message)) {
echo "郵件訊息成功追加到郵箱!";
} else {
echo "追加郵件訊息失敗!";
}
// 關閉 IMAP 連線
imap_close($imap_stream);
注意事項:
- 在使用 imap_append() 函式之前,需要先使用 imap_open() 函式連線到 IMAP 伺服器。
- 追加的郵件訊息必須符合 RFC 822 郵件訊息格式。
- 若要禁用 SSL 證書驗證,可以在 $options 引數中新增 "/novalidate-cert"。
- 如果沒有指定 $internal_date 引數,則將使用當前日期和時間作為郵件的內部日期。
- 追加郵件訊息時,需要確保目標郵箱存在並且使用者有足夠的許可權進行追加操作。