PHPMailer不会发送电子邮件

时间:2015-08-04 18:56:14

标签: php email

我是游戏开发人员,因此我没有任何PHP /网络/后端Web开发知识。我整个星期都没遇到过这个问题,网上没有运气。在PHPMailer之前,我尝试在PHP中使用mail()函数,但我对所有.ini文件感到困惑。我在这个网站上找到了PHPMailer的答案之一,我已经进一步了解。我完全按照本教程https://www.youtube.com/watch?v=FtWD_ZH9lnE&authuser=0,但电子邮件不会发送和调试不打印任何东西。我的代码总是返回false并显示我的"无法发送电子邮件"信息。

<?php session_start();

require_once 'PHPMailer/PHPMailerAutoLoad.php';
require_once 'PHPMailer/class.phpmailer.php';

$errors=[];



if(isset($_POST['name'], $_POST['twitterHandle'], $_POST['email'], $_POST['description'])) {

    $fields = [

        'name' => $_POST['name'],
        'twitterHandle' => $_POST['twitterHandle'],
        'email' => $_POST['email'],
        'description' => $_POST['description']

        ];

    foreach($fields as $field => $data){

        if(empty($data)){

            $errors[] = 'The ' . $field . ' field is required';

        }

    }

    //mail connection if all fields are ! empty

    if(empty($errors)){

        $mail = new PHPMailer;

        $mail->isSMTP();
        $mail->SMTPAuth = true;

        $mail->SMTPDebug = 1;

        $mail->Host = 'smtp.gmail.com';
        $mail->Username = '@gmail.com';
        $mail->Password = 'pass';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 25;

        $mail->isHTML;

        $mail->Subject = 'subject';
        $mail->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['description'] . '</p>';

        $mail->FromName = 'Contact';

        $mail->addReplyTo($fields['email'], $fields['name']);

        $mail->addAddress('@gmail.com', 'name');

        //send email
        if($mail->send()){

            header('Location: index.php');
            die();

        }else{

        //cannot send
            $errors[] = "Sorry, but I could not process your submission. Please try again.";

        }



    }

} else{

$errors[] = 'Something went wrong';

}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: index.php');


?>

我在端口465和25上尝试过SSL,在587上也尝试了TLS。我在网络上转发了465和25但587不会打开。我的Gmail上也启用了IMAP。

1 个答案:

答案 0 :(得分:0)

1)。继续使用tls

2)。很可能您已启用2Factor身份验证,您需要转到您的Gmail设置信息中心并允许您的应用通过Google进行身份验证。我也有同样的问题,只有在谷歌修复此设置后才能解决。

另外,请看一下这个答案: - send email using Gmail SMTP server through PHP Mailer