所以我一直致力于一个项目,我有一个功能,可以向选择雇主的员工发送电子邮件。现在这个工作除了邮件功能(我使用winhost,我们需要包含Mail.php以便邮件功能正常工作)它有时会发送3封电子邮件而不是2封,有时是1封电子邮件而不是2封。
代码:
if (isset($_POST['openemailmem'])){
$memberuser = $_POST['openemailmemusername'];
$sql = "SELECT email, username, password, status FROM csvdata WHERE memberview =:user ";
$getinfo=$DBH->prepare($sql);
$getinfo->execute(array(':user' => $memberuser));
while ($row = $getinfo->fetch(PDO::FETCH_ASSOC)) {
$check = $row;
$newEmployeeEmail = $check['email'];
$csvusername = $check['username'];
$password = $check['password'];
$status = $check['status'];
if ($status == "Open"){
echo "tesing";
$from = "the email of where it is coming from is here but i removed";
$to = $newEmployeeEmail;
if (!empty($_POST['cc'])){
$cc = $_POST['cc'];
}
if (!empty($_POST['ccsend'])){
$cc = $_POST['ccsend'];
$to .= ", $cc";
}
$subject = "removed msg";
$body = "removed msg";
$host = "i removed";
$username = "i removed";
$password = "i removed";
$headers = array ('From' => $from, 'To' => $to,'Cc' => $cc, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
}
}
header("Location: I removed this.php?getmsg=12");
exit;
}
感谢您的所有时间!!!
答案 0 :(得分:1)
您的解决方案最有可能出现在您的日志中。如果抛出异常,您可以在那里找到它们。
另外一个建议:
在循环之前和之后添加日志记录。所以你的例子就变成了:
if (isset($_POST['openemailmem']))
{
$memberuser = $_POST['openemailmemusername'];
$sql = "SELECT email, username, password, status "
. "FROM csvdata WHERE memberview =:user ";
$getinfo = $DBH->prepare($sql);
$getinfo->execute(array(':user' => $memberuser));
$log_file = "/home/my/transaction.log";
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "Starting loop...";
error_log($message, 3, $log_file);
while ($row = $getinfo->fetch(PDO::FETCH_ASSOC))
{
$check = $row;
$newEmployeeEmail = $check['email'];
$csvusername = $check['username'];
$password = $check['password'];
$status = $check['status'];
if ($status == "Open")
{
echo "tesing";
$from = "the email of where it is coming from is here but i removed";
$to = $newEmployeeEmail;
if (!empty($_POST['cc']))
{
$cc = $_POST['cc'];
}
if (!empty($_POST['ccsend']))
{
$cc = $_POST['ccsend'];
$to .= ", $cc";
}
$subject = "removed msg";
$body = "removed msg";
$host = "i removed";
$username = "i removed";
$password = "i removed";
$headers = array(
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject,
);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "Before connecting to server - " . $to;
error_log($message, 3, $log_file);
$smtp = Mail::factory(
'smtp',
array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
)
);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "After connecting to server - " . $to;
error_log($message, 3, $log_file);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "Before sending email - " . $to;
error_log($message, 3, $log_file);
$mail = $smtp->send($to, $headers, $body);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "After sending email - " . $to;
error_log($message, 3, $log_file);
}
}
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "End of loop";
error_log($message, 3, $log_file);
header("Location: I removed this.php?getmsg=12");
exit;
}
您可能会发现您的功能超时,具体取决于邮件程序将电子邮件发送给相关主机所需的时间。
从我看到的每个用户都有一个(可能)唯一的邮件服务器连接,然后发送电子邮件。如果由于某种原因,建立该连接存在延迟,那么您的脚本很可能会超时。
您可以调整上面的错误记录方法以使用microtime
,这样您就可以看到代码每次迭代之间的实际时间 - 当然,您需要在代码块之间添加更多日志记录。
如果您发现邮件服务器连接等确实存在延迟,您可以(如果您的要求允许)将一次连接到一台服务器并从那里发送电子邮件。
HTH