We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23510
    • 168 Posts
    I'm at a loss trying to figure this out.

    I have a formit hook setup to send email but it does not work. I do, however, receive the default email from FormIt so the problem is not mail on the server.

    Here's the formIt chunk:

    [[!FormIt?
      &hooks=`spam,email,hook.emailNotify,FormItSaveForm,redirect`
      &spamEmailFields=`youremail`
      &submitVar=`contactUs-submit`
      &emailTpl=`contactUs.form.emailTpl`
      &emailSubject=`[Contact Us]`
      &emailTo=`[[++site.dev.email]]`
      &redirectTo=`144`
      &store=`1`
      &validate=`
      youremail:email:required,
      workemail:blank,
      yourname:required,
      comments:required`
    ]]
    
    form html here.
    


    Here is the hook.emailNotify

    <?php
    
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);
    $log_prefix ="CONTACT: ";
    
    $message = "
    
    Thank You for your interest in ExecuService. A Customer Service Representative will contact you within the next business day. Have a safe day!
    
    Regards,
    ExecuService Marketing
    ";
    
      $to_email = $hook->getValue('youremail');
      
      $modx->log(modX::LOG_LEVEL_INFO, $log_prefix.$to_email);
     
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,$message);
    $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
    $modx->mail->set(modMail::MAIL_FROM_NAME,'ExecuService Admin');
    $modx->mail->set(modMail::MAIL_SENDER,'ExecuService Admin');
    $modx->mail->set(modMail::MAIL_SUBJECT,'[ExecuService] Thank you!');
    $modx->mail->address('to',$to_email);
    $modx->mail->setHTML(true);
    
    $sent = $modx->mail->send();
    if ($sent) {
        $output = 'Mail sent';
    } else {
        $output = 'An error occurred while trying to send the email';
    }
    
    
    $modx->log(modX::LOG_LEVEL_ERROR,$log_prefix.$output);
    
    // $modx->mail->reset();
    
    /* tell our snippet we're good and can continue */
    return true;
    
    



    All I get is the error message in my log...'An error occurred while trying to send the email'

    Any ideas?

    This question has been answered by pixelstation. See the first response.

      • 3749
      • 24,544 Posts
      This probably won't help, but it's worth a try:

      $output = 'An error occurred while trying to send the email: ' . $modx->mail->mailer->ErrorInfo ;


      It that doesn't display anything, try this:


      $output = 'An error occurred while trying to send the email: ' . $modx->mail->getError() ;


      Also, check the MODX Error Log if you haven't already.
        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
      • discuss.answer
        • 23510
        • 168 Posts
        Thanks Bob I found the problem.

        // $modx->mail->set(modMail::MAIL_SENDER,'ExecuService Admin');
        $modx->mail->set(modMail::MAIL_SENDER,'[email protected]');
        


        MAIL_SENDER is for email addresses.

        The incorrect line...I've seen it in multiple places...was copied from the official docs but the line is now removed.
          • 3749
          • 24,544 Posts
          I'm glad you got it sorted. Thanks for reporting back. That will probably help a lot of others.
            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
            • 38017
            • 18 Posts
            Thank you pixelstation, I was pulling my hair out.

            The problem is the online documentation is incorrect. If you are following the the example given here (https://docs.modx.com/extras/revo/login/login.tutorials/login.using-pre-and-post-hooks) -> Examples -> Register custom email postHook

            You will be stuck in "why is it not working" land for a while.

            If someone could update this given example it would be extremely helpful and cause a lot less confusion!
              • 3749
              • 24,544 Posts
              @rcarnick: I you can post the incorrect part of that page and what it should be replaced with, I'll fix it.

              Better yet, you can go to this page, become a contributor, and fix it yourself. Look for the "need an account?" line below the form.
                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