0

I want to loop through a few emails so that every time a form is submitted to myform.php, a different user is emailed.

Please note I am not looking for any functions to send mail. It happens to be emails being used, but the point is I need to know how to loop through a variable from a list of options.

This question isn't actually about emailing, because the form already works and sends email using a variable named $mail_to. Instead, this question is about looping a PHP variable each time a form is submitted.

Whatever I can do to loop them, the rest of the form works when putting an email into $mail_to like

$mail_to = '[email protected]';

What I want to do is instead of putting one email into $mail_to, instead I want to loop through a few emails. For example,

Emails:

[email protected]
[email protected]
[email protected]
[email protected]

In PHP form:

$email_looped = [???];

$mail_to = $email_looped;

Above, [???] is simply me saying in human terms I don't know what to do here.


Effectively, it will work like this in real time:

1st Visitor submits form from website >> $mail_to = '[email protected]';

2nd Visitor submits form from website >> $mail_to = '[email protected]';

3rd Visitor submits form from website >> $mail_to = '[email protected]';

4th Visitor submits form from website >> $mail_to = '[email protected]';

5th Visitor submits form from website >> $mail_to = '[email protected]';

etc


How can I make it so that every time the form is posted to myform.php (from mypage.html), it chooses the next email in the list and loops them?

5
  • I do not understand exactly your question. Please correct me if I'm wrong. Do you want to send an email to multiple recipients at once each time you submit a form? Commented May 5, 2021 at 22:15
  • No, a different one each time chosen the next item from a list of emails. Example: First time form is submitted, send to [email protected], second time form is submitted, send to [email protected], ... 5th time form is submitted, send to [email protected] again and keep looping on each time form is submitted. Commented May 5, 2021 at 22:17
  • Basically, every time a user submits a form to the website, it goes to the next rep in my company and loops through each rep, so it round-robin emails the support team. Commented May 5, 2021 at 22:18
  • But this isn't about email, it's about looping a variable every time a form is submitted. Commented May 5, 2021 at 22:19
  • I see. So with each form submitted, send an email to one recipient from the email recipient list. I personally would solve this using a txt file, or in the form of an SQL database, where all recipients will be stored plus some data that will inform which recipient will be next in line and rewrite this data with each successfully submitted form. Commented May 5, 2021 at 22:29

3 Answers 3

1

You could store the last index in a text file or database, retrieve that value on the next request, and use it to get the next item in the array. Tons of ways to accomplish this, some pseudo code to get you started.

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 = [
        '[email protected]',
        '[email protected]',
        '[email protected]',
        '[email protected]'
    ];

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

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


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

storeCurrentEmail($lastIndex);

\Mail::sendTo($mail_to);

Sign up to request clarification or add additional context in comments.

6 Comments

It doesn’t store emails in a file or database. There is no way to persist data within a PHP script itself, if that’s what you’re looking for. You will always need some kind of persistence layer for your app to remember state.
Yup, because there are so many ways to persist data. Depends on your stack. If you don’t have a stack, writing to a file is probably the quickest solution. If you have a database, add a new table and use that.
I tried your code for putting the file as well, also it doesn't work.
Sadly your code does not work at all. Even in its own php file and using your code for storing a file, your code doesn't work. Additionally, there should not be any /Mail function in your code. I am not sending mail, I am updating a variable. I said many times the purpose of this question is simply to update a variable (my code does send mail but that happens later in the page and nothing to do with this question). It could be an email, or an integer, or something else, the point is there is a list of items and it is looped through and stored as a variable for later use in the php script
Correct, pseudo code doesn’t actually run. It’s purpose is to give you an idea of how you could write your solution. I’m not writing your script for you.
|
0

Here I have solved it rather simply:

first create a file in current directory named current.txt and write a 0 in it.

php code:

        $emails = [
            '[email protected]',
            '[email protected]',
            '[email protected]',
            '[email protected]'
        ];
        
        $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");
        }

usage:

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

Now it will loop through all available items, and you can add as many items to the array as you want.

Comments

0

PHP can be brutally simple, enjoy ;)

targets.txt:
[email protected]
[email protected]
[email protected]
[email protected]

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.