SMTP电子邮件无法在PHP中运行

时间:2018-04-28 06:34:49

标签: php smtp

代码:

<?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>";
            }
        }
    }
?>

在此代码中,我使用smtp mail函数快速发送电子邮件。但是,当我点击提交按钮时会发生什么。它向我显示成功的消息但无法接收电子邮件。我不知道我在哪里做错了。我该如何解决这个问题?请帮助我。

谢谢

2 个答案:

答案 0 :(得分:0)

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

  1. 使用try-catch语法。如果出了什么问题,您可以在块中抓住它
  2. 将SMTP调试用于phpmailer

这是如何使用邮件程序的示例:

<?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";
}

答案 1 :(得分:0)

omkara

一个好主意是分部进行测试,所以:

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

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

2-检查服务器here's how to do it上是否已启用 mail()功能

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