title: Unable to send mail from localhost in zend framework 2
body: <p>I am unable to send mail using localhost connection and correct code . But getting this error </p>
  
  <pre><code>Zend\Mail\Protocol\Exception\RuntimeException
  
  File:
  
  /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/AbstractProtocol.php:348
  
  Message:
  
  5.7.14 &lt;https://accounts.google.com/signin/continue?sarp=1&amp;scc=1&amp;plt=AKgnsbsl
   5.7.14 6EWOp6ZKxNQid3Q84gVgHX7gRcrLH3FRL88s1Y-aurZXgnxEziLaRJrbc2OxtCgugbO98W
   5.7.14 CMXNsqYvCG5EwG5iP9GU2N3NErFFURRUqffsIRCJW_u7_4WdRXu_Rb1mZjTITQm8VexWbn
   5.7.14 tDk2rYv_9cv7NAcQUi5eItDC9HhF0mKns_yLRj1Rsl3nz0sQmn0rtfiJkg_mF8hQelwSAZ
   5.7.14 F943KqTxTU3tWiZzO54KNZdUd3YWI&gt; Please log in via your web browser and
   5.7.14 then try again.
   5.7.14  Learn more at
   5.7.14  https://support.google.com/mail/answer/78754 tn5sm2706283pac.32 - gsmtp
  
  Stack trace:
  
  #0 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp/Auth/Login.php(83): Zend\Mail\Protocol\AbstractProtocol-&gt;_expect(235)
  #1 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp.php(193): Zend\Mail\Protocol\Smtp\Auth\Login-&gt;auth()
  #2 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(340): Zend\Mail\Protocol\Smtp-&gt;helo('localhost')
  #3 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(325): Zend\Mail\Transport\Smtp-&gt;connect()
  #4 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(336): Zend\Mail\Transport\Smtp-&gt;lazyLoadConnection()
  #5 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(205): Zend\Mail\Transport\Smtp-&gt;connect()
  #6 /var/www/html/rustagi/module/ZfcAdmin/src/ZfcAdmin/Controller/AdminController.php(185): Zend\Mail\Transport\Smtp-&gt;send(Object(Zend\Mail\Message))
  #7 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): ZfcAdmin\Controller\AdminController-&gt;SendMailAction()
  #8 [internal function]: Zend\Mvc\Controller\AbstractActionController-&gt;onDispatch(Object(Zend\Mvc\MvcEvent))
  #9 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
  #10 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager-&gt;triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
  #11 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager-&gt;trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
  #12 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController-&gt;dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
  #13 [internal function]: Zend\Mvc\DispatchListener-&gt;onDispatch(Object(Zend\Mvc\MvcEvent))
  #14 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
  #15 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager-&gt;triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
  #16 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Application.php(313): Zend\EventManager\EventManager-&gt;trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
  #17 /var/www/html/rustagi/index.php(17): Zend\Mvc\Application-&gt;run()
  #18 {main}
  </code></pre>
  
  <p>I am using this code :-</p>
  
  <pre><code>$options = new Mail\Transport\SmtpOptions(array(  
          'name' =&gt; 'localhost',  
          'host' =&gt; 'smtp.gmail.com',  
          'port'=&gt; 587,  
          'connection_class' =&gt; 'login',  
          'connection_config' =&gt; array(  
              'username' =&gt; 'someone@gmail.com',  
              'password' =&gt; '********',  
              'ssl'=&gt; 'tls',  
          ),  
  ));  
  
  $fileContents = fopen("/usr/share/pixmaps/faces/sky.jpg", 'r');
  $attachment = new Mime\Part($fileContent);
  $attachment-&gt;type = 'image/jpg';
  $attachment-&gt;disposition = Mime\Mime::DISPOSITION_ATTACHMENT;
  
  // $this-&gt;renderer = $this-&gt;getServiceLocator()-&gt;get('ViewRenderer');  
  $content = "gdgdfgdfgdfgddfg";  
  
  // make a header as html  
  $html = new MimePart($content);  
  $html-&gt;type = "text/html";  
  $body = new MimeMessage();  
  $body-&gt;setParts(array($html,$attachment));  
  
  // instance mail   
  $mail = new Mail\Message();  
  $mail-&gt;setBody($body); // will generate our code html from template.phtml  
  $mail-&gt;setFrom('someone@gmail.com','Sender Name');  
  $mail-&gt;setTo('someone1@gmail.com');  
  $mail-&gt;setSubject('Your Subject');  
  
  $transport = new Mail\Transport\Smtp($options);  
  $transport-&gt;send($mail);
  </code></pre>
  
  <p>How can i achieve this .I've searched alot on this but getting no solution for this your help will be highly appriciated thanx.</p>