How to send mail php

In PHP, you can send an email using the mail() function, which is a built-in function in PHP. Here is a basic example of how to use it:

<?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".
    'MIME-Version: 1.0'. "\r\n".
    'Content-Type: text/plain; charset=UTF-8';

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

This code sends an email to the recipient at [email protected] with the subject "Test email" and the message "This is a test email sent using PHP". The email is sent from the sender at [email protected].

You can also use the PHPMailer library, which is a popular and powerful email sending library for PHP. Here is an example of how to use it:

<?php
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test email';
$mail->Body = 'This is a test email sent using PHPMailer';

if (!$mail->send()) {
    echo 'Error sending email: '. $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}
?>

This code sends an email to the recipient at [email protected] with the subject "Test email" and the message "This is a test email sent using PHPMailer". The email is sent from the sender at [email protected] using the SMTP server at smtp.example.com.

You can also use other libraries such as SwiftMailer or Zend_Mail to send emails in PHP.

It's also important to note that you should always validate and sanitize the email addresses and other data before sending an email to prevent any security vulnerabilities.

Also, you can use the mail() function with additional parameters to specify the email headers and other settings, such as:

mail($to, $subject, $message, $headers, 'From: [email protected]', 'Reply-To: [email protected]');

You can also use the mail() function with the mb_send_mail() function to send emails with non-ASCII characters:

mb_send_mail($to, $subject, $message, $headers);

It's also important to note that the mail() function is not reliable and may not work in all cases, especially if the email server is not configured correctly. It's recommended to use a more robust email sending library such as PHPMailer or SwiftMailer to send emails in PHP.