We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33657
    • 128 Posts
    So I have my own php mailer that I am using (mail.php) and in my file I have

    $email = '[email protected]';
    


    How can i pull an email from a template variable? or setup a way so that Me, or a client, can set the email address used in $email inside the MODX manager rather than having to edit the mail.php file?

      • 32316
      • 387 Posts
      Create a TV - lets say you call it 'emailaddress'
      Put your mail.php into a snippet - lets say you call it 'mailer'

      Now call your mailer from the page with:
      [[mailer? &email=`[[*emailaddress]]`]]


      $email in the mailer snippet will be whatever you enter in the emailaddress TV for the page
        • 33657
        • 128 Posts
        unfortunetally the mail.php file is called from a javascript file.

        I do alot of Javascript checks on my forms and animate them (colors and fading and cool effects) and then i do a AJAX Request for mail.php

        now if i could figure out how to put mail.php into a resource that would be cool but i already tried to add a new mime type, php, add the file into a new resource, and then call it from the alias .php but it doesn't work...

        i was hoping of a way to have my php file literally pull the variable from modx... like a get object or something
          • 33968
          • 863 Posts
          You could either:

          - put your mail code in a snippet, then place that snippet call in a new modx resource; or
          - include the following code in your mail.php file to get access to MODx 'from outside'
          <?php
          define('MODX_CORE_PATH', 'full/path/to/your/modx/core/');
          define('MODX_CONFIG_KEY', 'config');
          require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
          $modx = new modX();
          $modx->initialize('web');
          


          I guess you'll know how to pass the page id to your script; from there you can use something like this to get the TV value:
          $id = $_POST['id'];
          $resource = $modx->getObject('modResource',$id);
          $email = $resource->getTVValue('email_tv');
          
            • 33657
            • 128 Posts
            if i make a file called echo.php and i run it with this code

            <?php
            
            define('MODX_CORE_PATH', '/home/mysite/public_html/chsla/core/');
            define('MODX_CONFIG_KEY', 'config');
            require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
            $modx = new modX();
            $modx->initialize('web');
            
            $id = $_POST['id'];
            $resource = $modx->getObject('modResource',$id);
            $email = $resource->getTVValue('email_tv');
            
            echo $email;
            
            ?>
            


            why won't that work

            this must just be above me. i am not using POST and GET for my form. I am doing an ajax request like so..

            			$.post("/mail.php", {action: "sendMail", name: frmName , mail: frmMail,  phone: frmPhone, company: frmCompany, message: frmMessage},
            			function(data){
            				if (data.success == '1'){
            					success();
            					//If the mail was sent show the "success" message
            					return false;
            				
            				}else{
            
              • 33968
              • 863 Posts
              This... $.post() ...is a POST smiley

              I was just giving an example, but now I've seen your ajax call you could just do this:
              $.post("/mail.php", {action: "sendMail", email: [[*email_tv]], ... etc},
              

              That way you can send the email address without needing to look it up from the receiving script.
                • 33657
                • 128 Posts
                ack my message got deleted.

                here is the javascript file
                http://pastie.org/2445077


                and here is the mail.php file
                http://pastie.org/2445069

                perhaps that helps? maybe someone can make some sense of it. id like to replace $email in my mail.php file with a user defined template variable that they can edit on the contact page... so that way a client doesn't have to go in and modify a php file just to set a new email. anyone have any ideas?
                  • 3749
                  • 24,544 Posts
                  It looks like you just need the ID of the current resource so the code below will work. How about putting a hidden input field in your form with [[*id]] as the value?

                  Then this code should work:

                  $id = $_POST['id'];
                  $resource = $modx->getObject('modResource',$id);
                  $email = $resource->getTVValue('email_tv');
                   
                  echo $email;
                    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
                    • 33968
                    • 863 Posts
                    Or just post the TV value itself and avoid having to load modx in your mailer script.

                    Use a hidden field in your form:
                    <input type="hidden" name="email" value="[[*email_tv]]" id="contact_email_tv" >
                    

                    In your js, change this section as follows:
                    // FORM SUBMISSION
                      $('.button.btSend.css3button').click(function(){
                      //Get name field value
                          var frmName = $('#contact_name').val();
                          
                          //Get e-mail field value
                          var frmMail = $('#contact_email').val();
                     
                          //Get phone field value
                          var frmPhone = $('#contact_phone').val();
                          
                          //Get company field value
                          var frmCompany = $('#contact_company').val();
                          
                          //Get textarea message
                          var frmMessage = $('#contact_message').val();
                    
                          // ADDED: Get hidden email_tv field value
                          var frmEmail = $('#contact_email_tv').val();
                          
                          //Send data using ajax
                          $.post("/mail.php", {action: "sendMail", name: frmName , mail: frmMail,  phone: frmPhone, company: frmCompany, message: frmMessage, email: frmEmail},  // <--- added frmEmail
                    

                    Then in your php file you can the email tv value with:
                    $email = $_POST['email'];
                    

                    I might have missed something in your js file but pretty sure that should do it...

                    Note: my suggestion in this post would work fine if your js was embedded instead of in an external file (hadn't realised that was the case)
                      • 33657
                      • 128 Posts
                      ok thanks, got that to work. didn't even try the external method, just embedded it. I appreciate it.

                      Bob your code didn't show up as what i should put as the value of the id or etc? the solution below bobs worked but I am always interested in what he has to say because I respect your opinion on things Modx smiley love your getMyCompoenent, etc..

                      what I would really LOVE is the ability to use FormIt and do some jquery animations and effects over it and the responses.. i guess after this project I will revisit formit and see if i can do something cosmetic first before the submit.. im sure its probably possible. [ed. note: zurie last edited this post 15 years, 1 month ago.]