SMTP电子邮件在PHP中不起作用

SMTP电子邮件在PHP中不起作用,php,smtp,Php,Smtp,代码: 我认为有两件事可以帮助您诊断此类问题: 使用try-catch语法。如果有什么地方出了问题,你可以在街区里找到它 对phpmailer使用SMTP调试 以下是如何使用mailer的示例: <?php include 'library.php'; include "classes/class.phpmailer.php"; if(isset($_POST['submit'])) { $email = $_POST['email'];

代码:


我认为有两件事可以帮助您诊断此类问题:

  • 使用try-catch语法。如果有什么地方出了问题,你可以在街区里找到它
  • 对phpmailer使用SMTP调试
  • 以下是如何使用mailer的示例:

    <?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("info@example.com", $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>";
                }
            }
        }
    ?>
    
    乌姆卡拉

    一个好主意是按部件进行测试,因此:

    首先,确保发送电子邮件没有问题,然后检查代码

    1-您可以使用web服务器发送电子邮件吗(不使用php)

    2-检查服务器上是否启用了mail()函数

    3-试运行代码后,检查服务器中的php日志

    <?php
    
    require('./vendor/autoload.php');
    
    use PHPMailer\PHPMailer\PHPMailer;
    
    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!
            }
        }
    
    
    }
    
    $mail = new Mailer(array('test@test.com'));
    if ($mail->send('some message')) {
       echo "mail send";
    }