send email in php

To send an email in PHP, you can use the mail() function. Here's how you can do it:

main.php
<?php
$to = "recipient@example.com";
$subject = "Hello";
$message = "This is a test email.";

$headers = "From: sender@example.com";
// Additional headers that you may want to add:
// $headers .= "Reply-To: another@example.com";
// $headers .= "CC: copy@example.com";
// $headers .= "BCC: blindcopy@example.com";

if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully.";
} else {
    echo "Failed to send email.";
}
?>
446 chars
18 lines

In the code above, you provide the recipient's email address in the $to variable, the subject of the email in the $subject variable, and the actual message in the $message variable.

You can also add additional headers to the $headers variable, such as the "From" email address, "Reply-To" email address, and carbon copy (CC) or blind carbon copy (BCC) addresses.

The mail() function then sends the email. It returns true if the email is sent successfully and false otherwise.

Note that the mail() function relies on the server's configuration for sending emails, so make sure your server is properly configured to send emails. Additionally, this may not work on all hosting environments, as some have restrictions on sending emails.

related categories

gistlibby LogSnag