0

我正在尝试制作这个秘密的圣诞老人自动电子邮件脚本,这真的让我感到困惑......它无法正确发送电子邮件(一个人收到大量电子邮件,没有其他人收到......)

我试图在 for 循环中更改数组的长度,我做对了吗?

我使用了 shuffle 函数和 email 函数,它们似乎运行良好(除了 email 函数上的 for 循环——我这样做对吗?)。这是我需要帮助的主要功能。

这是代码:

<?php
function twodshuffle($array)
{
    // Get array length
    $count = count($array);
    // Create a range of indicies
    $indi = range(1,$count);
    // Randomize indicies array
    shuffle($indi);
    // Initialize new array
    $newarray = array($count);
    // Holds current index
    $i = 0;
    // Shuffle multidimensional array
    foreach ($indi as $index)
    {
        $newarray[$i] = $array[$index];
        $i++;
    }
    return $newarray;
}

function email($Name, $Email, $Likes)
{
    $to = $Email;
    $subject = '[Secret Santa] Happy Lolidays from Santabot 2020! Your match is here!';

    $message = "HEY! You are " . $Name ."'s Secret Santa. Isn't that exciting? \r\n\r\nThis is what your match filled in for likes/dislikes: \r\n----------------------------\r\n" . $Likes . "\r\n----------------------------\r\n\r\n\r\nNow get them something nice and thoughtful. We will be swapping sometime around Christmas, so you have a lot of time to put some thought in it.\r\nLet's keep gifts around the $30 mark.\r\n\r\nHappy Lolidays,\r\nDCo's Santabot 2020.\r\n\r\nPS: I have set this up so not even I know the matches nor is a list stored anywhere. DO NOT delete this email if you are at risk of forgetting your match.";

$headers = 'From: Santabot 2020 <santabot2020@mydomain.com>' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
sleep(1000);
}

$con = mysql_connect("mysql.myserver.com", "username", "password") or die(mysql_error());
mysql_select_db("lolidays_dco", $con) or die(mysql_error());
$array;
//I just changed this array size now. Is this the right syntax?
for ($i = 1; $i <= array.length() -1; $i++) {
    $sql = "SELECT * FROM tbl_data WHERE Number = " . $i;
    $result = mysql_query($sql);
    $count=mysql_num_rows($result);
    while($row = mysql_fetch_array($result))
        {
        $Name = $row['Name'];
        $Email = $row['Email'];
        $Likes = $row['Likes'];
        }
    $array[$i] = array("Name" => $Name, "Email" => $Email, "Likes" => $Likes);


}
$array = twodshuffle($array);
$string;
//changed this array length as well, is this the right syntax?
for ($i = 0; $i <= array.length(); $i++) {
    if ($i == 2) {
        $n = $array['0']["Name"];
        $e = $array[$i]["Email"];
        $l = $array['0']["Likes"];
        email($n, $e, $l);
        echo "Email Sent!<br />";
        $string = $string . $array[$i]["Name"] . " => " . $array['0']["Name"] . "\r\n";
        email('Matches', 'backup@email.com', $string);
    }
    else {
        $j = $i + 1;
        $n = $array[$j]["Name"];
        $e = $array[$i]["Email"];
        $l = $array[$j]["Likes"];
        email($n, $e, $l);
        echo "Email Sent!<br />";
        $string = $string . $array[$i]["Name"] . " => " . $array[$j]["Name"] . "\r\n";

    }
}
echo "<strong>Done!</strong>";
?>
4

1 回答 1

1

您已硬编码收件人:

    $n = $array['0']["Name"];   <---should be $i, not '0'
    $e = $array[$i]["Email"];
    $l = $array['0']["Likes"];   <---should be $i, not '0'
于 2012-11-14T19:02:06.457 回答