【问题标题】:When sending PHP form, loop from a list of PHP variables each time form is submitted发送 PHP 表单时,每次提交表单时从 PHP 变量列表中循环
【发布时间】:2023-03-08 07:59:01
【问题描述】:

我想循环浏览几封电子邮件,以便每次向myform.php 提交表单时,都会向不同的用户发送电子邮件。

请注意,我不是在寻找任何发送邮件的功能。它恰好是正在使用的电子邮件,但关键是我需要知道如何从选项列表中循环遍历一个变量。

这个问题实际上与电子邮件无关,因为表单已经可以使用并使用名为 $mail_to 的变量发送电子邮件。相反,这个问题是关于每次提交表单时循环一个 PHP 变量。

无论我能做些什么来循环它们,当将电子邮件放入 $mail_to 时,表单的其余部分都可以正常工作

$mail_to = 'user1@mycompany.com';

我想做的不是将一封电子邮件放入$mail_to,而是要循环浏览几封电子邮件。例如,

电子邮件:

user1@mycompany.com
user2@mycompany.com
user3@mycompany.com
user4@mycompany.com

PHP 形式:

$email_looped = [???];

$mail_to = $email_looped;

以上,[???] 只是我用人类的话说我不知道​​在这里做什么。


实际上,它会像这样实时工作:

第一个访客从网站提交表单 >> $mail_to = 'user1@mycompany.com';

第二位访客从网站提交表单 >> $mail_to = 'user2@mycompany.com';

第三位访客从网站提交表单 >> $mail_to = 'user3@mycompany.com';

第 4 位访客从网站提交表单 >> $mail_to = 'user4@mycompany.com';

第 5 位访客从网站提交表单 >> $mail_to = 'user1@mycompany.com';


我怎样才能使每次表单发布到myform.php(来自mypage.html)时,它会选择列表中的下一封电子邮件并循环它们?

【问题讨论】:

  • 我不完全理解你的问题。如果我错了,请纠正我。您想在每次提交表单时同时向多个收件人发送电子邮件吗?
  • 不,每次从电子邮件列表中选择下一个项目时都会有一个不同的。示例:第一次提交表单,发送到user1@mycompany.com,第二次提交表单,发送到user2@mycompany.com,... 第五次提交表单,再次发送到user1@mycompany.com,每次提交表单时循环.
  • 基本上,每次用户向网站提交表单时,它都会转到我公司的下一个代表并循环访问每个代表,因此它会循环向支持团队发送电子邮件。
  • 但这不是关于电子邮件,而是关于每次提交表单时循环一个变量。
  • 我明白了。因此,每提交一份表单,就从电子邮件收件人列表中向一个收件人发送一封电子邮件。我个人会使用 txt 文件或 SQL 数据库的形式来解决这个问题,其中将存储所有收件人以及一些数据,这些数据将告知下一个收件人,并使用每个成功提交的表单重写此数据。跨度>

标签: php loops


【解决方案1】:

您可以将最后一个索引存储在文本文件或数据库中,在下一次请求时检索该值,并使用它来获取数组中的下一项。实现这一点的方法有很多,一些伪代码可以帮助您入门。

function storeCurrentEmail(int $currentIndex): void
{
    // return \Database::table->store('lastEmailIndex', $currentIndex);
    // return file_put_contents("/path/to/cached/file.txt", $currentIndex);
}

function getNextEmail(): array
{
    // $lastEmailIndex = file_get_contents("/path/to/cached/file.txt");
    // $lastEmailIndex = \Database::table->get('lastEmailIndex');
    $lastEmailIndex = 3; // for illustrative purposes

    $emails = [
        'user1@mycompany.com',
        'user2@mycompany.com',
        'user3@mycompany.com',
        'user4@mycompany.com'
    ];

    if ($lastEmailIndex >= sizeof($emails)) {
         $lastEmailIndex = 0;
    }

    return [
        $emails[$lastEmailIndex],
        $lastEmailIndex
    ];
}


list($mail_to, $lastIndex) = getNextEmail();

storeCurrentEmail($lastIndex);

\Mail::sendTo($mail_to);

【讨论】:

  • 它不会将电子邮件存储在文件或数据库中。如果您正在寻找的话,没有办法在 PHP 脚本本身中保存数据。你总是需要某种持久层来让你的应用记住状态。
  • 是的,因为有很多方法可以保存数据。取决于你的堆栈。如果您没有堆栈,写入文件可能是最快的解决方案。如果您有数据库,请添加一个新表并使用它。
  • 我也试过你的代码来放置文件,它也不起作用。
  • Sadly your code does not work at all。即使在它自己的 php 文件中并使用您的代码来存储文件,您的代码也不起作用。此外,您的代码中不应有任何 /Mail 函数。我没有发送邮件,我正在更新一个变量。我说过很多次这个问题的目的只是为了更新一个变量(我的代码确实发送了邮件,但这发生在页面的后面,与这个问题无关)。它可能是一封电子邮件,或者一个整数,或者其他什么,关键是有一个项目列表,它被循环并存储为一个变量,供以后在 php 脚本中使用
  • 正确,伪代码实际上并没有运行。它的目的是让您了解如何编写解决方案。我不是为你写剧本。
【解决方案2】:

在这里我已经很简单地解决了:

首先在当前目录下创建一个名为current.txt的文件,并在其中写入一个0。

php代码:

        $emails = [
            'user1@mycompany.com',
            'user2@mycompany.com',
            'user3@mycompany.com',
            'user4@mycompany.com'
        ];
        
        $lastEmailIndex = file_get_contents("current.txt");
        $max = count($emails) - 1;
        
        if ($lastEmailIndex >= $max) { 
            $lastEmailIndex = 0;
            file_put_contents("current.txt",0);
        } else {
            file_put_contents("current.txt",@file_get_contents("current.txt")+1);
            $lastEmailIndex = file_get_contents("current.txt");
        }

用法:

    echo '<script>alert("'. $emails[$lastEmailIndex] . '")</script>';

现在它将遍历所有可用的项目,您可以根据需要向数组中添加任意数量的项目。

【讨论】:

    【解决方案3】:

    PHP 可以非常简单,享受 ;)

    targets.txt:
    user1@example.com
    user2@example.com
    user3@example.com
    user4@example.com
    
    example.php
    //read and trim the targets file
    $targets = file_get_contents('targets.txt');
    $targets = trim($targets," \r\n");
    $arr = explode("\n",$targets);
    $mail_to = $arr[0];
    //now rotate the file
    $temp = array_shift($arr);
    $arr = array_merge($arr,array($temp));
    file_put_contents('targets.txt', implode("\n",$arr));
    echo $mail_to;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多