使用Zend Framework发送HTML电子邮件时出现问题

时间:2019-01-13 17:25:57

标签: php html email zend-framework

嗨,我有以下内容:

 public function generatePasswordResetToken($user)
{
    if ($user->getStatus() != User::STATUS_ACTIVE) {
        throw new \Exception('Cannot generate password reset token for inactive user ' . $user->getEmail());
    }

    // Generate a token.
    $token = Rand::getString(32, '0123456789abcdefghijklmnopqrstuvwxyz', true);

    // Encrypt the token before storing it in DB.
    $bcrypt = new Bcrypt();
    $tokenHash = $bcrypt->create($token);  

    // Save token to DB
    $user->setPasswordResetToken($tokenHash);

    // Save token creation date to DB.
    $currentDate = date('Y-m-d H:i:s');
    $user->setPasswordResetTokenCreationDate($currentDate);  

    // Apply changes to DB.
    $this->entityManager->flush();

    // Send an email to user.
    $subject = 'Password Reset';

    $httpHost = isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'localhost';
    $passwordResetUrl = 'http://' . $httpHost . '/set-password?token=' . $token . "&email=" . $user->getEmail();

    // Produce HTML of password reset email
    $bodyHtml = $this->viewRenderer->render(
            'user/email/reset-password-email',
            [
                'passwordResetUrl' => $passwordResetUrl,
            ]);

    $html = new MimePart($bodyHtml);
    $html->type = "text/html";

    $body = new MimeMessage();
    $body->addPart($html);

    $mail = new Mail\Message();
    $mail->setEncoding('UTF-8');
    $mail->setBody($body);
    $mail->setFrom('no-reply@example.com', 'User Demo');
    $mail->addTo($user->getEmail(), $user->getFullName());
    $mail->setSubject($subject);

    // Setup SMTP transport
    $transport = new SmtpTransport();

    //$options   = new SmtpOptions($this->config['smtp']);

    $options = $options   = new SmtpOptions([
        'name'              => 'localhost.localdomain',
        'host'              => '127.0.0.1',
        'connection_class'  => 'plain',
        'connection_config' => [
            'username' => 'zenduser',
            'password' => 'xxxxxxxxx',
        ],
    ]);

    $transport->setOptions($options);

    $transport->send($mail);
}

当我调度控制器时,它给了我一个例外。调试到send()方法没有任何问题。我使用zend3。有什么建议吗?如果我改为在local.php中给$ options作为

'smtp' => [
    'name'              => 'localhost.localdomain',
    'host'              => '127.0.0.1',
    'port'              => 25,
    'connection_class'  => 'plain',
    'connection_config' => [
        'username' => 'zenduser',
        'password' => 'xxxxxxxxx',
    ],
],

然后在我的课上使用

$options = new SmtpOptions($this->config['smtp']);

尝试连接到smtp时出错。我的用户名为<user>,密码为<pass>。 有什么建议吗?

#0 C:\xampp\htdocs\using-zf3-book-samples-
master\userdemo\vendor\zendframework\zend- 
mail\src\Protocol\Smtp\Auth\Plain.php(74): 
Zend\Mail\Protocol\AbstractProtocol->_expect(Array)

0 个答案:

没有答案