PHP发送带有按钮的电子邮件,提示收件人回答并将值发送回实际数据库



我想问,我如何使我发送的电子邮件由两个按钮组成,即接受或拒绝,当收件人单击拒绝/接受时,该值实际上会发送回数据库。

有人知道这个方法吗?

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>";

这是我的电子邮件的php代码

完整的解决方案由多个部分组成:

第1部分:发送电子邮件脚本

我将以PHPMailer为例。有关安装详细信息,请参阅链接。

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
//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}";
}

第2部分:响应脚本

<?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
?>

因此,此功能应在您发送的电子邮件中启动。电子邮件模板或代码首先应该是HTML,以便能够容纳按钮。所以电子邮件模式应该是HTML。这些按钮可以链接到表示接受或拒绝操作的表单或直接链接。

这些可以是PHP文件,然后将信息发送到数据库。

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

然后在PHP代码中,您可以使用普通代码读取这些值并将其存储在数据库中。

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$data = $_REQUEST['accept'];
// store in DB
}

最新更新