codific / sammy

SAMMY Open Source v2
https://sammy.codific.com
Creative Commons Attribution Share Alike 4.0 International
13 stars 4 forks source link

Cannot get email sending to work #31

Closed fasc8 closed 1 month ago

fasc8 commented 2 months ago

I cannot get the email sending to work which is required to create new users and I assume is useful beyond that.

My situation: I have an SMTP server running which I can connect to from my container (I checked for that by manually sending a mail via curl request: curl --url 'smtp://localhost:25' --mail-rcpt 'recipient@example.com' -T <(echo -e 'From: from-email@gmail.com\nTo: to-email@gmail.com\nSubject: Curl Test\n\nHello')). The server uses an anonymous login, so no username and password is required. To make sammy compatible with that I did the following changes to the MailingService and the ProcessMailingCommand:

diff --git a/src/Command/ProcessMailingCommand.php b/src/Command/ProcessMailingCommand.php
index 4e5d578..6adddde 100644
--- a/src/Command/ProcessMailingCommand.php
+++ b/src/Command/ProcessMailingCommand.php
@@ -34,7 +34,7 @@ class ProcessMailingCommand extends Command
         $port = (int) $this->parameterBag->get('phpmailer.smtp.port');
         $username = $this->parameterBag->get('phpmailer.smtp.username');
         $password = $this->parameterBag->get('phpmailer.smtp.password');
-        if ($host === '' || $port === 0 || $username === '' || $password === '') {
+        if ($host === '' || $port === 0) {
             $io->text('[' . date('c') . '] Mail cron - Missing mail configuration');

             return Command::SUCCESS;
@@ -44,4 +44,4 @@ class ProcessMailingCommand extends Command

         return Command::SUCCESS;
     }
-}
+}
\ No newline at end of file
diff --git a/src/Service/MailingService.php b/src/Service/MailingService.php
index 23e1758..4d6b13c 100644
--- a/src/Service/MailingService.php
+++ b/src/Service/MailingService.php
@@ -192,10 +192,10 @@ class MailingService
             $mail->isSMTP();
             $mail->Host = $this->parameterBag->get('phpmailer.smtp.host');
             $mail->Port = (int) $this->parameterBag->get('phpmailer.smtp.port');
-            $mail->SMTPAuth = true;
-            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
-            $mail->Username = $this->parameterBag->get('phpmailer.smtp.username');
-            $mail->Password = $this->parameterBag->get('phpmailer.smtp.password');
+            #$mail->SMTPAuth = true;
+            #$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
+            #$mail->Username = $this->parameterBag->get('phpmailer.smtp.username');
+            #$mail->Password = $this->parameterBag->get('phpmailer.smtp.password');

             $mail->setFrom($from, $fromName);
             $mail->addAddress($to, $toName);
@@ -220,4 +220,4 @@ class MailingService

         return $result;
     }
-}
+}
\ No newline at end of file

I also added the appropriate environment variables to the compose file with the host and port. As the username and password are not used anymore, I did not added them to the compose file.

My hope was that this would achieve disabling authentication for sending emails while leaving all other functionality intact. But now I encounter the issue that no emails are sent.

Further debugging showed that the processMailing() function never goes into its for loops and just runs through. Even though I created new users and therefore an email should have been sent to them to provide them with login credentials.

I hope you can help me to find a solution. Looking forward to your replies.

fasc8 commented 2 months ago

Further research into why no email is sent has let me down to look into the whole user creation (addition) process.

After looking into what prerequisites the ProcessMailingCommand and subsequently the MailingService need, I had a look into the database and realized that there is no entry in the mailing table after a new user is created. As this is required to have an email sent via the ProcessMailingCommand, I continued to look into where this entry should have come from in the first place.

I currently assume that the following method in the UserSubscriber is not called / firing:

#[NoReturn]
public function onUserCreated(UserCreatedEvent $event)
{
    $user = $event->getUser();
    $isAdmin = in_array(Role::ADMINISTRATOR->string(), $user->getRoles(), true);
    if (!$isAdmin) {
        $isAlone = count($this->userRepository->findAllExcept($user)) === 0;
        if ($isAlone) {
            $user->setRoles(User::getAllNonAdminRoles());
            $hasNoGroups = count($this->groupRepository->findAll()) === 0;
            if ($hasNoGroups) {
                $newGroup = $this->groupService->createGroup("Team", null);
                $this->userService->addUserToGroups($user, [$newGroup]);
            }
        }
        $userIsMissingUserRole = !in_array(Role::USER->string(), $user->getRoles(), true);
        if ($userIsMissingUserRole) {
            $newRoles = $user->getRoles();
            $newRoles[] = Role::USER->string();
            $user->setRoles($newRoles);
        }

        if ($user->getDateFormat() === null || $user->getDateFormat() === '') {
            $user->setDateFormat(DateTimeUtil::DEFAULT_DATE_FORMAT);
        }
    }
    $mailTemplate = $event->getMailTemplate();
    $this->userService->welcomeUser($user, $mailTemplate); # <---------------
    $this->entityManager->flush();
}

Which leads to never calling the welcomeUser method which would perform a password reset on the newly created user and insert a mailing into the mailing table. Which in turn would then get picked up by the ProcessMailingCommand and sends the mail.

I am not sure how to test for this which is why it is only my assumption at the moment. Hopefully this can help you in finding a solution to my problem.

fasc8 commented 2 months ago

Looking into the Symfony event dispatcher shows that the UserCreatedEvent is needed to get the aforementioned onUserCreated method to be called. Searching through the source files it seems that this event is never called.

aramhovsepyan commented 1 month ago

Hi fasc8, Indeed, UserCreatedEvent was not created after register. Should be fixed now. Regards.