PHP - imap_mail() Function
PHP−IMAP functions helps you to access email accounts, IMAP stands for Internet Mail Access Protocol using these functions you can also work with NNTP, POP3 protocols and local mailbox access methods.
The imap_mail() function accepts three string values representing to address, subject, and message body as parameters and sends the given message.
Syntax
imap_mail ($to, $subject, $message [,$additional_headers, $cc, $bcc, $rpat]);
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
to (Mandatory) This is a string value representing the senders address. |
| 2 |
subject (Mandatory) This is a string value representing the subject of the mail. |
| 3 |
message (Mandatory) This is a string value representing the mail body. |
| 4 |
additional_headers (Optional) This is a string value representing the additional headers. |
| 5 |
cc (Optional) This is a string value representing the cc address. |
| 6 |
bcc (Optional) This is a string value representing the bcc address. |
| 7 |
rpath (Optional) This is a string value representing the return path upon the delivery failure. |
Return Values
This function returns a Boolean value which is TRUE in case of success and FALSE in case of a failure.
PHP Version
This function was first introduced in PHP Version 4 and works in all the later versions.
Example
Following example demonstrates the usage of the imap_mail() function −
<html>
<body>
<?php
$to_address = "sender@test.com";
$from_address = "from@test.com";
$subject = "Test_subject";
//Sending a mail
$res = imap_mail($to_address, $from_address, $subject);
if($res){
print("Mail sent successfully");
}else{
print("Error Occurred");
}
?>
</body>
</html>
Output
This will generate the following output −
Mail sent successfully
Example
Following is an example of the above function with the optional parameters −
<html>
<body>
<?php
$to_address = "senders_address@test.com";
$from_address = "from_address@test.com";
$subject = "Test_subject";
$cc = "cc_addressc@test.com";
$bcc = "bcc_addressc@test.com";
$rpath = "return_path";
//Sending a mail
$res = imap_mail($to_address, $from_address, $subject, $cc, $bcc, $rpath);
if($res){
print("Mail sent successfully");
} else {
print("Error Occurred");
}
?>
</body>
</html>
Output
This will generate the following output −
Mail sent successfully