I am trying to use Mailtrap to use PHPMailer, but I am getting an error, and am having trouble finding a solution. Most people suggest fixing this problem by using a SMTP server, but as you can see, I already am.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$phpmailer = new PHPMailer();
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.mailtrap.io';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 2525;
$phpmailer->Username = 'myusername';
$phpmailer->Password = 'mypassword';
$mail->SMTPDebug = 2;
//Recipients
$mail->setFrom('myemail', 'Mailer');
$mail->addAddress('myemail', 'Joe User');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}