0

我正在使用 symfony 1.4。对于电子邮件,我使用的是 SwiftMail。以下代码对我来说非常好。

class ForgetPasswordMessage extends Swift_Message
{
    const SUBJECT = 'Reset Password verification mail from Awssum.com.';
    const MESSAGE = 'Test Message';
    public function __construct($to)
    {
        parent::__construct(self::SUBJECT, self::MESSAGE);

        $this->setFrom(array(Member::getAdminMember()->getEmail() => Troupa));
        $this->setTo($to);
        $this->setContentType('text/html');
    }
}//ForgetPasswordMessage

但是我需要发送一个 HTML 邮件。我可以更新MESSAGE以编写 html 标签,但我想这不是正确的方法。HTML 必须放在模板中。我尝试解决其他 SO 问题,但它对我不起作用(在 sfView 中失败)。再次,不使用邮件类的问题回答条件。

如何为电子邮件类编写和使用视图?

在评论 1 后编辑

控制器中的代码

public function executeForgetMail(sfWebRequest $request)
{
    try
    {
        $to = $request->getParameter('email');
        $criteria = new Criteria();
        $criteria->add(AppUsersPeer::EMAIL, $to);
        $user = AppUsersPeer::doSelectOne($criteria);
        if($user)
        {
            $mailer=$this->getMailer();
            $mailer->send(new ForgetPasswordMessage($to));
        }
        else
        {
            throw new Exception("The email does not exist. Please try again.");
        }
    }
    catch(Exception $e)
    {
        $this->getUser()->setFlash('error', sprintf($e->getMessage()));
        $this->redirect('signup/forgot');
    }
    $this->getUser()->setFlash('error', "An email with password reset link has been send to your email.");
    $this->redirect('signup/forgot');
}

回答评论后编辑

中的新代码ForgetPasswordMail.class.php

<?php
class ForgetPasswordMail extends TroupaBaseMessage
{
    public function __construct($user) {
        $subject = 'Reset Password verification mail from Awssum.com.';

        sfLoader::loadHelpers(array('Partial'));

        $mailContext = array('user' => $user);
        $mailBody = get_partial('global/emails/forget_password', $mailContext);

        parent::__construct($user->getEmail(), $mailBody);
    }
}
4

1 回答 1

1

您应该更新参数 fromForgetPasswordMessage以接受 html 内容:

class ForgetPasswordMessage extends Swift_Message
{
    const SUBJECT = 'Reset Password verification mail from Awssum.com.';
    const MESSAGE = 'Test Message';

    public function __construct($to, $htmlBody = null)
    {
        $message = self::MESSAGE;
        if (null !== $htmlBody)
        {
          $message = $htmlBody;
        }

        parent::__construct(self::SUBJECT, $message);

        $this->setFrom(array(Member::getAdminMember()->getEmail() => Troupa));
        $this->setTo($to);
        $this->setContentType('text/html');
    }
}

然后调用一个部分来获得 html 内容:

    $to = $request->getParameter('email');
    $criteria = new Criteria();
    $criteria->add(AppUsersPeer::EMAIL, $to);
    $user = AppUsersPeer::doSelectOne($criteria);
    if($user)
    {
        $mailBody = $this->getPartial('module/partial_name', array('user' => $user));
        $mailer=$this->getMailer();
        $mailer->send(new ForgetPasswordMessage($to, $mailBody));
    }
    else
    {
        throw new Exception("The email does not exist. Please try again.");
    }

检查这本食谱(适用于 sf 1.2,但适用于 1.4)

编辑

否则,您可以直接从您的课程中调用部分:

class ForgetPasswordMessage extends Swift_Message
{
    const SUBJECT = 'Reset Password verification mail from Awssum.com.';
    const MESSAGE = 'Test Message';

    public function __construct($to)
    {
        // needed to be able to user `get_partial`
        sfApplicationConfiguration::getActive()->loadHelpers(array('Partial'));

        $mailContext = array('test' => 'wahou');
        $mailBody = get_partial('module/partial_name', $mailContext);

        parent::__construct(self::SUBJECT, $mailBody);

        $this->setFrom(array(Member::getAdminMember()->getEmail() => Troupa));
        $this->setTo($to);
        $this->setContentType('text/html');
    }
}
于 2012-10-09T09:07:19.830 回答