【问题标题】:Zend Framework - Send only 1 mailZend 框架 - 只发送 1 封邮件
【发布时间】:2016-02-14 18:03:09
【问题描述】:

我在使用 Zend\Mail 的应用程序(Zend Framework 2)中有这个功能。

这是一个示例场景,用户单击一个复选框,然后继续提交它。收件人将收到包含内容的电子邮件。

这是发送函数。

public function sendNotification()
{
    $mail = new Mail\Message();
    $mail->setBody('This is the text of the email.');
    $mail->setFrom('Freeaqingme@example.org', 'Sender\'s name');
    $mail->addTo('user1.arak@gmail.com', 'Name of recipient');
    $mail->setSubject('TestSubject');

    $transport = new Mail\Transport\Sendmail();
    $transport->send($mail);
}

我的问题是,当用户单击 2 个或多个复选框然后单击提交按钮时,它会发送 2 个或多个电子邮件,我想要做的是只向接收者发送 1 封电子邮件。

我该怎么办? 任何想法将不胜感激。请评论您想澄清和想知道的任何内容。

【问题讨论】:

  • 此时我不确定复选框是否重要。您是否多次调用该方法?
  • 是的,就是这样,因为每次单击并提交复选框时都会调用该方法。
  • 好吧,你为什么要这么做?不要那样做。或者如果用户已经发送了一封电子邮件,则存储某种值。
  • 感谢您陈述我一直忽略的内容!我会改的。
  • 不要在单击复选框时发送电子邮件,而是在表单提交时发送。

标签: php zend-framework2 zend-mail


【解决方案1】:

解决方案非常简单。您需要做的就是跟踪 sendNotification() 方法之前是否被调用过。

该会话非常适合此。为了使其工作并同时看起来更干净,您可以将其包装成一个独立的方法,如下所示:

public function sendNotificationOnDemand()
{
   $session = new \Zend\Session\Container();

   if (!$session->offsetExists('mail_sent')) {
      $session->offsetSet('mail_sent', true);
      return $this->sendNotification();
   }
}

所以无论你拨打多少次sendNotificationOnDemand(),通知都只会发送一次。

【讨论】:

    【解决方案2】:
        public function sendNotification()
        {
    
         $session = new Container('email');
    
        if($session->offsetGet('send') != 'yes') {
            $mail = new Mail\Message();
            $mail->setBody('This is the text of the email.');
            $mail->setFrom('Freeaqingme@example.org', 'Sender\'s name');
            $mail->addTo('user1.arak@gmail.com', 'Name of recipient');
            $mail->setSubject('TestSubject');
    
            $transport = new Mail\Transport\Sendmail();
            $transport->send($mail);
    
             $session->setExpirationSeconds(5000);
             $session->offsetSet('send','yes');
        }
    
    }  
    

    【讨论】:

      【解决方案3】:

      如果您希望禁止每个用户多次调用功能,您需要一些存储信息(用户已调用操作,例如收到的电子邮件)的存储空间。

      每当用户尝试重新发送电子邮件时,您的代码都会从存储中查找信息,并可以决定是否重新发送电子邮件的结果(已接收/未发送)。

      正如已经建议的那样,您可以使用 ZF2 会话存储Zend\Session\Container

      http://framework.zend.com/manual/current/en/modules/zend.session.container.html

      但请记住,会话不是永久的信息存储。如果用户关闭浏览器并重新访问您的网站,则之前操作的信息会丢失,他可以重新提交表单。

      ZF2 会话示例

      use Zend\Session\Container;
      
      public function sendNotification()
      {
          $sessionContainer = new Container;
      
          if (!$sessionContainer->offsetExists('mail_send')) {
      
              $sessionContainer->offsetSet('mail_send', true);
      
              // send notification
              ...
          }
      }
      

      如果您想保持信息的持久性,请使用像MySQL 这样的持久性数据库存储。获取和存储类似。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 2014-09-29
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 2012-01-20
        • 2015-12-19
        相关资源
        最近更新 更多