$email_to = "user@example.com"; $message = "This is a test email message."; $subject = "Test Email"; $headers = "From: admin@example.com\r\n"; $headers .= "Reply-To: admin@example.com\r\n"; $mail_sent = mail($email_to, $subject, $message, $headers); if ($mail_sent) { echo "Email sent successfully to $email_to"; } else { echo "Email sending failed."; }
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $email_to = "user@example.com"; $message = "This is a test email message."; $subject = "Test Email"; $mail = new PHPMailer(true); try { $mail->setFrom('admin@example.com', 'Admin'); $mail->addAddress($email_to); $mail->Subject = $subject; $mail->Body = $message; $mail->send(); echo "Email sent successfully to $email_to"; } catch (Exception $e) { echo "Email sending failed. Error: {$mail->ErrorInfo}"; }In this example, the PHPMailer package library is used to send an email. The addAddress method is used to set the recipient email address.