PHPmailer脚本运行没有问题,但不发送任何内容

时间:2019-04-18 10:29:40

标签: php phpmailer

我有一个非常简单的PHP代码,用于发送电子邮件。该页面正在加载,没有任何错误,但似乎没有任何作用。 另外,我是PHP的新手,我真的不知道如何调试这些东西。 我将非常感谢您的帮助(:谢谢!

<?php
require_once('PHPMailer/PHPMailerAutoload.php');

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure ='ssl';
$mail->Host = 'smtp.gmail.com';
$mail->port = '456';
$mail->isHtml();
$mail->Username = 'lagofbot@gmail.com';
$mail->Password = 'lago9876543s';
$mail->Subject = 'Hello';
$mail->Body = "test";
$mail->From = 'no-replay';
$mail->FromName = 'no-replay';
$mail->AddAddress('mrxvr123@gmail.com');
$mail->send();
?>

2 个答案:

答案 0 :(得分:0)

这样更改代码,看看它是否会显示任何错误

$mail = new PHPMailer();
try {
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure ='ssl';
$mail->Host = 'smtp.gmail.com';
$mail->port = '456';
$mail->isHtml();
$mail->Username = 'lagofbot@gmail.com';
$mail->Password = 'lagofbot258258#258258';
$mail->Subject = 'Hello';
$mail->Body = "test";
$mail->From = 'no-replay';
$mail->FromName = 'no-replay';
$mail->AddAddress('mrxvr123@gmail.com');
$mail->send();
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

答案 1 :(得分:0)

尝试一下

require __DIR__.'../phpmailer/src/PHPMailer.php'; //configure according your files
require __DIR__."./phpmailer/src/Exception.php";
require __DIR__."../phpmailer/src/SMTP.php";


$mail = new \PHPMailer\PHPMailer\PHPMailer(); // create a new object
    $mail->isSMTP(); // enable SMTP
    $mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->Username = ""; //GMAIL ACCOUNT EMAIL ID
    $mail->Password = ""; // GMAIL ACCOUNT PASSWORD
    $mail->SetFrom("",''); // FROM THIS MAIL ID & SET AS DEFINE IN SECOND PARAMETER
    $mail->addAddress($email); // `TO` FIELD IN MAIL
    $mail->IsHTML(true);


    if (!$mail->Send()) {
        return true;
    } else {
        return false;
    }

如果使用gmail,请不要忘记启用安全性较低的应用程序。

希望这会有所帮助:)