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

    I'm sure that I'm missing something simple. I would like to change my snippet so that the I can use fields from the array to fill in the email (i.e. MAIL_FROM = webc_name; MAIL_BODY = webc_message; etc). Below is a copy of the snippet.

    Thank you for your help.

    <?php
    /* CreateQuote Snippet */
     
    if (isset($_POST['submit']) &&
        ($_POST['submit'] == 'Submit')) {
        /* Form has been submitted */
     
        /* You might want to check for empty
         * required fields here */
    
        /* We need to load our class so newObject() will work */
        $path = MODX_CORE_PATH . 'components/webc/';
        $result = $modx->addPackage('webc',$path .
            'model/','eswc_');
        if (! $result) {
            return 'Failed to add package';
        }
     
        $fields = array(
            'webc_name' => $_POST['webc_name'],
            'webc_email' => $_POST['webc_email'],
            'webc_company' => $_POST['webc_company'],
    		'webc_phone' => $_POST['webc_phone'],
    		'webc_message' => $_POST['webc_message'],
        );
        $webc = $modx->newObject('webcontact', $fields);
        if ( $webc->save()) {
    	$from = $modx->getOption('email_noreply');
     	 	$modx->getService('mail', 'mail.modPHPMailer');
    		$modx->mail->set(modMail::MAIL_BODY, 'You have a new web message');
    		$modx->mail->set(modMail::MAIL_FROM,$from);
    		$modx->mail->set(modMail::MAIL_FROM_NAME,$from);
    		$modx->mail->set(modMail::MAIL_SUBJECT,'E-mail from website');
    		$modx->mail->address('to','[email protected]');
    		$modx->mail->address('reply-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();
            $output = "<b>Thank you for your message - we'll follow up with you soon!</b>";
        } else {
            $output = "Error";
        }
     
    } else {
        /* Not a repost, just display the form */
        $output = $modx->getChunk('webcontactTpl');
    
    }
    return $output;


      • 3749
      • 24,544 Posts
      I'm not sure this will solve your problem, but sometimes you have to create the object before you set its fields:

      $webc = $modx->newObject('webcontact');
      $webc->set('webc_name', $_POST['webc_name']);
      // etc.


      Also, what xPDO object does your webcontact object extend?
        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