0

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();
    }
  }
}

DCodeMania
  • 1,027
  • 2
  • 7
  • 19
  • This doesn't really sound like an issue with PHP though. All this code does is sending the mail to the SMTP server (which apparently works since some receivers gets the emails). It's then the SMTP that's sending the email to the receiver. Check if you can see any mail server logs in Cpanel. Also check the receivers spam folder. – M. Eriksson Dec 01 '21 at 09:28
  • `$mail->setFrom(SMTP_USER, APP_NAME);` - is your `SMTP_USER` actually a valid email address here? – CBroe Dec 01 '21 at 09:37
  • @M.Eriksson as I mentioned mail is sending but when I use Gmail domain, not in other cpanel custom domain from the live server. And I also checked the spam folder there is no mail received there. – DCodeMania Dec 01 '21 at 10:49
  • @CBroe Yes sir SMTP_USER is a valid email address that is created in the same cpanel from where I've hosted the app. – DCodeMania Dec 01 '21 at 10:51
  • There's a difference between connecting and sending the email from PHP to the SMTP server (which is the only thing PHP does) and the SMTP server sending the emails to the receiver. You say that you don't get any errors (I assume you've debugged it by setting `$mail->SMTPDebug = 2;`?) Your question is a bit unclear. What exactly do you mean by "not in other cpanel custom domain"? Are you talking about using Google SMTP vs Other Cpanel SMTP or are you talking about sending emails to gmail-addresses vs sending emails to addresses with "custom" domains where the mail server is hosted on Cpanel? – M. Eriksson Dec 01 '21 at 13:14

0 Answers0