PHPMailer GMail电子邮件已发送但未收到..

时间:2020-04-08 17:33:29

标签: gmail phpmailer


我已经安装了PHPMailer 6,并在MVC应用程序中使用它,如下所示,我的电子邮件显然已发送,但从未收到:

// Controller:

$this->sendmailer = new PHPMailer(true);
        //$this->sendmailer->isSMTP();
        $this->sendmailer->SMTPDebug = 2;
        $this->sendmailer->SMTPAuth = true;
        $this->sendmailer->SMTPSecure = 'tls';
        $this->sendmailer->Host = 'smtp.gmail.com';
        $this->sendmailer->Port = 587;
        $this->sendmailer->Username = 'xxxxxx@gmail.com';
        $this->sendmailer->Password = 'xxxxxxx';
        $this->sendmailer->setFrom('xxxxxxx@gmail.com', 'Test PHPMailer / GMail');

// In the inherited Controller...:


$mail=$this->sendmailer;

$subject="Mail Subject....";
$content="HTML content";

    $mail->addAddress('jmbrosselin@jmbdev.com', 'JM Brosselin');
    $mail->Subject = $subject;
    $mail->isHTML();
    $mail->msgHTML($content);

    echo '<pre>';
        echo $message;
        echo '<br/><hr><br/>';
        print_r($mail);
echo '</pre>';
    die();

注1:无论我使用什么SMTP配置(tls / 587或ssl / 465 ...),我都已注释掉了使发送崩溃的isSMTP()方法;除此之外,电子邮件输出似乎正确;
注意2:我已经以更简单的非MVC样式测试了此配置,并且它可以完美运行,例如,它已发送。

糟糕。.忘了在代码副本中添加send方法。.显然我在原始脚本中拥有它:

if (!$mail->send()) {
            $message='<br/>Mailer Error: '. $mail->ErrorInfo;
        } else {
            $message='<br/>Message sent!';
        }

嗨!
还要感谢@Dhruvadeep。
我使用的是法语Google / Admin控制台,因此菜单有些不同,但是我确实实施了“安全性较低的Apps Control”;我不做的是使用
require 'PHPMailerAutoload.php';”指令;相反,我使用Composer和以下代码:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


在我的“控制器”中...
我似乎已经读到“ require 'PHPMailerAutoload.php';”已经过时,我正在使用PHPMailer v6.1.4…
这是正确的吗?

事实上,按照配置,Gmail请求可以“随机”运行(...!),要花点时间才能将mai发送到收件人电子邮件框中,或者..not!我已经读到这可能是由于GMail考虑到我正在发送垃圾邮件..但是这是否不会被过滤/从收件人邮箱而不是从发行方(例如GMAIL…)进行过滤?
在我当前的设置下:

$this->sendmailer = new PHPMailer(true);
//$this->sendmailer->isSMTP();
$this->sendmailer->SMTPDebug = 2;

$this->sendmailer->Host = 'smtp.gmail.com';
//$this->sendmailer->Host =gethostbyname("smtp.gmail.com");
$this->sendmailer->Port = 587;
$this->sendmailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->sendmailer->SMTPAuth = true;
$this->sendmailer->Username = 'my_gmail_email_email@gmail.com';
$this->sendmailer->Password = 'my_gmail_email_pwd';
$this->sendmailer->setFrom('my_gmail_email_email', 'Test');

1 个答案:

答案 0 :(得分:0)

实际使用如下所述的最新的PhPMailer指南。 另外,通过以下

激活Google Less Secure App。

**允许安全性较低的应用访问Gmail

打开您的Google管理控制台(admin.google.com)。 点击安全>基本设置。

在“安全性较低的应用程序”下,选择“转到安全性较弱的应用程序的设置”。

在子窗口中,选择“对所有用户强制使用不太安全的应用程序访问权限”单选按钮。 ...

点击“保存”按钮。**

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('myfriend@example.net', 'My Friend');
$mail->Subject  = 'First PHPMailer Message';
$mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}```