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

    Firstly my apologies if this is going in the wrong place...mods feel free to move it if necessary.

    So I have a snippet that takes input from a contact form and then sends it to a few people:

    <?php
    ini_set('display_errors',1);  
    error_reporting(E_ALL);
    
    $tpl;
    $mailerTpl;
    $ty;
    
    if(isset($_POST['submit'])){
        $subjectline = 'Contact made at Website';
        $emailfrom = '[email protected]';
        $message = $modx->getChunk($mailerTpl, array(
            'name' => $_POST['inputName'],
            'email' => $_POST['inputEmail'],
            'subject' => $_POST['inputSubj'],
            'message' => $_POST['inputMessage'],
    	));
    	$modx->getService('mail', 'mail.modPHPMailer');
        $modx->mail->set(modMail::MAIL_BODY,$message);
        $modx->mail->set(modMail::MAIL_FROM,$emailfrom);
        $modx->mail->set(modMail::MAIL_FROM_NAME,'Website Owner');
        $modx->mail->set(modMail::MAIL_SUBJECT,$subjectline);
        $modx->mail->address('to','[email protected]');
        $modx->mail->setHTML(true);
        if (!$modx->mail->send()) {
    	    $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
        }
        $modx->mail->reset();
        //redirect to thank you page after successful send
        $url = $modx->makeUrl($ty);
        $modx->sendRedirect($url);
    }else{
        $output = $modx->getChunk($tpl);
    }
    
    return $output;
    


    The thing is...this site was built a few years ago...its modx 2.2 I believe (where do you go to get the version number of the current install? I cant find it). Anyways...it was working fine until recently it stopped working fine.

    I am getting the error: [2017-05-14 14:16:52] (ERROR @ /index.php) An error occurred while trying to send the email: SMTP connect() failed.

    A part of me thinks that it is something to do with the host but I dont know if that connect() function is related to MODX or not?

    I have tried enabling SMTP but this isnt my website and the person I am building it for doesnt seem to know the details I need to make an SMTP connection.

    Any thoughts / suggestions are more than welcome...thanks in advance!

    Matt
      • 3749
      • 24,544 Posts
      You have my sympathy. I think there's nothing I hate more than trying to straighten out email issues.

      It's common for hosts to simply turn off the PHP mail() function without notifying you.

      You might try installing the QuickEmail extra. It's meant to help diagnose email problems, though they often remain mysterious.

      With respect to SMTP (which I generally find to be more reliable), it's possible that the host has changed the SMTP server name, port, or prefix. They should have a Knowledge Base article or community support post giving the correct values.

      FYI, you can send thousands of emails per month for free through Mailgun. The Notify extra has a MailgunX class which extends the Mailgun class you can get with Composer.

      With Mailgun, you not only get lots of stats, you also get better deliverability and are less likely to have messages end up in the spam folder because of their reputation.

      You're welcome to use the code.
        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
        • 42596
        • 45 Posts
        It seems a bit long winded but to be honest at this point if it works then I'll take it haha...thanks for the advice! I'll give mailgun a try!
          • 42596
          • 45 Posts
          When you say get the mailgun class through composer do you mean from the terminal? This site is already online and I cant ssh in im afraid. Is there another way to add this?? If so how? Do I just get the class and add it as a snippet? What then?

          Thanks again!
            • 3749
            • 24,544 Posts
            There's some info on getting the Mailgun files without composer here: https://github.com/BobRay/notify/blob/master/core/components/notify/vendor/mailgun/mailgun-php/SharedHostInstall.md.

            I used composer on my local machine and put the files in the Notify package.

            You might be able to install the Notify and Subscribe extras and reference the files there in your code. I think the mailgun and mailgunX files there will work independently of Notify/Subscribe, but I wouldn't swear to it.


              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