我在PHP7 Codeanywhere容器中配置Swiftmailer . 我没有使用任何框架,因此我的基础设施只是:Linux机器,PHP7,Composer和带有库存配置的Swiftmailer .

我没有运行自己的SMTP服务器,因此决定使用Google的SMTP . 运输是Swiftmailer的运输 .

来自Swiftmailer's docs的标准化配置 . 所以我的代码是:

require_once '/box/workspace/vendor/autoload.php';
    // Create the Transport
    $transport = (new Swift_SmtpTransport('ssl://smtp.gmail.com', 465))
      ->setUsername('mymail@gmail.com')
      ->setPassword('mypassword')
    ;

    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);

    // Create a message
    $message = (new Swift_Message('Wonderful Subject'))
      ->setFrom(['mymailgmail.com' => 'John Doe'])
      ->setTo(['hismail@gmail.com', 'hermail@yahoo.com' => 'My reciever name'])
      ->setBody('Here is the message itself')
      ;

    // Send the message
    $result = $mailer->send($message);

但是无法发送电子邮件 . 跟踪是:

box@box-codeanywhere workspace]$ php a/mail.php

       > PHP Fatal error:  Uncaught Swift_TransportException: Expected response
       > code 250 but got code "535", with message "535-5.7.8 Username and
       > Password not accepted. Learn more at 535 5.7.8 
       > https://support.google.com/mail/?p=BadCredentials
       > j23-v6sm3304186oiy.22 - gsmtp " in
       > 
      /box/workspace/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:419
    > Stack trace:
    > #0 /box/workspace/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(317):
    > Swift_Transport_AbstractSmtpTransport->assertResponseCode('535-5.7.8
    > Usern...', Array)
    > #1 /box/workspace/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(272):
    > Swift_Transport_AbstractSmtpTransport->executeCommand('RSET\r\n',
    > Array, Array)
    > #2 /box/workspace/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/XOAuth2Authenticator.php(55):
    > Swift_Transport_EsmtpTransport->executeCommand('RSET\r\n', Array)
    > #3 /box/workspace/vendor/swiftmailer/swiftmailer/lib/cla in /box/workspace/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php
    > on line 419 [box@box-codeanywhere workspace]$

我想这个日志说关于登录或密码不正确,或者由于某些其他原因Gmail不接受它们 .

但登录/密码是正确的 .

谷歌的跟踪链接说明了两件事,在很多论坛上讨论过:

我做了他们两个,但它没有帮助我 .

我花了几个小时搜索论坛和Stackoverflow . 许多答案和教程(例如this)已经过时并且是旧版本的Swiftmailer,所以它们在2018年无效,因为程序的开发人员制作了大量的changes . 许多答案建议从Google执行我的1-2步骤 . 其他人建议改变我的

'ssl://smtp.gmail.com', 465) to 567

(例如,在这个question中)这对我来说太不幸了 .

请帮我解决这个问题!也许Symphony Framework是必需的?如果是这样,请原谅我的愚蠢 . 或者可能需要对Swiftmailer的配置进行一些更改?

Update: 添加了防火墙规则,接受TCP 465上的连接:

iptables -A INPUT -p tcp -m tcp --dport 465 -j ACCEPT

检查我的机器的465 TCP端口是否已打开:

[box @ box-codeanywhere workspace] $ netstat -punta(没有信息可以读取“-p”:geteuid()= 500但你应该是root . )活动的Internet连接(服务器和已 Build )Proto Recv-Q发送-Q本地地址外部地址状态PID /程序名称tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:25 0.0 .0.0:* LISTEN - tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN - tcp 0 0 my_ip:51121 my_2_ip:465 TIME_WAIT - tcp 0 0 my_s_ip:22 my_con_ip:43664 ESTABLISHED - tcp 0 0 ::: 22 ::: * LISTEN -

Update 2: 尝试了另一台SMTP服务器 - 一切正常 . 所以问题与我认为的Gmail有关..