2013-04-06 53 views
1

使用ZF2的Message类尝试发送电子邮件。但是,在执行下面的代码之后,页面会尝试重定向,一分钟左右之后会给我一个“本地主机超时”错误。有任何想法吗?Zend Mail Message问题

$message = new Message(); 
    $message->addTo('[email protected]') 
     ->addFrom('[email protected]') 
     ->setSubject('Greetings and Salutations!') 
     ->setBody("Sorry, I'm going to be late today!"); 

    // Setup SMTP transport using LOGIN authentication 
    // Setup SMTP transport using PLAIN authentication over TLS 
    $transport = new SmtpTransport(); 
    $options = new SmtpOptions(array(
     'name'    => 'localhost', 
     'host'    => 'localhost', 
     'port'    => 8888, // Notice port change for TLS is 587 
     'connection_class' => 'plain', 
     'connection_config' => array(
      'username' => '[email protected]', 
      'password' => 'FromPassword', 
      'ssl'  => 'tls', 
     ), 
    )); 
    $transport->setOptions($options); 
    $transport->send($message); 
+0

只是好奇任何问题,但根据您的设置,你可以远程登录本地主机8888并获得回应?普通SMTP设置是端口25或587(TLS)。 – Diemuzi 2013-04-06 20:24:52

+0

是的,我可以telnet到8888.我试过25和587,它无法连接。我也试过再次执行代码,而不是得到一个“本地主机超时”错误,我得到以下错误324(net :: ERR_EMPTY_RESPONSE):服务器关闭连接而不发送任何数据。“ – Mosinel 2013-04-06 20:41:55

+0

由于您使用的是SMTP,您是否可以尝试将该服务器用作桌面邮件客户端上的发送邮件服务器,以查看发送甚至是否可以从那里发送? – Diemuzi 2013-04-06 20:52:38

回答

0

我可以使用Zend Framework 2.0发送电子邮件。请按照下面的代码。

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


          $content = 'Message to be sent in the email.(goes here)'; 

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

          // instance mail 
          $mail = new Mail\Message(); 
          $mail->setBody($body); // will generate our code html from template.phtml 
          $mail->setFrom('[email protected]'); 
          $mail->setTo('[email protected]'); 
          $mail->setSubject('Subject for sending email.'); 

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

让我知道,如果你面对的[email protected]