Php send mail function

The classic PHP mail function!

In PHP, you can send an email using the mail() function, which is a built-in function that sends an email message to one or more recipients. Here's a basic example:

<?php
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using PHP';
$headers = 'From: [email protected]'. "\r\n".
    'Reply-To: [email protected]'. "\r\n".
    'Content-Type: text/plain; charset=UTF-8';

mail($to, $subject, $message, $headers);
?>

In this example:

The mail() function takes four arguments:

  1. $to: The email address of the recipient.
  2. $subject: The subject line of the email.
  3. $message: The body of the email.
  4. $headers: An array of headers that specify the sender, reply-to, and content type of the email.

You can also use the mail() function with additional parameters, such as:

Here's an example that includes additional headers and an attachment:

<?php
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using PHP';
$headers = 'From: [email protected]'. "\r\n".
    'Reply-To: [email protected]'. "\r\n".
    'Content-Type: text/plain; charset=UTF-8';
$additional_headers = array(
    'X-Mailer: PHP/'. phpversion(),
    'MIME-Version: 1.0'
);
$attachment = 'path/to/file.txt';

mail($to, $subject, $message, $headers, $additional_headers, $attachment);
?>

Note that the mail() function is a simple function that sends an email using the PHP mail() function, which is a wrapper around the system's mail function. This means that the email may not be sent immediately, and it may not be delivered to the recipient's inbox.

If you need more advanced features, such as sending emails with attachments, HTML content, or tracking, you may want to consider using a third-party library or service, such as PHPMailer or SendGrid.