Zend \ Mail \消息问题

时间:2013-04-06 20:08:20

标签: zend-framework2

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

    $message = new Message();
    $message->addTo('ToEmail@gmail.com')
      ->addFrom('FromEmail@gmail.com')
      ->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' => 'FromEmail@gmail.com',
            'password' => 'FromPassword',
            'ssl'      => 'tls',
        ),
    ));
    $transport->setOptions($options);
    $transport->send($message);

1 个答案:

答案 0 :(得分: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('FromEmail@gmail.com');  
                            $mail->setTo('ToEmail@gmail.com');
                            $mail->setSubject('Subject for sending email.');

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

如果您在dineshs@mindfiresolutions.com面对任何问题,请告诉我。