【问题标题】:using swift mailer of symfony 2, got an email but getting unnecessary response in email使用 symfony 2 的 swift mailer,收到一封电子邮件,但在电子邮件中收到不必要的回复
【发布时间】:2012-11-13 07:56:55
【问题描述】:

通过电子邮件收到此响应“HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: text/html; charset=UTF-8 Date: Tue, 13 Nov 2012 04:56:14 GMT”。

这是我的代码:

 public function sendEmail($subject, $template, $templateParams)
    {
        $userEmail = $this->session->get('email');
        $name = $this->session->get('name');
        $adminEmail = $this->container;
        $templateParams['username'] = $name; 
        $message = \Swift_Message::newInstance()
                ->setSubject($subject)
                ->setFrom($adminEmail)
                ->setTo($userEmail)
                ->setBody($this->templating->render('SocialDonSocialBundle:Email:'.$template,$templateParams), 'text/html'); 
        $this->mailer->send($message);

另请注意,此方法属于“电子邮件”服务。我创建了一个负责发送电子邮件的服务“电子邮件”。有谁知道可能是什么问题?

【问题讨论】:

  • 我看不出你为什么会得到这些标题。你的模板有没有做一些奇怪的事情,比如输出标题本身?
  • 我也收到了。你修好了吗?

标签: symfony twig swiftmailer


【解决方案1】:

你需要使用renderView()而不是render(),因为render()总是显示header。

【讨论】:

  • 我遇到了同样的问题。感谢您的回答
【解决方案2】:

在较新版本的 Symfony2 中,例如 2.5.*

解决方案是使用 renderResponse 并对其执行 getContent() :

$content = $this->container->get('templating')->renderResponse(
        'YOUR_TEMPLATE.twig',
        templateParams)
    )->getContent();

或与问题中的值相同:

$this->templating->renderResponse('SocialDonSocialBundle:Email:'.$template,$templateParams)->getContent();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2016-12-19
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多