$to = "recipient@example.com"; $subject = "New Email from My Website!"; $message = "This is a test message."; $headers = "From: sender@example.com"; mail($to, $subject, $message, $headers);
$mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->setFrom('sender@example.com', 'Sender Name'); $mail->addAddress('recipient@example.com', 'Recipient Name'); $mail->Subject = 'New Email from My Website!'; $mail->Body = 'This is a test message.'; if (!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }In this example, we use PHPMailer library to send an email to the recipient with the subject line "New Email from My Website!". Here we also set the body of the message using the Body property. If the email is not sent due to any errors, we display the error message using the ErrorInfo property.