简体   繁体   English

Zend Framework-仅发送1封邮件

[英]Zend Framework - Send only 1 mail

I have this feature in my application (Zend Framework 2) that uses the Zend\\Mail. 我在使用Zend \\ Mail的应用程序(Zend Framework 2)中具有此功能。

This is a sample scenario, a user clicks a checkbox and then proceeds to submit it. 这是一个示例场景,用户单击一个复选框,然后继续提交。 The receiver will receive the email with the content. 收件人将收到包含内容的电子邮件。

This is the function for sending. 这是发送功能。

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);
}

My issue here is that when a user clicks 2 or many checkbox and then clicks the submit button, it sends 2 or many emails, what I want to do is to send only 1 email to the receiver. 我的问题是,当用户单击2个或多个复选框,然后单击“提交”按钮时,它将发送2个或多个电子邮件,我想做的是仅向收件人发送1个电子邮件。

What should I do with this? 我该怎么办? Any idea(s) would be much appreciated. 任何想法将不胜感激。 Please comment anything that you want to clarify and want to know. 请评论任何您想澄清和想知道的内容。

The solution for this is pretty simple. 解决方案非常简单。 All you need to do is to track if the method sendNotification() has been called before or not. 您需要做的只是跟踪方法sendNotification()是否已被调用过。

The session would ideally fit for this. 该会议非常适合此。 To make it work and look cleaner at the same time, you can wrap it into a standalone method, like this: 为了使其同时工作并看起来更整洁,可以将其包装为一个独立的方法,如下所示:

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

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

So no matter, how many times you would call sendNotificationOnDemand() , the notification will be sent only once. 因此,无论调用多少次sendNotificationOnDemand() ,通知都只会发送一次。

    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');
    }

}  

If you wish to disallow functionality being called multiple times per user you need some storage where the information (user allready called action eg received email) is stored. 如果您希望不允许每个用户多次调用该功能,则需要一些存储信息(用户已经调用的动作,例如收到的电子邮件)的存储空间。

Whenever the user trys to resend the email, your code will lookup the information from storage and can decide on the result (allready reveiced/not send) if the email should be resend or not. 每当用户尝试重新发送电子邮件时,您的代码都会从存储中查找信息,并可以决定是否重新发送电子邮件(已全部显示/未发送)。

As allready suggested you can use the ZF2 Session Storage Zend\\Session\\Container . 正如已经建议的那样,您可以使用ZF2会话存储Zend\\Session\\Container

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

But keep in mind that Sessions are not permanent information storages . 但是请记住, 会话不是永久的信息存储 If the user close his browser and revisit your site, the information from previous actions are lost and he could resubmit the form. 如果用户关闭浏览器并重新访问您的网站,则先前操作中的信息将会丢失,因此他可以重新提交表单。

ZF2 Session Example ZF2会话示例

use Zend\Session\Container;

public function sendNotification()
{
    $sessionContainer = new Container;

    if (!$sessionContainer->offsetExists('mail_send')) {

        $sessionContainer->offsetSet('mail_send', true);

        // send notification
        ...
    }
}

If you want to keep the information persistent use a persistent database storage like MySQL . 如果要保持信息的持久性,请使用MySQL等持久性数据库存储。 Fetching and storing would be similar. 获取和存储将类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM