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メールを送信する必要があります。HTMLタグを書くように更新することはできMESSAGEますが、それは正しいアプローチではないと思います。HTML はテンプレートに入れる必要があります。他のSOの質問で解決策を試しましたが、うまくいきません(sfViewで失敗しました)。メールクラスを使用せずに再びその質問回答条件。

Email クラスのビューを作成して使用するにはどうすればよいですか?

コメント 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

ForgetPasswordMessagehtmlコンテンツを受け入れるには、からパラメータを更新する必要があります。

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の場合はOK)

編集

それ以外の場合は、クラスから直接パーシャルを呼び出すことができます。

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 に答える