I'm using my Cpanel email's SMTP credentials to send mail using the PHPMailer library. Mail is sending fine to Gmail domain but when I use any custom domain like other Cpanel emails then email is not sending to the email. Below are my codes. This code is working fine when I use any Gmail domain email but on custom domain email it sends (without any errors) but mail is not going to that email.
<?php
require_once dirname(__DIR__, 2) . '/config/config.php';
require_once dirname(__DIR__, 2) . '/vendor/phpmailer/phpmailer/src/Exception.php';
require_once dirname(__DIR__, 2) . '/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require_once dirname(__DIR__, 2) . '/vendor/phpmailer/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Mailing {
// Send Mail PHPMailer
public function sendMail($to, $replyTo, $subject, $body, $output = null, $filename = null) {
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
if ($_SERVER['HTTP_HOST'] == 'localhost') {
$mail->isSMTP();
} else {
$mail->isMail();
}
$mail->Host = SMTP_HOST; // my cpanel mail smtp host
$mail->SMTPAuth = true;
$mail->Username = SMTP_USER; // my cpanel smtp user
$mail->Password = SMTP_PASS; // my cpanel smtp password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = SMTP_PORT; // 465
$mail->setFrom(SMTP_USER, APP_NAME);
$mail->addAddress($to);
$mail->addReplyTo($replyTo);
if ($output != null) {
$mail->AddStringAttachment($output, $filename, 'base64', 'application/pdf');
}
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$result = $mail->send();
if ($result) {
return true;
} else {
return $mail->ErrorInfo;
}
} catch (Exception $e) {
return $e->getMessage();
}
}
}