Sending an Email within an Editor Table

Sending an Email within an Editor Table

cemlimitedcemlimited Posts: 32Questions: 8Answers: 0

Okay Guys

$aaa =Editor::inst( $db, $table );

$aaa ->on('postCreate', function ($values) {
               $from = "email@email.com"; //Change this to whatever 
        $subject = "Test Subject";
                $to = "youremail@youremail.com"; 
                $message = "Hello World";
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        
        // More headers
        $headers .= 'From: <youremail@youremail.com>' . "\r\n";
        
        mail($to,$subject,$message,$headers); 
});

$aaa->process( $_POST )->json();


When this is added, it is throwing a JSON problem

How do I manage this so that the function does not impact the JSON?

Answers

  • allanallan Posts: 56,849Questions: 1Answers: 9,035 Site admin

    Nothing immediately obvious in that code - the syntax is valid. Is the response from the server the same as we discussed by e-mail - zero length empty?

    If you check the web-server's error logs, is there anything shown there? What web-server are you using?

    Allan

  • cemlimitedcemlimited Posts: 32Questions: 8Answers: 0
    edited July 1

    Okay - So I have identified that the Code is not causing an error until i try to use the information posted within the $message.

    I then used $vals = $_POST['action'];

    and used $vals within the message and it worked...returning the "Create" value.....

    So the question is how to access the submitted values.....

    Tried $vals = $_POST['values] and then made sure to have them within the USE() section of the function.....It simply returns 1 when I use print_r($vals)

    '''

    $vals = $_POST['values'];

    $result2 = $db->prepare($fieldquery);
    $result2->execute();

    foreach ($result2 as $row) {
    $fields[] =$row['Field'];
    }

    $excludes = array("created_by","updated_by","created_date","accepted_date","completed_date","updated_date");

    $selects = array("task","skill","action");
    $selectstable = array("task","skills","registration");
    $selectssource = array("id","id","id");
    $selectslabels = array("task","skill","username");
    $hyperlinks = array("id");

    $dates = array("target_date");

    $boolean = array("fee");

    $range = array("skill_level");

    // DataTables PHP library
    include( $_SERVER["DOCUMENT_ROOT"]."/DTEditor/lib/DataTables.php" );

    $db->sql("SET names 'utf8'");

    // Alias Editor classes so they are easy to use
    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

    // Build our Editor instance and process the data coming from _POST

    $aaa =Editor::inst( $db, $table );
    $aaa->where('brief', $brief);

    foreach(array_diff($fields,$excludes, $selects, $dates, $boolean) as $field){
    $aaa ->field(Field::inst( $table.".".$field));
    }

    $cycle = 0;

    foreach($selects as $select){
    $aaa ->field(Field::inst($table.'.'.$select)->options( Options::inst()->table( $selectstable[$cycle] )->value( $selectssource[$cycle] )->label( $selectslabels[$cycle] ))->validator( Validate::dbValues()));
    $aaa ->field(Field::inst( $selectstable[$cycle].'.'.$selectslabels[$cycle]));
    $aaa ->leftJoin( $selectstable[$cycle],$selectstable[$cycle].'.'.$selectssource[$cycle], '=', $table.'.'.$select );
    $cycle = $cycle + 1;
    }

    foreach($dates as $date){
    $aaa ->field(Field::inst($table.'.'.$date)->validator( Validate::dateFormat('Y-m-d'))->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )->setFormatter( Format::dateFormatToSql('Y-m-d' ) ));
    }

    foreach($boolean as $boo){
    $aaa ->field(Field::inst($table.'.'.$boo)->setFormatter( function ( $val, $data, $opts ) {return ! $val ? 0 : 1;} ));
    }

    $aaa->on('postCreate', function ($editor, $id, $values, $row) use ($briefname, $vals, $brief, $projectname)  {
        $from = "admin@email.com"; 
        $subject = "AscentXD registration";
        $to = "email@email.com"; 
    
        $message = "
        <html>
        <head>
        <title>AscentXD</title>
        </head>
        <body>
        <left> 
            Congratulations!<br> <br>
            A New task has been created on <br><br>".$projectname."<br>".print_r($vals)."<br>".$briefname.".<br><br> Access Here: <a href='http://subdomain.domain.com/site/brief.php?brief_id=".$brief."'>subdomain.domain.com</a> for your attention<br> <br> 
            <br>Kind Regards, <br><br>Ascent Admin<br><br>
    
            </left> 
    
        </body>
        </html>
        ";
    
        //$message = print_r($values);
    
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    
        // More headers
        $headers .= 'From: <email@email.com>' . "\r\n";
    
        mail($to,$subject,$message,$headers); 
    
        return '';
    } );
    

    $aaa->process( $_POST )->json();
    '''

  • cemlimitedcemlimited Posts: 32Questions: 8Answers: 0

    Sorry - Dont know why it is kicking the last string outside the '''

  • cemlimitedcemlimited Posts: 32Questions: 8Answers: 0

    Okay All.....

    I managed to resolve the matter by iterating through the complete Post info...

    The information from the submission is 3 levels down....

    $vals = array();
    foreach ($_POST as $key => $value) {
        foreach ($value as $key1 => $value1) {
            foreach ($value1 as $key2 => $value2) {
                foreach ($value2 as $key3 => $value3) {
                    $vals[$key3] =  $value3;
                }
            }
        }
    }
    
  • allanallan Posts: 56,849Questions: 1Answers: 9,035 Site admin

    You need to use a back tick for the syntax highlighting, rather than an apostrophe.

    It might have clicked for me why it wasn’t working before - are you using joined tables here - i.e. field names with table.column syntax? If so, in your postCreate` event handler you’d need to use:

    $values[‘table][‘field’]
    

    Rather than just $values[‘field’]. I’m not certain about that, but guessing due to the triple loop you have there. Hopefully, if that works, it should make the code a lot cleaner!

    Allan

Sign In or Register to comment.