Send email php script

Here is a basic PHP script that sends an email using the mail() function:

<?php

// Define the email settings
$to = '[email protected]'; // Replace with the recipient's email address
$subject = 'Test Email'; // Replace with the subject of the email
$message = 'This is a test email sent using PHP!'; // Replace with the email body
$headers = 'From: [email protected]'. "\r\n". 'Reply-To: [email protected]'. "\r\n". 'X-Mailer: PHP/'. phpversion();

// Send the email
if (mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email!';
}

?>

This script defines the email settings, including the recipient's email address, subject, and body. It then uses the mail() function to send the email. The mail() function takes four arguments: the recipient's email address, the subject, the email body, and the headers.

You can customize the script by adding more features, such as:

Here is an example of how you can add attachments to the email:

<?php

// Define the email settings
$to = '[email protected]'; // Replace with the recipient's email address
$subject = 'Test Email'; // Replace with the subject of the email
$message = 'This is a test email sent using PHP!'; // Replace with the email body
$headers = 'From: [email protected]'. "\r\n". 'Reply-To: [email protected]'. "\r\n". 'X-Mailer: PHP/'. phpversion();

// Define the attachment
$attachment = 'path/to/attachment.txt'; // Replace with the path to the attachment

// Send the email
if (mail($to, $subject, $message, $headers, $attachment)) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email!';
}

?>

This script adds an attachment to the email by passing the path to the attachment as the fifth argument to the mail() function.

You can also use a PHP library such as PHPMailer to send emails. PHPMailer is a popular and widely-used library that provides a more robust and flexible way of sending emails. Here is an example of how you can use PHPMailer to send an email:

<?php

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

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';

$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!';

$mail->addAttachment('path/to/attachment.txt');

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

?>

This script uses the PHPMailer library to send an email. It defines the email settings, including the recipient's email address, subject, and body, and adds an attachment to the email. The script then sends the email using the send() method.