Yes, PHP can be used to send emails from a web application or website. Here’s a simple example code snippet that demonstrates how to send an email using PHP’s built-in mail() function:
$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email sent from PHP.';
$headers = 'From: sender@example.com';
if (mail($to, $subject, $message, $headers)) {
echo 'Email sent successfully.';
} else {
echo 'Email could not be sent.';
}
In the code above, we first define the recipient email address, subject, message body, and the sender email address (headers). We then pass these variables to the mail() function, which sends the email.
Note that the mail() function requires a mail server to be properly configured on the server running the PHP code. If your server doesn’t have a mail server configured, or if you need more advanced email functionality, there are third-party libraries available that can be used instead. One such popular library is PHPMailer.