PHPMailer类与Gmail,每次发送两封电子邮件
我有一个控制台应用程序,它发布到一个 PHP 页面(使用 Gmail 和 PHPMailer 类)向用户发送电子邮件。
问题是,用户总是收到两次电子邮件!
这是我的 C# 帖子(我传入 uid 是为了记录目的。)
public static void PostUpdate(string uid)
{
WebRequest request = WebRequest.Create("http://127.0.0.1/bin/server/check_table.php");
request.Method = "POST";
string postData = "check_table";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : HttpWebResponse( " + ((HttpWebResponse)response).StatusDescription + " )");
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Display the content for debugging
// Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : " + responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
我的 PHP 文件包含
$sql = "SELECT * FROM users as u JOIN value_store as v ON u.id = v.uid WHERE u.last_notification < DATE_SUB( NOW( ) , INTERVAL 5 MINUTE ) ";
$results = mysql_query($sql, $db_link);
while($row = mysql_fetch_array($results)) {
$id = $row[0];
$msg = "";
if($row["inlet_moisture"] > $row["inlet_moisture_high_critical"]) {
$msg = "Critical Inlet Moisture";
} else if($row["inlet_moisture"] > $row["inlet_moisture_high_warning"]) {
$msg = "Inlet Moisture Warning";
}
if($msg != "") {
if($row["mobile_notification"] == 1) {
if( sendMessage(($row["mobile_number"] . "@" . $row["mobile_carrier"]), $row["first_name"], $msg) ) {
$res1 = mysql_query("UPDATE users SET last_notification = NOW() WHERE id = $id",$db_link);
echo "SMS Sent for [$id]";
}
}
if($row["email_notification"] == 1) {
if( sendMessage($row["email"], $row["first_name"], $msg) ) {
$res2 = mysql_query("UPDATE users SET last_notification = NOW() WHERE id = $id",$db_link);
echo "Email Sent for [$id]";
}
}
}
echo "Update For [$id] Complete!";
}
我已经考虑到的一些调试选项。
我已经设置了控制台应用程序来读取 WebRequest 的输出... 所以我可以看到我正在打开发布页面,POST 正常。我认为可能存在关闭问题,因为数据库中有两行,如果我回显每行的 ID,我只会得到最后一行(两次)。我尝试更新我的 PHP while 循环以输出我正在使用的 id(row) 。
我仍然收到
Update For [2] Complete!
Update For [2] Complete!
而不是
Update For [1] Complete!
Update For [2] Complete!
参见上面的修订。
I have a console application that posts to a PHP page that (using Gmail and PHPMailer class) emails the user.
The problem is, the user always receives the email twice!
Here is my post from C# (I pass in uid for logging purposes.)
public static void PostUpdate(string uid)
{
WebRequest request = WebRequest.Create("http://127.0.0.1/bin/server/check_table.php");
request.Method = "POST";
string postData = "check_table";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : HttpWebResponse( " + ((HttpWebResponse)response).StatusDescription + " )");
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
// Display the content for debugging
// Console.WriteLine("[" + uid + "]- " + DateTime.Now.ToString() + " : " + responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
My PHP file consists of
$sql = "SELECT * FROM users as u JOIN value_store as v ON u.id = v.uid WHERE u.last_notification < DATE_SUB( NOW( ) , INTERVAL 5 MINUTE ) ";
$results = mysql_query($sql, $db_link);
while($row = mysql_fetch_array($results)) {
$id = $row[0];
$msg = "";
if($row["inlet_moisture"] > $row["inlet_moisture_high_critical"]) {
$msg = "Critical Inlet Moisture";
} else if($row["inlet_moisture"] > $row["inlet_moisture_high_warning"]) {
$msg = "Inlet Moisture Warning";
}
if($msg != "") {
if($row["mobile_notification"] == 1) {
if( sendMessage(($row["mobile_number"] . "@" . $row["mobile_carrier"]), $row["first_name"], $msg) ) {
$res1 = mysql_query("UPDATE users SET last_notification = NOW() WHERE id = $id",$db_link);
echo "SMS Sent for [$id]";
}
}
if($row["email_notification"] == 1) {
if( sendMessage($row["email"], $row["first_name"], $msg) ) {
$res2 = mysql_query("UPDATE users SET last_notification = NOW() WHERE id = $id",$db_link);
echo "Email Sent for [$id]";
}
}
}
echo "Update For [$id] Complete!";
}
I have taken into consideration some debug options posted.
I have setup my console application to read the output from the WebRequest...
So I can see I am opening the posting the page, the POST is OK. I think there might be a closure issue because there are two rows in the database, and If I echo back the ID for each row I only get the last row (twice). I have tried updating my PHP while loop to output for which id(row) i'm using.
I still receive
Update For [2] Complete!
Update For [2] Complete!
instead of
Update For [1] Complete!
Update For [2] Complete!
See above for revisions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有以下代码:
如果 mobile_notification 设置为 1,则发送电子邮件一次;如果 email_notification 设置为 1,则发送电子邮件;这可能就是原因。 您可能只想
如果手机尚未发送,
发送电子邮件。在不了解更多代码或数据的情况下,很难说。您可以采取一些措施来进一步追踪它
在 PHP sendMessage 函数中放置一个 echo:
并查看它是否确实触发了两次(应该是)。
现在看看每次从数据库返回的内容,
每个查询可能会返回 2 行,在这种情况下,您可以查看 SQL 查询以查看是否正确。
假设是这样,也许 PHP 文件被调用了两次,所以在 C# 文件中放置一个调试(我不懂 C#,所以我将语法留给你)
通过只打印一些输出,你可以跟踪它的去向错了,知道了之后再看看为什么会出错。
我刚刚看到你正在这样做......
然后稍后,在该循环中,说
并重新定义你已经循环的结果集。您可以运行查询而不获取返回值;
或者第二次给它一个不同的名字
,它可能会起作用:)
You have this code:
Which is sending an email once if the mobile_notification is set to 1, and also sending an email if the email_notification is set to 1; this could be the cause. You may want to use
to only send the email if the mobile hasn't been sent.
Without knowing some more of the code or data, it's hard to say. Some things you can do to track it down further
Put an echo in the PHP sendMessage function:
and see if it's definitely firing that twice (it should be).
Now have a look to see what's coming back from the database each time
It may be getting 2 rows back for each query, in which case you may look at your SQL query to see if that's correct.
Assuming it is, maybe the PHP file is being called twice, so put a debug in the C# file (I don't know C# so I'll leave the syntax to you)
By just printing some output, you can track where it's going wrong, and then look at why it's going wrong once you know that.
I've just seen that you're doing this...
And then later on, in that loop, saying
And re-defining the result set you're already looping on. You can run a query without taking a return value;
or just give it a different name the second time
And it'll probably work :)