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

    I'm having trouble including the PHPMailer class in my FormIt hook.
    This is what I'm using:
    require_once($modx->config['base_path']."PHPMailer/class.phpmailer.php");

    but it doesn't work.

    (For anyone wondering why I would want to include PHPMailer in Formit, I need a hook which generates a pdf using TCPDF and then mails it out.
    I have a nice script already which does this using PHPMailer and I thought I'd use that. If anyone has any better ways I'd love to hear them!)

    Thanks in advance.
      • 16348
      • 64 Posts
      I'm working with a script that should do the same thing but I have a bit to go until it is finished.
      Look at the FormIt email-hook to see how it is done there.

      You can load PHPMailer with this command:
      /* load mail service */
      $modx->getService('mail', 'mail.modPHPMailer');
      


      and then use syntax like this to send the mail:
      /* set email main properties */
      $modx->mail->set(modMail::MAIL_BODY, $message);
      $modx->mail->set(modMail::MAIL_FROM, $emailFrom);
      $modx->mail->set(modMail::MAIL_FROM_NAME, $emailFromName);
      $modx->mail->set(modMail::MAIL_SENDER, $emailFrom);
      $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
      $modx->mail->address('to', $emailTo, $emailToName);
      /* send email */
      $sent = $modx->mail->send();
      if (!$sent) {
        $modx->log(modX::LOG_LEVEL_ERROR, '[Error sending email] ' . print_r($modx->mail->mailer->ErrorInfo, true));
      }
      


      you can also use all functions from PHPMailer in this way:
      $modx->mail->mailer->AnyPHPMailerFunction();
      
        • 38359
        • 93 Posts
        Thanks Kristoffer, that seems to work!
        Are you going to release it as a module?
          • 38359
          • 93 Posts
          Hmm, actually I can include PHPMailer but when I run the whole script it fails.
          Is modMail:: correct for this example?
            • 38359
            • 93 Posts
            Scratch that, got it working now, thanks.
              • 16348
              • 64 Posts
              Great!
              For right now, my solution is very specific and the PDF creation/handling is a small part of a big component so I have no plan to release it as a component right now.
              I think that FormIt is missing a way to add an attachment in a post hook, and I'm thinking about implementing a way to do this.
                • 38359
                • 93 Posts
                That'd be sweet. Anyway I've got it working now and can send pdfs to my heart's content!

                Thanks again
                  • 3749
                  • 24,544 Posts
                    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
                    • 16348
                    • 64 Posts
                    I have looked at that thread but there is no answer to the question in the first post. And the question is asked again in the last posts in the thread.
                    The e-mail hook in FormIt is designed in a way that it is hard (impossible?) to add an attachment in a custom hook, i.e. a pdf file that is created on the fly upon form submission.
                    One way to do it is to store the file in the file system and then add that file to the files array but I want to use the method in PHPMailer (AddStringAttachment) to attach a file from a string.