use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Create a new PHPMailer instance
$mail = new PHPMailer;
// Tell PHPMailer to use SMTP
$mail->isSMTP();
// Set the hostname of the mail server
$mail->Host = 'smtp.example.com';
// Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
// Enable SMTP authentication
$mail->SMTPAuth = true;
// Provide username and password
$mail->Username = 'yourusername';
$mail->Password = 'yourpassword';
// Set the subject and recipient
$mail->Subject = 'Test message';
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('to@example.com', 'Recipient Name');
// Set the body of the message
$mail->Body = 'Hello! This is a test message.';
// Send the message
if (!$mail->send()) {
echo 'Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
请注意,您需要将上面的示例代码中的SMTP主机名、端口、用户名和密码替换为您自己的值。