skip to Main Content

code:

<?php
    include 'library.php';
    include "classes/class.phpmailer.php";
    if(isset($_POST['submit']))
    {
        $email = $_POST['email'];

        $sql = "select email from login where email='".$email."'";
        $results = mysqli_query($con,$sql);
        $fetch = mysqli_num_rows($results);
        if($fetch > 0)
        {
            echo "<p id='red'>Email already exist. Please register with different email id.</p>";
        }
        else
        {
            $query = "insert into student_login(email)values('$email')";

            $result = mysqli_query($con,$query);
            if($result==true)
            {
                $information="hello everyone";
                $mail   = new PHPMailer;
                $mail->IsSMTP(); 
                $mail->Host = 'example.com';
                $mail->Port = 25;
                $mail->SMTPAuth = true;
                $mail->Username = 'cpanel-username';
                $mail->Password = 'cpanel-password';
                $mail->AddReplyTo($email);
                $mail->SetFrom("[email protected]", $email);
                $mail->Subject = "Account Activation Link @Example";
                $mail->AddAddress($email);
                $mail->MsgHTML($information); 
                $send = $mail->Send();
                if($send)
                {
                    echo "<p id='green'>To activate your account. Please login your email and click on link.</p>";
                }
                else
                {
                    echo "<p id='red'>Your message not sent.</p>";
                }
            }
            else
            {
                echo "<p id='red'>Error!</p>";
            }
        }
    }
?>

In this code I am using smtp mail function to sent an email quickly. But here what happen when I click on submit button. It show me successfull message but can’t receive an email. I do’t know where am I doing wrong. How can I fix this issue ?Please help me.

Thank You

2

Answers


  1. I think that two things will help you in diagnosing such problems:

    1. Use try-catch syntax. If something is wrong then you can catch this in block
    2. use SMTP Debug for phpmailer

    this is example how you can use mailer:

    <?php
    
    require('./vendor/autoload.php');
    
    use PHPMailerPHPMailerPHPMailer;
    
    class Mailer {
    
        public $phpmailer;
    
        public function __construct($addresses)
        {
    
            $this->phpmailer = new PHPMailer(true);
            $this->phpmailer->SMTPDebug = 3; // here you can debug
            $this->phpmailer->isSMTP();
            $this->phpmailer->Host = 'SMTP.host.example.blabla';
            $this->phpmailer->SMTPAuth = true;
            $this->phpmailer->Port = 587;
            $this->phpmailer->Username = 'username';
            $this->phpmailer->Password = 'password';
            $this->phpmailer->SetFrom('username', 'subject');
            $this->phpmailer->CharSet = "UTF-8";
            foreach ($addresses as $address) {
                $this->phpmailer->AddAddress($address);
            }
        }
    
        public function send($messageTemplate) {
    
            try {
                $this->phpmailer->MsgHTML($messageTemplate);
                $this->phpmailer->Subject = 'subject';
                $this->phpmailer->Send();
                return true;
            } catch (phpmailerException $e) {
                echo $e->errorMessage(); //Pretty error messages from PHPMailer
            } catch (Exception $e) {
                echo $e->getMessage(); //Boring error messages from anything else!
            }
        }
    
    
    }
    

    and use it:

    $mail = new Mailer(array('[email protected]'));
    if ($mail->send('some message')) {
       echo "mail send";
    }
    
    Login or Signup to reply.
  2. omkara

    A good idea is take test by parts, so:

    First of all, ensure that there are no problems with sending emails, and then check your code!

    1 – Can You send a e-mail using the web server (whitout use php)?

    2 – Check if mail() function is enabled on your server, here’s how to do it.

    3 – After try run your code check the php logs in the server.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search