0

Here is my code

$subject  = 'Verification code';
   $message  = '
      <html>
       <head>
         <title>Here is your Verification Code</title>
       </head>
       <body>
         <p>Please use this verification code this is a one time verification '. $fourRandomDigit.'</p>
       </body>
      </html>
      ';
     //Server settings
        $mail->isSMTP();
        $mail->Host = 'smtp.test.io';
        $mail->SMTPAuth = true;
        $mail->Port = 421;
        $mail->Username = 'name';
        $mail->Password = 'pass';                            
        
       //Recipients
        $mail->setFrom('from@sof.com', 'name');
        $mail->addAddress('to@sof.com');        //Name is optional
        $mail->addReplyTo('from@sof.com', 'name');
        
    //Content
        $mail->isHTML(true);         //Set email format to HTML
        $mail->Subject =  $subject;
        $mail->Body    = $message;
        
       if (!$mail->Send()) {
          echo "Error while sending Email.";
       } else {
          echo "Email sent successfully";
       }

My smtp server shows that the mail is sent but the mails are not being received, also the "Email sent successfully" is being printed,so i am a bit confused where am i wrong

Thanks in advance

Mukash Wasti
  • 135
  • 1
  • 16
  • 3
    Sending an email is the easy part, getting it to be accepted by the receiving mail server is an art form. See: [SPF, DKIM, DMARC: The 3 Pillars of Email Authentication](https://www.higherlogic.com/blog/spf-dkim-dmarc-email-authentication/). (link is just meant as an introduction to this topic) – KIKO Software Feb 04 '22 at 08:22
  • https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail has lots of hints as to what possible reasons might be. – CBroe Feb 04 '22 at 08:23
  • Make sure to set a valid email for return path and to check the spam folder (Gmail routinely classifies as spam all the failed delivery reports I get). – Álvaro González Feb 04 '22 at 08:38

1 Answers1

0

Your e-mail is probably considred as SPAM.

To help you resolve each little detail that could cause the issue, I recommend using mail-tester.com (or any other equivalent tool)

E-telier
  • 766
  • 5
  • 15
  • It said my email is almost perfect, i guess i am doing something wrong in the code. – Mukash Wasti Feb 04 '22 at 08:35
  • @MukashWasti If you got a result from mail-tester it means they actually received your e-mail – E-telier Feb 04 '22 at 08:40
  • at fist i composed a mail and send it, then i got the result, but now i tried sending it via my code, and i got no response from them. – Mukash Wasti Feb 04 '22 at 08:47
  • @MukashWasti Yes, you need to send them the e-mail the way you want to test it, of course. If they didn't receive your e-mail sent by PHPMailer, then the problem comes from your server. Do you have the **sendmail** extension installed? Could you try sending an e-mail throught the PHP `mail()` function? – E-telier Feb 04 '22 at 09:27
  • using mail() works fine, but it sends the mail to spam, can you guide me how to set up smtp in mail()? – Mukash Wasti Feb 04 '22 at 09:59
  • If you're using mail(), you are, by definition, not using SMTP. The PHP mail function calls a sendmail binary through a shell, it doesn't use SMTP from PHP's perspective. – Synchro Feb 04 '22 at 10:11
  • @MukashWasti As @Synchro said the `mail()` function can't use SMTP. Your problem seems to come from your server configuration or SMTP address. Are you sure **smtp.test.io** is allowed to send e-mails from **sof.com** throught the **421 port** ? – E-telier Feb 04 '22 at 11:09
  • They are not the real credentials, i changed them while posting the question, as i mentioned in the question my SMTP dashboard shows that the mail is sent but mails are not showing in the gmail etc – Mukash Wasti Feb 04 '22 at 11:30
  • @MukashWasti Understood. Yet if mail-test.com doesn't receive your e-mail I really doubt it was properly sent. Maybe contact your hoster for more info than given by your dashboard. – E-telier Feb 04 '22 at 11:51