PHP电子邮件正常工作但实际上没有被用户接收

时间:2017-06-20 08:19:55

标签: php html phpmailer

提交表单并提供正确的信息后,将显示一条消息,通知用户该电子邮件已发送。但是由于某种原因,用户没有收到电子邮件。

我已经在网上查了但没有用。有人可以帮我解决这个问题吗?

以下是HTML表单和PHP的代码。

<form method="POST" action="contact.php">
                    <div class="row">
                        <div class="col-sm-6 form-group">
                            <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
                        </div>
                        <div class="col-sm-6 form-group">
                            <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
                        </div>
                    </div>
                    <textarea class="form-control inputStyle" id="comments" name="comments" placeholder="Comment" rows="6" required></textarea>
                    <br>
                    <div class="row">
                        <div class="col-sm-12 form-group">
                            <button class="btn btn-default pull-right" name="submit" type="submit">Send</button>
                        </div>
                    </div>                

                       <!-- Contacting Support Team -->
                       <?php

                            if(isset($_POST['submit'])){
                                $email = $_POST['email'];
                                $name = $_POST['name'];
                                $comment = $_POST['comments'];

                                if(!empty($email) && !empty($name) && !empty($comment)){

                                    require 'PHPMailer-master/PHPMailerAutoload.php';

                                    $mail = new PHPMailer;

                                    $mail->Host = 'ssl://smtp.gmail.com';                 // Specify main and backup SMTP servers
                                    $mail->Port = 465;
                                    $mail->SMTPAuth = true;                               // Enable SMTP authentication
                                    $mail->Username = '*****';                 // SMTP username
                                    $mail->Password = '*****';                           // SMTP password
                                    $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

                                    $mail->From = '*****';;
                                    $mail->addAddress($email);                            // Name is optional
                                    $mail->isHTML(true);                                  // Set email format to HTML

                                    $mail->Subject = 'Test';
                                    $mail->Body    = '$comment';
                                    $mail->AltBody = '$comment';

                                    if(!$mail->send()) {
                                        echo '<div class="alert alert-danger alert-dismissible" id="myAlert">
                                        <a href="#"class="close">&times;</a>
                                        <strong> Email not sent </strong> Something went wrong.
                                        </div>';

                                        echo 'Mailer Error: ' . $mail->ErrorInfo;
                                    } else {
                                        echo '<div class="alert alert-success alert-dismissible" id="myAlert">
                                        <a href="#"class="close">&times;</a>
                                        <strong> Email sent!</strong> We will contact you as soon as possible.
                                        </div>';
                                    }
                                }
                            }
                        ?>
                </form>     

提前致谢!

1 个答案:

答案 0 :(得分:0)

您正在设置

$mail->SMTPSecure = 'tls';

但是端口465是Gmail SSL端口。 587是TLS端口。因此,要么切换端口,要么改为使用

$mail->SMTPSecure = 'ssl';