To send an email from PHP, you can use the mail
function. This function sends an email message to a specified recipient.
Here is an example of how to use the mail
function to send an email:
<?php $to = 'recipient@example.com'; $subject = 'Subject of the email'; $message = 'Body of the email'; $headers = 'From: sender@example.com' . "\r\n" . 'Reply-To: sender@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers);
This will send an email with the specified subject and message to the recipient's email address. The $headers
variable specifies the sender's email address and other optional headers, such as the Reply-To
header and the X-Mailer
header.
It's important to note that the mail
function relies on a mail server to deliver the email. You will need to configure your PHP installation to use a mail server or a third-party email service to send emails. Consult the PHP documentation and your server's documentation for more information.
You can also use other functions and libraries, such as mail
, mail
, and Swift Mailer
, to send emails from PHP. These libraries provide more advanced features and options for sending emails, such as support for attachments and HTML formatting. Consult the PHP documentation and online resources for more information.