public function sendByEmailAction() { $uid = $this->_getParam('uid'); $emails = $this->_getParam('emails'); $origin = $this->getRequest()->getHeader('referer'); $origin .= "&emails=" . $emails; // Checking if parameters are passed if ($uid && $emails) { // Checking if uid is a valid user $user = UserDao::getInstance()->get($uid); if ($user) { // Getting user wished books $wishedUserbooks = $user->getNotDeletedUserBooks(); $wishedUserbooks = array_filter($wishedUserbooks, array(&$this, "isWished")); // Cheking if some valid emails are passed $emailsArray = array($emails); if (strpos(",", $emails) !== 0) { $emailsArray = explode(",", $emails); } foreach ($emailsArray as $email) { if (!StringHelper::isValidEmail($email)) { Flash::addItem(__("Un des emails renseigné n'est pas valide.", "s1b")); $this->_redirect($origin); exit; } } // Building the mail content $emailContent = \Sb\Helpers\MailHelper::wishedUserBooksEmailBody($user, $wishedUserbooks); // Sending mail MailSvc::getInstance()->send($emails, sprintf(__("%s - Liste des livres souhaités par %s", "s1b"), Constants::SITENAME, $user->getFriendlyName()), $emailContent); Flash::addItem(__("La liste a bien été envoyée par email.", "s1b")); $this->_redirect($origin); exit; } } Flash::addItem(__("Une erreur s'est produite lors de l'envoi de la liste par email", "s1b")); $this->_redirect($origin); exit; }
public function testIsValidEmailKO() { $emailToTest = "*****@*****.**"; $validEmail = \Sb\Helpers\StringHelper::isValidEmail($emailToTest); $this->assertEquals($validEmail, false, "\\Sb\\Helpers\\StringHelper::isValidEmail failed testing a unvalid email."); }
/** * Send invitation to users */ public function inviteAction() { try { $globalContext = new \Sb\Context\Model\Context(); $user = $globalContext->getConnectedUser(); $continue = true; $emails = ArrayHelper::getSafeFromArray($_POST, 'Emails', null); $message = ArrayHelper::getSafeFromArray($_POST, 'Message', null); if (!$emails || !$message) { Flash::addItem(__("Vous devez renseigner tous les champs obligatoires.", "s1b")); $continue = false; } if ($continue) { $emailsListFromPost = explode(",", $emails); // Getting emails list if ($emailsListFromPost) { // Looping through all emails for validating all of them // At the end of the loop: // we will have an array of emails to be processed // and flag to process or not the sendings $emailsList = array(); foreach ($emailsListFromPost as $emailToInvite) { $addEmail = true; $emailToInvite = strtolower(trim($emailToInvite)); if ($emailToInvite != "") { // Testing if the email is valid if (!StringHelper::isValidEmail($emailToInvite)) { Flash::addItem(sprintf(__("%s n'est pas un email valide.", "s1b"), $emailToInvite)); // We will stop invitation sending $continue = false; // Current email not added to the array of emails to be processed $addEmail = false; } else { // Testing if the email does not match an existing user $userInDb = UserDao::getInstance()->getByEmail($emailToInvite); if ($userInDb && !$userInDb->getDeleted()) { $friendRequestUrl = HTTPHelper::Link(Urls::USER_FRIENDS_REQUEST, array("fid" => $userInDb->getId())); Flash::addItem(sprintf(__("Un utilisateur existe déjà avec l'email : %s. <a class=\"link\" href=\"%s\">Envoyer lui une demande d'ami</a>", "s1b"), $emailToInvite, $friendRequestUrl)); // We will stop invitation sending $continue = false; // Current email not added to the array of emails to be processed $addEmail = false; } else { // Testing if invitations have been sent to that guest (email) by the current user $invitations = InvitationDao::getInstance()->getListForSenderAndGuestEmail($user, $emailToInvite); if ($invitations && count($invitations) > 0) { Flash::addItem(sprintf(__("Vous avez déjà envoyé une invitation à cet email : %s.", "s1b"), $emailToInvite)); // We will stop invitation sending $continue = false; // Current email not added to the array of emails to be processed $addEmail = false; } } } } if ($addEmail) { $emailsList[] = $emailToInvite; } } if ($continue) { // Looping through all emails for sending invitation $initialMessage = $message; foreach ($emailsList as $emailToInvite) { $emailToInvite = strtolower(trim($emailToInvite)); if ($emailToInvite != "") { $sendInvitation = true; // Testing again if invitations have been sent to that guest (email) by the current user $invitations = InvitationDao::getInstance()->getListForSenderAndGuestEmail($user, $emailToInvite); if ($invitations && count($invitations) > 0) { $sendInvitation = false; } if ($sendInvitation) { // Getting existing guests matching email, and take first 1 $guest = null; $guests = GuestDao::getInstance()->getListByEmail($emailToInvite); if ($guests && count($guests) > 0) { $guest = $guests[0]; } $token = sha1(uniqid(rand())); // Send invite email $message = str_replace("\n", "<br/>", $initialMessage); $message .= "<br/><br/>"; $message .= sprintf(__("<a href=\"%s\">S'inscrire</a> ou <a href=\"%s\">Refuser</a>", "s1b"), HTTPHelper::Link(Urls::SUBSCRIBE), HTTPHelper::Link(Urls::REFUSE_INVITATION, array("Token" => $token, "Email" => $emailToInvite))); $message .= "<br/><br/>"; $message .= "<strong>" . sprintf(__("L'équipe %s", "s1b"), Constants::SITENAME) . "<strong/>"; MailSvc::getNewInstance($user->getEmail(), $user->getEmail())->send($emailToInvite, sprintf(__("Invitation à rejoindre %s", "s1b"), Constants::SITENAME), $message); // Create invitation $invitation = new Invitation(); $invitation->setSender($user); $invitation->setToken($token); $invitation->setLast_modification_date(new \DateTime()); if ($guest) { // Updating guest $guest->addInvitations($invitation); GuestDao::getInstance()->update($guest); } else { // Create guest $guest = new Guest(); $guest->setEmail($emailToInvite); $guest->setCreation_date(new \DateTime()); $guest->addInvitations($invitation); GuestDao::getInstance()->add($guest); } Flash::addItem(sprintf(__("Une invitation a été envoyée à %s.", "s1b"), $emailToInvite)); } } } HTTPHelper::redirectToHome(); } } } // If we arrive here : an error occured, then redirect to the invite form HTTPHelper::redirect(Urls::USER_FRIENDS_INVITE, array("emails" => $emails)); } catch (\Exception $e) { Trace::addItem(sprintf("Une erreur s'est produite dans \"%s->%s\", TRACE : %s\"", get_class(), __FUNCTION__, $e->getTraceAsString())); $this->forward("error", "error", "default"); } }
private function validateUserInputForm() { $ret = true; if ($_POST) { if (strlen(ArrayHelper::getSafeFromArray($_POST, "guest_name", NULL)) < 3) { Flash::addItem(__("Le nom doit comprendre au moins 3 caractères.", "s1b")); $ret = false; } if (ArrayHelper::getSafeFromArray($_POST, "send_invitation", NULL) == 1) { $guestEmail = ArrayHelper::getSafeFromArray($_POST, "guest_email", NULL); if (!$guestEmail) { Flash::addItem(__("Vous devez renseigné un email si vous souhaitez envoyer une invitation.", "s1b")); $ret = false; } else { if (!StringHelper::isValidEmail($guestEmail)) { Flash::addItem(__("L'email que vous avez renseigné n'est pas valide. Merci de réessayer.", "s1b")); $ret = false; } } } } else { $ret = false; } return $ret; }