To send an email with PHP, you can use the built-in mail
function. Here is an example of how you can use it:
< ?
$to = 'recipient@example.com';
$subject = 'Email Subject';
$message = 'Email message';
$headers = 'From: sender@example.com' . "\r\n" .
'Reply-To: sender@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Email was sent successfully!';
} else {
echo 'There was an error sending the email.';
}
?>
The $to
variable specifies the recipient’s email address. The $subject
variable specifies the subject of the email. The $message
variable specifies the message body of the email. The $headers
variable specifies additional headers, such as the sender’s email address and the email client used to send the email.
Note that the mail
function requires a properly configured mail server to be able to send emails. If you are running PHP on a web server, you will need to make sure that the server is configured to send emails.