我每天在浏览器上调用此PHP页面发送报告。它(经常)发送两次电子邮件(即使我确保每次都打开一个新标签)。
代码有什么问题+如何防止它?
以下是代码:
<?php
require ("/home/phpmailer/PHPMailer-master/PHPMailerAutoload.php");
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'USERNAME@DOMAIN.com'; // SMTP username
$mail->Password = 'PASSWORD'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'FROM-NAME@DOMAIN.com';
$mail->FromName = 'FROM NAME';
$mail->ClearAddresses();
$mail->addAddress('email1@ABC.com', 'CLARA'); // Add a recipient
$mail->addCC('email@@ABC.com', 'TOM'); // Add a CC recipient
$mail->addReplyTo('email2@ABC.com', 'Info');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'EMAIL SUBJECT TITLE';
$mail->Body = file_get_contents('http://ADDRESS-OF-THE-FILE.PHP');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
$mail->ClearAddresses();
}
?>
答案 0 :(得分:1)
正如SmartyCoder在评论中所说的那样。
如果您确定自己是唯一一个点击它的人,那么您可以尝试使用快速和脏的方式跟踪Cookie,例如:
// See if a cookie is set, and if so, compare it to today
// If cookie value == today, die() - stop executing
if ( isset( $_COOKIE['email_reports_lastsent'] ) &&
$_COOKIE['email_reports_lastsent'] == date('Y-m-d') ) die();
// Set the cookie as today's date
setcookie( 'email_reports_lastsent', date('Y-m-d') );
如果其他设备/用户正在访问您的脚本,则不会解决任何问题。它还要求您使用相同的浏览器发送,并且您不能使用隐身或其他隐私浏览选项卡。
答案 1 :(得分:0)