PHPMailer类与Gmail,每次发送两封电子邮件

发布于 2024-12-03 12:42:35 字数 2849 浏览 0 评论 0原文

我有一个控制台应用程序,它发布到一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2024-12-10 12:42:35

您有以下代码:

if($msg != "") {
    if($row["mobile_notification"] == 1) {
        // stuff
    }
    if($row["email_notification"] == 1) {
        // stuff
    }

如果 mobile_notification 设置为 1,则发送电子邮件一次;如果 email_notification 设置为 1,则发送电子邮件;这可能就是原因。 您可能只想

    else if($row["email_notification"] == 1) {

如果手机尚未发送,

发送电子邮件。在不了解更多代码或数据的情况下,很难说。您可以采取一些措施来进一步追踪它

在 PHP sendMessage 函数中放置一个 echo:

function sendMessage($to, $toName, $body) {
    echo 'Sending mail...';
    // rest of the function here
}

并查看它是否确实触发了两次(应该是)。

现在看看每次从数据库返回的内容,

while($row = mysql_fetch_array($results)) {
    print_r($row);
    // script continues
}

每个查询可能会返回 2 行,在这种情况下,您可以查看 SQL 查询以查看是否正确。

假设是这样,也许 PHP 文件被调用了两次,所以在 C# 文件中放置一个调试(我不懂 C#,所以我将语法留给你)

public static void PostUpdate(string uid)
{
    // echo uid here
    // script continues
    WebRequest request = WebRequest.Create("http://127.0.0.1/bin/server/check_table.php");
    request.Method = "POST";

通过只打印一些输出,你可以跟踪它的去向错了,知道了之后再看看为什么会出错。


我刚刚看到你正在这样做......

while($row = mysql_fetch_array($results)) {

然后稍后,在该循环中,说

 $results = mysql_query("UPDATE users...

并重新定义你已经循环的结果集。您可以运行查询而不获取返回值;

 mysql_query("UPDATE users

或者第二次给它一个不同的名字

$results2 = mysql_query("UPDATE users...

,它可能会起作用:)

You have this code:

if($msg != "") {
    if($row["mobile_notification"] == 1) {
        // stuff
    }
    if($row["email_notification"] == 1) {
        // stuff
    }

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

    else if($row["email_notification"] == 1) {

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:

function sendMessage($to, $toName, $body) {
    echo 'Sending mail...';
    // rest of the function here
}

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

while($row = mysql_fetch_array($results)) {
    print_r($row);
    // script continues
}

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)

public static void PostUpdate(string uid)
{
    // echo uid here
    // script continues
    WebRequest request = WebRequest.Create("http://127.0.0.1/bin/server/check_table.php");
    request.Method = "POST";

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

while($row = mysql_fetch_array($results)) {

And then later on, in that loop, saying

 $results = mysql_query("UPDATE users...

And re-defining the result set you're already looping on. You can run a query without taking a return value;

 mysql_query("UPDATE users

or just give it a different name the second time

$results2 = mysql_query("UPDATE users...

And it'll probably work :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文