查詢

imap_mail()函式—用法及示例

「 傳送一封電子郵件 」


函式名:imap_mail()

適用版本:PHP 4、PHP 5、PHP 7

用法:該函式用於傳送一封電子郵件。它使用指定的郵件伺服器和配置資訊來傳送郵件。

語法:

bool imap_mail ( string $to , string $subject , string $message [, string $additional_headers = NULL [, string $cc = NULL [, string $bcc = NULL [, string $rpath = NULL ]]]] )

引數:

  • $to: 必需,接收郵件的地址,可以是單個地址或多個地址,多個地址使用逗號分隔。
  • $subject: 必需,郵件的主題。
  • $message: 必需,郵件的內容。
  • $additional_headers: 可選,附加的郵件頭部資訊,例如發件人、回覆地址等。預設為NULL。
  • $cc: 可選,抄送地址,可以是單個地址或多個地址,多個地址使用逗號分隔。預設為NULL。
  • $bcc: 可選,密送地址,可以是單個地址或多個地址,多個地址使用逗號分隔。預設為NULL。
  • $rpath: 可選,返回路徑,指定郵件的返回路徑。預設為NULL。

返回值:成功傳送郵件返回true,傳送失敗返回false。

示例:

$to = "[email protected]";
$subject = "Test Email";
$message = "This is a test email.";

$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Cc: [email protected]\r\n";
$headers .= "Bcc: [email protected]\r\n";

if (imap_mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully.";
} else {
    echo "Failed to send email.";
}

以上示例中,我們定義了收件人的地址($to),郵件的主題($subject),郵件的內容($message),以及附加的郵件頭部資訊($headers)。然後呼叫imap_mail()函式來傳送郵件。如果郵件傳送成功,輸出"Email sent successfully.",否則輸出"Failed to send email."。

補充糾錯
上一個函式: imap_mailboxmsginfo()函式
下一個函式: imap_lsub()函式
熱門PHP函式
分享連結