-1

I would like to ask, how can i make the email i sent out to consist of two buttons which is accept or reject that when the recipient click on reject/accept, the value actually sends back to the database.

Does anyone have idea on this method ?

PHP

$subject1="E-Calibration Registration Form Approval Notice for HOD";
            $message1="Dear HOD, you currently have a pending E-Calibration Registration Form waiting for approval.";
            $to1="67508@siswa.unimas.my";
            //starting outlook        
            com_load_typelib("outlook.application"); 

            if (!defined("olMailItem")) {define("olMailItem",0);}
        
            $outlook_Obj1 = new COM("outlook.application") or die("Unable to start Outlook");
        
            //just to check you are connected.        
            echo "<center>Loaded MS Outlook, version {$outlook_Obj1->Version}\n</center>";
            $oMsg1 = $outlook_Obj1->CreateItem(olMailItem);        
            $oMsg1->Recipients->Add($to1);
            $oMsg1->Subject=$subject1;        
            $oMsg1->Body=$message1;        
            $oMsg1->Save();        
            $oMsg1->Send();    
            echo "</br><center>Email has been sent succesfully</center>";

This is my php code for my email

Wee Yaw
  • 13
  • 8
  • You shouldn't use Outlook to send email. You can use `SwiftMailer` or `PHPMailer` instead. For the buttons you mentioned, the answer by Beaton is almost correct. – Raptor Apr 09 '21 at 01:46

2 Answers2

1

So this functionality should start in the email you are sending. The email template or code should first of all be HTML to be able to accommodate the buttons. So the email mode should be HTML. These buttons can be linked to a form or direct links which denote Accept or Reject actions.

These can be the PHP file which then sends that information to the database.

<input type="submit" name="accept" value="Accept">
<input type="submit" name="accept" value="Reject">

Then in your PHP code, you can have your normal code to read these values and store them in the database.

if ($_SERVER["REQUEST_METHOD"] == "POST") {
      // collect value of input field
     $data = $_REQUEST['accept'];
     // store in DB
}
  • but what if i am using localhost with odbc and microsoft access as database ? – Wee Yaw Apr 08 '21 at 03:43
  • Nothing changes in the approach. You still need to send an email with a button and then in your PHP code you receive the input and save them in your DB. Your code just accesses the Microsoft Access database through for example PDO with a Microsoft Access driver like this `$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");` – Beaton Nyamapanda Apr 08 '21 at 14:19
  • what if my email is in php format ? @BeatonNyamapanda – Wee Yaw Apr 09 '21 at 00:11
  • @WeeYaw the tools you selected are uncommon. Try not to use MS Access as database; try more common database software like MySQL, MSSQL or SQLite instead (for better performance and support by PHP). Also, an email will not be in PHP format; it's either plain text, rich text or HTML (no PHP codes can be run in the email). – Raptor Apr 09 '21 at 01:49
  • My company is using ms access as database , i have no choice. How does the solution above by Beaton work, someone please give me an example... @Raptor – Wee Yaw Apr 09 '21 at 02:14
1

The full solution consists of multiple parts:

Part 1: Send email script

I will use PHPMailer as an example. You should refer to the links for installation details.

<?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\SMTP;
use PHPMailer\PHPMailer\Exception;

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

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     //SMTP username
    $mail->Password   = 'secret';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('sender@example.com', 'Sender');
    $mail->addAddress('67508@siswa.unimas.my');

    //Content
    $mail->isHTML(true); //Set email format to HTML
    $mail->Subject = 'E-Calibration Registration Form Approval Notice for HOD';
    
    $id = 12345;
    
    $body = '<p>Dear HOD, you currently have a pending E-Calibration Registration Form waiting for approval.</p>';
    $body.= '<p><a href="https://example.com/response.php?id=' . $id . '&answer=accept">Accept</a> <a href="https://example.com/response.php?id=' . $id . '&answer=reject">Reject</a></p>';
    $mail->Body    = $body;

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Part 2: Response script

<?php
if(!isset($_GET['id'], $_GET['answer'])) {
    die('missing parameters');
}
$id = (int)$_GET['id']; // assume ID is an integer
$answer = trim($_GET['answer']);
if(!in_array($answer, array('accept', 'reject'))) {
    die('invalid answer');
}

// TODO: Save the info to MS Access DB
// read https://stackoverflow.com/questions/19807081/how-to-connect-php-with-microsoft-access-database for the codes

?>
Raptor
  • 53,206
  • 45
  • 230
  • 366