函数名: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 = "recipient@example.com";
$subject = "Test Email";
$message = "This is a test email.";
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: reply@example.com\r\n";
$headers .= "Cc: cc@example.com\r\n";
$headers .= "Bcc: bcc@example.com\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."。