We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35291
    • 21 Posts
    Hi all!

    It might seem weird but i can’t seem to get Modx to sent emails (i need smtp, authenticated...i need swiftmailer, it works!) After scavenging code upon code, i finally got the Login.Register snippet to send an email (only the link to Login.ConfirmRegister... sweat - sweat;!)

    to do this, i modified ’core\components\login\processors\register.php’ as follow;

    i commented out theses lines;

    <?php
    // ---------------------------------------------------------------------------------------------------------------------	
        // /* send either to user's email or a specified activation email */
        // $activationEmail = !empty($scriptProperties['activationEmail']) ? $scriptProperties['activationEmail'] : $user->get('email');
        // $subject = $modx->getOption('activationEmailSubject',$scriptProperties,$modx->lexicon('register.activation_email_subject'));
        // $login->sendEmail($activationEmail,$user->get('username'),$subject,$emailProperties);
    // ---------------------------------------------------------------------------------------------------------------------
    


    I then added my minimalist replacement code;

    <?php
    $recipientMail = $user->get('email');
    $recipientName = $user->get('username');
    $subject = $modx->getOption('activationEmailSubject',$scriptProperties,$modx->lexicon('register.activation_email_subject'));
    
    // $activationEmail seems not to do anything with swiftmailer - or is it me? (help)
    // I used $confirmUrl as a quick fix (no template processing?)
    $activationEmail = !empty($scriptProperties['activationEmail']) ? $scriptProperties['activationEmail'] : $user->get('email');
    
    $modx->getService('mail', 'mail.modSwiftMailer');
    $modx->mail->address('to', $recipientMail, $recipientName);
    $modx->mail->address('from', '[email protected]', 'xxxx.com account activation');
    $modx->mail->subject($subject);
    $modx->mail->body($confirmUrl);
    $modx->mail->send();
    


    It’s not pretty, well tought-about, but it works - For now...

    Nice, i then said... but as of now it’s very limited. Only Login.Register has been modified (and simply poorly) to send an e-mail.

    My issues (let-alone my mental state):

    - My modification is only a temporary fix for registering.
    - It ONLY works for registration, since i modified the original file.
    - Currently, this modification doesn’t process any e-mail template, it just sends the url to the e-mail address.

    After checking out ’core\components\login\model\login\login.class.php’ i got to the sendEmail() function.

    <?php
        public function sendEmail($email,$name,$subject,$properties = array()) {
            if (empty($properties['tpl'])) $properties['tpl'] = 'lgnForgotPassEmail';
            if (empty($properties['tplType'])) $properties['tplType'] = 'modChunk';
    
            $msg = $this->getChunk($properties['tpl'],$properties,$properties['tplType']);
    
            $this->modx->getService('mail', 'mail.modPHPMailer');
            $this->modx->mail->set(modMail::MAIL_BODY, $msg);
            $this->modx->mail->set(modMail::MAIL_FROM, $this->modx->getOption('emailsender'));
            $this->modx->mail->set(modMail::MAIL_FROM_NAME, $this->modx->getOption('site_name'));
            $this->modx->mail->set(modMail::MAIL_SENDER, $this->modx->getOption('emailsender'));
            $this->modx->mail->set(modMail::MAIL_SUBJECT, $subject);
            $this->modx->mail->address('to', $email, $name);
            $this->modx->mail->address('reply-to', $this->modx->getOption('emailsender'));
            $this->modx->mail->setHTML(true);
            $sent = $this->modx->mail->send();
            $this->modx->mail->reset();
    
            return $sent;
        }


    My questions:

    - Is there an easy way to modify the above code to seemlessly (snippet(s) must behave as intended) use swiftmailer as ’mailer’?
    - Would this be worth as feature request?

    Please consider helping... i am building this site non-profit. No clients involved, no timeframe. Basically, it’s a matter of knowledge, to keep my brain challenged as everyday business isn’t satisfying enough.

    I’m sure it’ll help some other fellow modx’ers!

    Thanks!

    Addendum;

    Would it be possible to be more clear on the ’Login’ snippet documentation in general? It lacks uniformity ie; all ’Login.*’ snippets should be detailed as if you never even use or heard of the ’Login’ snippet before. Derivated snippets should’nt assume you’ve read everything before, after, in between.

    Just my 2 cents! Again - Thanks!
      • 4971
      • 964 Posts
      Somebody wrote and extra that is a wrapper for swiftMailer
      I think is something like modxSwiftMailer, it is in the extras
      section of the docs...
        Website: www.mercologia.com
        MODX Revo Tutorials:  www.modxperience.com

        MODX Professional Partner
        • 35291
        • 21 Posts
        Thanks for the reply, unfortunately, modSwiftMailer (as described in my original post) already works fine with my current setup.

        What i really need to know:

        How to force the Login snippet to use modSwiftMailer instead of the default mailer in the script.

        i understand part of the code, well, enough to achieve sending an e-mail from the Login.Register... My solution was a workaround to get it to work.

        I now need something permanent (without editing/modifying loads of files...)

        Could it be as simple as replacing

        <?php
        $this->modx->getService('mail', 'mail.modPHPMailer');
        


        With;

        <?php
        $this->modx->getService('mail', 'mail.modSwiftMailer');
        


        ?

        I’ll continue testing, parsing code and trying to figure it out. In the meantime, Shaun, where are you??? smiley))

        Thanks!
          • 35291
          • 21 Posts
          So, couple minutes later, i tested my previous ’could it be as easy as’...

          Turns out it is.

          So, just to make things clear with my previous posts - thus maybe helping someone else in the process;

          If you absolutely need to use SMTP to send an email from modx (for whatever reasons) or you’re just having troubles sending emails from the Login snippet, here’s what i found out;

          - Try installing modSwiftMailer from modx extra
          - Test it with the supplied test script

          If you achieve sending an e-mail using modSwiftmailer and want to use it as default mailer for the Login snippet, do the following;

          - Locate \core\components\login\model\login\login.class.php and edit it
          - Look for (use search or even better, search and replace);

          <?php
          $this->modx->getService('mail', 'mail.modPHPMailer');
          


          - Replace all occurences of mail.modPHPMailer to mail.modSwiftMailer
          - Save the file, refresh, clear cache...

          And, hopefuly, it works! smiley


          It might sound easy for most of you out there, i can only say that it wasn’t for me - even tho the solution is pretty basic (it’s part of the modx environment learning process i guess). Not pretending anything, i hope the above can help another user in need.

          Thanks for your help!
            • 3749
            • 24,544 Posts
            You do know that Revolution has SMTP system settings that force it to use SMTP for all mail, right? wink

            If you really need to modify the login processor, I’d suggest that you rename (if you haven’t already) it so it won’t be overwritten if you upgrade the login package.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 35291
              • 21 Posts
              Thanks for the reply and the reminder about renaming the file...

              I knew about revo’s SMTP settings but for some reasons - go figure - nothing is sent using phpmailer (modphpmailer) as opposed to Swiftmailer, wich, in my case, is a sure thing!

              I’ll still look into that matter (between modx/ajax figuring out), even tho i solved my problem; it’s all about knowledge!