2017-01-31 2 views
2

Мне нужна помощь в том, как отправить электронное письмо с использованием данных. Я хочу отправить электронное письмо на адрес электронной почты в столбце EMAIL, если значение в столбце STATUS изменится на COMPLETED.Datatables отправить электронное письмо по адресу edit

<?php 

/* 
* Editor server script for DB table vehicles 
*/ 

// DataTables PHP library and database connection 
include("lib/DataTables.php"); 
session_start(); 
// 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; 

// Build our Editor instance and process the data coming from _POST 
Editor::inst($db, 'vehicles') 
    ->fields(
    Field::inst('vehicles.email') 
      ->options(Options::inst() 
      ->table('users') 
      ->value('userId') 
      ->label('email') 
       ) 
       ->validator('Validate::dbValues'), 
    Field::inst('users.email'), 
    Field::inst('vehicles.name') 
      ->options(Options::inst() 
      ->table('users') 
      ->value('userId') 
      ->label('name') 
       ) 
       ->validator('Validate::dbValues'), 
    Field::inst('users.name'), 
    Field::inst('vehicles.stock'), 
    Field::inst('vehicles.make'), 
    Field::inst('vehicles.model'), 
    Field::inst('vehicles.color'), 
    Field::inst('vehicles.year'), 
    Field::inst('vehicles.service') 
      ->options(Options::inst() 
      ->table('services') 
      ->value('service_id') 
      ->label('service') 
      ) 
      ->validator('Validate::dbValues'), 
    Field::inst('services.service'),  
    Field::inst('vehicles.due') 
     ->validator('Validate::dateFormat', array(
      'empty' => false, 
      'format' => 'm-d-Y g:i A' 
      )) 
       ->getFormatter('Format::datetime', array(
      'from' => 'Y-m-d H:i:s', 
      'to' => 'm-d-Y g:i A' 
      )) 
      ->setFormatter('Format::datetime', array(
      'from' => 'm-d-Y g:i A', 
      'to' => 'Y-m-d H:i:s' 
      )), 
    Field::inst('vehicles.notes'), 
    Field::inst('vehicles.status') 
      ->options(Options::inst() 
      ->table('status') 
      ->value('status_id') 
      ->label('status') 
      ) 
      ->validator('Validate::dbValues'), 
    Field::inst('status.status'),  
    Field::inst('vehicles.detailer') 
      ->options(Options::inst() 
        ->table('detailers') 
        ->value('detailer_id') 
        ->label('detailer_name') 
        ) 
      ->validator('Validate::dbValues'), 
    Field::inst('detailers.detailer_name'),  
    Field::inst('vehicles.comments') 
    ) 
    ->leftJoin('users', 'users.userId', '=', 'vehicles.name') 
    ->leftJoin('services', 'services.service_id', '=', 'vehicles.service') 
    ->leftJoin('status', 'status.status_id', '=', 'vehicles.status') 
    ->leftJoin('detailers', 'detailers.detailer_id', '=', 'vehicles.detailer') 
    ->where(function ($q) { 
    $q->where('due', 'DATE_ADD(NOW(), INTERVAL -1 DAY)', '>=', false); 
    }) 
     ->on('preEdit', function ($editor, $values) { 
      $editor 
       ->field('vehicles.email') 
       ->setValue($_SESSION['user']); 
     }) 
    ->process($_POST) 
    ->json(); 

Я знаю, что мне нужно использовать «preEdit» в следующей части, но точно не знаю, как.

->on('preEdit', function ($editor, $values) { 
     $editor 
      ->field('vehicles.email') 
      ->setValue($_SESSION['user']); 
    }) 

Thanks.

+1

Хотя я против построения запросов классов, это один берет его на вершине. Не было бы проще написать sql самостоятельно? – Xorifelse

ответ

0

Предлагаем Вам попробовать

->on('postEdit', function ($editor, $id, $values, $row) { 
    if ($values['status'] === "completed") { 
     my_mail_function(); 
    } 
}) 

где my_mail_function() функция PHP вы определили для отправки электронной почты. Например, если вы должны были использовать phpMailer библиотеки вашего mail_function() может быть установлен как:

<?php 
// Import PHPMailer classes into the global namespace 
// These must be at the top of your script, not inside a function 
use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

//Load composer's autoloader 
require 'vendor/autoload.php'; 

function my_mail_function(){ 

$mail = new PHPMailer(true);        // Passing `true` 

    //Server settings 
    $mail->SMTPDebug = 2;         // Enable verbose debug output 
    $mail->isSMTP();          // Set mailer to use SMTP 
    $mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers 
    $mail->SMTPAuth = true;        // Enable SMTP authentication 
    $mail->Username = '[email protected]';     // SMTP username 
    $mail->Password = 'secret';       // SMTP password 
    $mail->SMTPSecure = 'tls';       // Enable TLS encryption, `ssl` also accepted 
    $mail->Port = 587;         // TCP port to connect to 

    //Recipients 
    $mail->setFrom('[email protected]', 'Mailer'); 
    $mail->addAddress('[email protected]', 'Joe User');  // Add a recipient 
    $mail->addAddress('[email protected]');    // Name is optional 
    $mail->addReplyTo('[email protected]', 'Information'); 
    $mail->addCC('[email protected]'); 
    $mail->addBCC('[email protected]'); 

    //Attachments 
    $mail->addAttachment('/var/tmp/file.tar.gz');   // Add attachments 
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name 

    //Content 
    $mail->isHTML(true);         // Set email format to HTML 
    $mail->Subject = 'Here is the subject'; 
    $mail->Body = 'This is the HTML message body <b>in bold!</b>'; 
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 

    $mail->send(); 
} 
Смежные вопросы