我正在使用 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);
}
}