`标题:如何根据 PHP 和 MySQL 中的用户输入在没有 Cron 作业的情况下安排电子邮件
身体:
我正在使用 HTML、CSS、JavaScript、PHP、AJAX 和 MySQL 开发一个项目。该项目包括一个包含四个字段的表单:日期、时间、注释和电子邮件,以及一个提交按钮。管理员使用该表格来提醒客户有关他们的保险单的信息。
当管理员填写表格并单击提交时,数据将存储在数据库中,并应在指定的日期和时间向客户发送电子邮件。电子邮件安排应支持所有时区,因为我们的客户位于美国。
要求:我需要在不使用 cron 作业来安排电子邮件的情况下实现此目的。
问题:如何在不使用 PHP 中的 cron 作业的情况下在指定的日期和时间安排并发送电子邮件?
对此的任何帮助或指导将不胜感激。谢谢!
<?php
// Include your config file
include('config.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
// Function to send email
function sendEmail($to, $subject, $message) {
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Replace with your Gmail username
$mail->Password = 'mzusspapcjrvezwy'; // Replace with your Gmail password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('[email protected]');
$mail->addAddress($to); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
echo 'Email has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
if (isset($_POST['submit'])) {
// Retrieve form data
$date = $_POST['date'];
$notes = $_POST['notes'];
$email = $_POST['email'];
$time = $_POST['time'];
// Insert data into the database
$query = "INSERT INTO prospect (date, notes, email, time, email_sent) VALUES (?,?,?,?)";
mysqli_query($conn, $query);
// Get current datetime
date_default_timezone_set('Asia/Kolkata');
// date_default_timezone_set('UTC');
$current_datetime = date('Y-m-d H:i:s');
// Calculate the time difference between current datetime and entered datetime
$scheduled_datetime = $date . ' ' . $time;
$time_diff = strtotime($scheduled_datetime) - strtotime($current_datetime);
if ($time_diff > 0) {
// If the scheduled datetime is in the future, schedule the email to be sent later
echo "<script>scheduleEmail('$email', '$time', '$date');</script>";
echo
"<div class='alert alert-success alert-dismissible fade show'>
Remainder added successfully!
<button type='button' class='btn-close' data-bs-dismiss='alert'></button>
</div>";
?>
<script>
setTimeout(function () { window.location.href= 'Prospect_notes.php?';}, 1000);
</script>
<?php
} else {
echo "Invalid date/time entered or date/time already passed.";
}
}
?>
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Admin | Dashboard</title>
<!-- Google Font: Source Sans Pro -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
<!-- Font Awesome -->
<link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<!-- Tempusdominus Bootstrap 4 -->
<link rel="stylesheet" href="plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
<!-- iCheck -->
<link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css">
<!-- JQVMap -->
<link rel="stylesheet" href="plugins/jqvmap/jqvmap.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="dist/css/adminlte.min.css">
<!-- admin dashboard css-->
<link rel="stylesheet" href="dist/css/admin-dashboard.css">
<!-- overlayScrollbars -->
<link rel="stylesheet" href="plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
<!-- Daterange picker -->
<link rel="stylesheet" href="plugins/daterangepicker/daterangepicker.css">
<!-- summernote -->
<link rel="stylesheet" href="plugins/summernote/summernote-bs4.min.css">
<link rel="icon" href="../images/favicon.ico" type="image/x-icon" />
<script>
function showAlert(message) {
alert(message);
}
</script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function scheduleEmail(email, time, date) {
$.ajax({
type: 'POST',
url: 'send_email1.php',
data: {
email: email,
time: time,
date: date,
notes: notes
},
success: function(response) {
console.log(response);
}
});
}
</script>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
<nav class="main-header navbar navbar-expand navbar-white navbar-light">
<!-- Left navbar links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
</li>
</ul>
</nav>
<!-- /.navbar -->
<!-- Main Sidebar Container -->
<aside class="main-sidebar sidebar-dark-indigo elevation-4">
<!-- Brand Logo -->
<a href="" class="brand-link">
<!-- <img src="dist/img/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3" style="opacity: .8"> -->
<span class="brand-text font-weight-light">Admin Dashboard</span>
</a>
<!-- Sidebar -->
<div class="sidebar">
<!-- Sidebar Menu -->
<nav class="mt-4">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->
<li class="nav-item">
<a href="index" class="nav-link">
<i class="nav-icon fas fa-tachometer-alt"></i>
<p>
Main Dashboard
</p>
</a>
</li>
<li class="nav-item">
<a href="salesform" class="nav-link ">
<i class="nav-icon fa fa-check-square"></i>
<p>
Sales Form
</p>
</a>
</li>
<li class="nav-item">
<a href="generalproduction" class="nav-link ">
<i class="nav-icon fa fa-industry"></i>
<p>
General Production
</p>
</a>
</li>
<li class="nav-item">
<a href="admin" class="nav-link ">
<i class="nav-icon fas fa-user-circle"></i>
<p>
Setup Users Admin
</p>
</a>
</li>
<li class="nav-item">
<a href="company" class="nav-link ">
<i class="nav-icon fas fa-building"></i>
<p>
Companies We Sell For
</p>
</a>
</li>
<li class="nav-item">
<a href="leadsource" class="nav-link ">
<i class="nav-icon fas fa-quote-left"></i>
<p>
Lead Sources
</p>
</a>
</li>
<li class="nav-item">
<a href="" class="nav-link ">
<i class="nav-icon fas fa-quote-left"></i>
<p>
Producer Sales Report
</p>
</a>
</li>
<li class="nav-item">
<a href="prospect.php" class="nav-link nav-link-width active">
<i class="nav-icon fas fa-calendar-check"></i>
<p>
Customer Action Reminder
</p>
</a>
</li>
<li class="nav-item">
<a href="logout" class="nav-link">
<i class="nav-icon fas fa-power-off"></i>
<p>
Logout
</p>
</a>
</li>
</ul>
</nav>
<!-- /.sidebar-menu -->
</div>
<!-- /.sidebar -->
</aside>
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Prospects</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="index">Main Dashboard</a></li>
<li class="breadcrumb-item active">Prospects</li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content ">
<!-- Form -->
<form enctype="multipart/form-data" method="post" id="emailForm">
<div class="row">
<div class="col-md-12">
<div class="card card-yellowclr">
<div class="card-header">
<h3 class="card-title"></h3>
</div>
<div class="card-body">
<div>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="form-group">
<label>Date</label>
<input type="date" class="form-control" value="" required="" name="date" min="1900-01-01" max="2099-12-31" id="date">
</div>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="form-group">
<label>Notes</label>
<textarea id="notes" name="notes" class="form-control" id="notes"></textarea>
</div>
<!-- /.form-group -->
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" value="" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" name="email" id="email">
</div>
<!-- /.form-group -->
</div>
<!-- /.col -->
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="form-group">
<label>Time</label>
<input type="time" class="form-control" value="" name="time" id="time">
</div>
<!-- /.form-group -->
</div>
<!-- /.col -->
</div>
</div>
<div class="row pb-lg-4 m-2">
<div class="col-12">
<a href="prospect.php" class="btn btn-secondary">Back</a>
<input type="submit" name="submit"value="Submit" class="btn btn-success float-right" onclick="submitForm()">
</div>
</div>
<!-- col-->
</div>
</div>
<!-- /.card-body -->
<!-- <div class="card-footer ">
<div class="row">
<div class="col-12">
<a href="index" class="btn btn-secondary">Back</a>
<input type="submit" value="submit" name="submit" class="btn btn-success float-right">
</div>
</div>
</div> -->
</div>
<!-- /.card -->
</div>
</div>
</form>
</section>
<!-- /.content -->
</div>
</div>
<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<script>
$.widget.bridge('uibutton', $.ui.button)
</script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- ChartJS -->
<script src="plugins/chart.js/Chart.min.js"></script>
<!-- Sparkline -->
<script src="plugins/sparklines/sparkline.js"></script>
<!-- JQVMap -->
<script src="plugins/jqvmap/jquery.vmap.min.js"></script>
<script src="plugins/jqvmap/maps/jquery.vmap.usa.js"></script>
<!-- jQuery Knob Chart -->
<script src="plugins/jquery-knob/jquery.knob.min.js"></script>
<!-- daterangepicker -->
<script src="plugins/moment/moment.min.js"></script>
<script src="plugins/daterangepicker/daterangepicker.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Summernote -->
<script src="plugins/summernote/summernote-bs4.min.js"></script>
<!-- overlayScrollbars -->
<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="dist/js/pages/dashboard.js"></script>
</body>
</html>
<script>
document.onkeydown = function(e) {
if(event.keyCode == 123) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)) {
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)) {
return false;
}
}
</script>
<script>
function submitForm() {
var email = $('[name="email"]').val();
var date = $('[name="date"]').val();
var time = $('[name="time"]').val();
var notes = $('[name="notes"]').val();
$.ajax({
type: 'POST',
url: 'send_email1.php',
data: $('#emailForm').serialize(),
success: function(response) {
console.log(response);
}
});
}
</script>
在您的网页中包含一个 PHP 脚本,用于验证您上次发送自动电子邮件的时间。如果已经过去了超过 x 次(您的 cron 间隔),请执行您的发送提醒脚本。
它确实需要有人(或机器人)请求您的页面,但这是您无需实际操作即可完成 cron 作业的结束。