I have this feature in my application (Zend Framework 2) that uses the Zend\Mail.
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.
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.
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.
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 e.g. 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
.
http://framework.zend.com/manual/current/en/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
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
. Fetching and storing would be similar.