Can we send mail in php without using any library
Yes, you can send mail in PHP without using any library. PHP has a built-in function called mail()
that allows you to send an email. Here is an example of how to use it:
<?php
$to = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email message.';
$headers = 'From: [email protected]'. "\r\n".
'Reply-To: [email protected]'. "\r\n".
'MIME-Version: 1.0'. "\r\n".
'Content-Type: text/plain; charset=UTF-8';
if (mail($to, $subject, $message, $headers)) {
echo 'Email sent successfully.';
} else {
echo 'Failed to send email.';
}
?>
In this example, the mail()
function takes four parameters:
$to
: The recipient's email address.$subject
: The subject of the email.$message
: The message body of the email.$headers
: The headers of the email.
The $headers
parameter is a string that contains the email headers. You can add multiple headers by separating them with \r\n
. In this example, we're setting the From
, Reply-To
, MIME-Version
, and Content-Type
headers.
The mail()
function returns true
if the email is sent successfully, and false
otherwise.
Note that the mail()
function is a low-level function that requires you to specify the email headers manually. This can be error-prone and may not work correctly in all situations. For more complex email sending tasks, it's recommended to use a library like PHPMailer or SwiftMailer that provides a higher-level interface and more features.