简体   繁体   English

PHPmailer无法在使用带有cookiePHP的gmail smtp的实时服务器上工作

[英]PHPmailer is not working on live server using gmail smtp with cakePHP 3

I use cakePHP 3, when I moved my app to server, it stopped sending emails, I am using gmail smtp server. 我使用cakePHP 3,将我的应用程序移动到服务器时,它停止发送电子邮件,我正在使用gmail smtp服务器。 I tried with SSL connect to smtp.gmail.com on port 465, still not working. 我尝试使用SSL在端口465上连接到smtp.gmail.com,但仍无法正常工作。 Also variable $mail->SMTPDebug = on; 同样变量$mail->SMTPDebug = on; is making some troubles. 正在制造一些麻烦。

My send function is this: 我的发送功能是这样的:

  public function send($to, $subject, $message) {
    $sender = "me@me.com"; // this will be overwritten by GMail

    $header = "X-Mailer: PHP/".phpversion() . "Return-Path: $sender";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/html; charset=UTF-8\r\n";

    $mail = new \PHPMailer();

    $mail->IsSMTP();
    $mail->Host = "aspmx.l.google.com";
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Port = 25;
    $mail->SMTPDebug  = on; // turn it off in production
    $mail->Username   = "........";
    $mail->Password   = "........";

    $mail->From = $sender;
    $mail->FromName = "From Me";

    $mail->AddAddress($to);

    $mail->IsHTML(true);
    $mail->CreateHeader($header);

    $mail->Subject = $subject;
    $mail->Body    = nl2br($message);
    $mail->AltBody = nl2br($message);

    // return an array with two keys: error & message
    if(!$mail->Send()) {
        return array('error' => true, 'message' => 'Mailer Error: ' . $mail->ErrorInfo);
    } else {
        return array('error' => false, 'message' =>  "Message sent!");
    }
}
}

my error is this: 我的错误是这样的:

Use of undefined constant on - assumed 'on' [APP/Controller/Component/EmailComponent.php, line 30]

Undefined variable: errno [ROOT/vendor/phpmailer/class.smtp.php, line 182]

Undefined variable: errstr [ROOT/vendor/phpmailer/class.smtp.php, line 183]

Is is possible that webhosting on which I have page is blocking it? 我所在页面的虚拟主机是否有可能阻止了它?

Thank you 谢谢

Edit your lines like below: 修改您的行,如下所示:

$mail->Host = "smtp.gmail.com";
$mail->Port = 587;

It should work now. 现在应该可以工作了。

In line $mail->SMTPDebug = on; // turn it off in production 在行$mail->SMTPDebug = on; // turn it off in production $mail->SMTPDebug = on; // turn it off in production

on must be in quotes: 'on' on必须用引号引起来: 'on'

UPDATE: 更新:

In the class.phpmailer.php (from line nr. 315) there is description of the SMTPDebug values: it is integer from 0 to 4. class.phpmailer.php (从第315行开始)中,有SMTPDebug值的描述:它是0到4之间的整数。

 // 0 - No output
 // 1 - Commands
 // 2 - Data and commands
 // 3 - As 2 plus connection status
 // 4 - Low-level data output

There's some misunderstanding of basic PHP syntax happening here. 这里发生了对基本PHP语法的误解。 Have you tried reading the manual ? 您是否尝试过阅读手册

In this case, on (as well as being just plain invalid PHP) is not a valid value for SMTPDebug , even if you make it a string. 在这种情况下, on (以及仅是无效的PHP)对于SMTPDebug来说不是有效值,即使您将其设为字符串也是如此。 Try setting it to 2 , as the docs suggest . 尝试将其设置为2 ,如文档所建议

Your code is something of a mess - you're trying to break bits of PHPMailer (don't try to set headers yourself like that, let PHPMailer do it properly); 您的代码有些混乱-您正试图破坏PHPMailer的某些位(不要像这样自己设置标头,而是让PHPMailer正确执行); you've based your code on an obsolete example, and are using an old version of PHPMailer; 您已经基于过时的示例编写了代码,并且使用的是旧版本的PHPMailer; You're talking to the wrong address to send out through gmail - it should be smtp.gmail.com ; 您说的是通过gmail发送的错误地址-它应该是smtp.gmail.com ; You're trying to use SSL on a non-SSL port; 您正在尝试在非SSL端口上使用SSL; You're putting HTML tags into the plain text version. 您正在将HTML标签放入纯文本版本中。

There's just too much wrong here to be worth fixing, so I suggest you start again with a clean, working example , and update to the latest PHPMailer . 这里有太多错误值得修复,所以我建议您从一个干净的示例开始 ,并更新到最新的PHPMailer

As far as problems connecting to gmail go, this is also very well covered by the troubleshooting guide and by many other questions on SO. 至于连接到gmail的问题,《故障排除指南》以及有关SO的许多其他问题也对此进行了很好的介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM