2

I configure Swiftmailer in my PHP7 Codeanywhere container. I don't use any Frameworks, so my infrastructure is just: Linux machine, PHP7, Composer and Swiftmailer with stock configuration.

I don't run my own SMTP-server, so decided to use Google's SMTP. Transport is Swiftmailer's transport.

Used standart congiguration from Swiftmailer's docs. So my code is:


    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);

But emails can't be sent. Trace is:


    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]$

I suppose this log says about login or password are not correct, or Gmail doesn't acept them due to some other reason.

But login/password are correct.

Google's link from my trace says about 2 things, discussed on lot's of forums:

  1. Allow less secure apps: If you don't use 2-Step Verification, you might need to allow less secure apps to access your account.

  2. If the tips above didn't help, visit https://www.google.com/accounts/DisplayUnlockCaptcha and follow the steps on the page.

I did them both, but it didn't help me.

I've spent hours searching forums and Stackoverflow. Many answers and tutorials (ex. this) are outdated and desrcibe old versions of Swiftmailer, so they are not valid in 2018 because program's developers made lots of changes. Many answers suggest to do my 1-2 steps from Google. Others suggest to change my


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

(for ex. in this question) This don't help me too unfortunately.

Please help me with this problem! Maybe Symphony Framework is required? If so, excuse my stupidness. Or may be some changes in Swiftmailer's config needed?

Update: Added Firewall rule, accepting connections on TCP 465:

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

Checked if my machine's 465 TCP port is open:

[box@box-codeanywhere workspace]$ netstat -punta (No info could be read for "-p": geteuid()=500 but you should be root.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name 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: Tryed another SMTP server - everything is OK. So problem is related to Gmail I suppose..

aysee
  • 121
  • 10
  • I also tryed in this manner: ('smtp.gmail.com', 465, 'ssl') and ('smtp.gmail.com', 567, 'ssl'), didn't help – aysee Mar 29 '18 at 08:26
  • Have you try to stop 2 step verification and allow less secure app in gmail account this link used for enable less secure https://myaccount.google.com/lesssecureapps?pli=1 – Kamlesh Solanki Mar 29 '18 at 14:31
  • @Kamlesh Solanki, yes as I mensioned before, I've allowed less secure apps. Two step verification I think is not configured in my account. – aysee Mar 30 '18 at 06:41
  • Need to disable two step verification. Have you disable? – Kamlesh Solanki Mar 30 '18 at 07:44
  • @Kamlesh Solanki, checked right now, yes, disabled – aysee Mar 30 '18 at 08:09

0 Answers0