无法在zend框架2中从localhost发送邮件

时间:2016-03-30 06:33:57

标签: php zend-framework2 phpmailer sendmail zend-mail

我无法使用localhost连接和正确的代码发送邮件。但是得到这个错误

Zend\Mail\Protocol\Exception\RuntimeException

File:

/var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/AbstractProtocol.php:348

Message:

5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&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> 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->_expect(235)
#1 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp.php(193): Zend\Mail\Protocol\Smtp\Auth\Login->auth()
#2 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(340): Zend\Mail\Protocol\Smtp->helo('localhost')
#3 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(325): Zend\Mail\Transport\Smtp->connect()
#4 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(336): Zend\Mail\Transport\Smtp->lazyLoadConnection()
#5 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(205): Zend\Mail\Transport\Smtp->connect()
#6 /var/www/html/rustagi/module/ZfcAdmin/src/ZfcAdmin/Controller/AdminController.php(185): Zend\Mail\Transport\Smtp->send(Object(Zend\Mail\Message))
#7 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): ZfcAdmin\Controller\AdminController->SendMailAction()
#8 [internal function]: Zend\Mvc\Controller\AbstractActionController->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->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->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->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#13 [internal function]: Zend\Mvc\DispatchListener->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->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 /var/www/html/rustagi/index.php(17): Zend\Mvc\Application->run()
#18 {main}

我正在使用此代码: -

$options = new Mail\Transport\SmtpOptions(array(  
        'name' => 'localhost',  
        'host' => 'smtp.gmail.com',  
        'port'=> 587,  
        'connection_class' => 'login',  
        'connection_config' => array(  
            'username' => 'someone@gmail.com',  
            'password' => '********',  
            'ssl'=> 'tls',  
        ),  
));  

$fileContents = fopen("/usr/share/pixmaps/faces/sky.jpg", 'r');
$attachment = new Mime\Part($fileContent);
$attachment->type = 'image/jpg';
$attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;

// $this->renderer = $this->getServiceLocator()->get('ViewRenderer');  
$content = "gdgdfgdfgdfgddfg";  

// make a header as html  
$html = new MimePart($content);  
$html->type = "text/html";  
$body = new MimeMessage();  
$body->setParts(array($html,$attachment));  

// instance mail   
$mail = new Mail\Message();  
$mail->setBody($body); // will generate our code html from template.phtml  
$mail->setFrom('someone@gmail.com','Sender Name');  
$mail->setTo('someone1@gmail.com');  
$mail->setSubject('Your Subject');  

$transport = new Mail\Transport\Smtp($options);  
$transport->send($mail);

我怎样才能实现这一目标。我已经搜索了很多但是没有得到解决方案,你的帮助将非常高,而不是。

4 个答案:

答案 0 :(得分:0)

更改行

'ssl'=> 'tls' // tls causes problems when sending via gmail

'ssl'=> 'ssl'

确保在配置中没有覆盖条目

smtp.params.ssl = "tls" // this is bad -> tls causes problems when sending via gmail

答案 1 :(得分:0)

与TLS无关。您还应该通过您的Gmail帐户here允许安全性较低的应用。为我工作.. !!

答案 2 :(得分:0)

对不起,如果我正在研究这个主题,但是我遇到了同样的问题。发送功能似乎不起作用,但是没有错误消息...

这是我的代码的一部分:

        $mail = new Mail\Message();
        $options = new Mail\Transport\SmtpOptions(array(  
        'name' => 'localhost',  
        'host' => 'smtp.gmail.com',  
        'port'=> 465,  
        'connection_class' => 'login',  
        'connection_config' => array(  
            'username' => 'someone@gmail.com',  
            'password' => '***',  
            'ssl'=> 'ssl',  
        ),  
    )); 
    $mail->setBody("This is the text of the email");
    $mail->setFrom('noreply@reims.fr','Ecole Municipal des Sports');
    $mail->addTo('someone@gmail.com');
    $mail->setSubject('Inscription annulée');
    $transport = new Mail\Transport\Sendmail('someone@gmail.com',$options);
    $transport->send($mail);
    var_dump($mail);
    var_dump($transport);
    var_dump($transport->send($mail));

答案 3 :(得分:-2)

看你的配置是假的 使用ssl或tls? 我建议您使用phpmailer脚本

来自php.ini的第一个活动SMTP服务

搜索:[邮件功能]

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

下次你必须使用PHPMailer类〜&gt; Download PHPMailer

模板功能:

使用此↓

<?php
    require_once ("PHPMailer/class.phpmailer.php");
    $mail = new PHPMailer(true);
    $mail->isSMTP();

   try{
    $mail->host = "smtp@gmail.com";
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Port = 465;
    $mail->Username = "username@email.com";
    $mail->Password = "yourPassword";
    $mail->AddAddress("target@email.com");
    $mail->SetForm("yourMail@Email.com","your name");
    $mail->Subject = "emailSubject";
    $mail->CharSet = "UTF-8";
    $mail->ContentType = "text/htm";
    $mail->msgHTML("MailContent");
    $mail->send();
        echo 'Seccess';

    }
   catch(phpmailerException $mail){
    echo $mail->errorMessage();
    }
    catch(Exception $mail){
        echo $mail->getMessage();
    }
?>