数据表在编辑时发送电子邮件

时间:2017-01-31 23:00:45

标签: php email datatables

我需要一些关于如何使用数据表发送电子邮件的帮助。 如果STATUS列中的值更改为COMPLETED,我希望将电子邮件发送到EMAIL列中的电子邮件地址。

<?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();

我知道我需要使用&#34; preEdit&#34;在下面的部分中,但并不确切知道如何。

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

感谢。

1 个答案:

答案 0 :(得分: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 = 'user@example.com';                 // 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('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //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();
}