Tilly Tilly - 6 months ago 10
PHP Question

Alternate Email Address each time a form submits

Hi Could someone please tell me if the following is possible please?

I have a small php contact form which sends the information collected on the form to a designated email address

$emailaddress=whatever@whatever.com

What I am trying to achieve is that every time the form sends data, it switches so it would do the following...

Customer sends data via submit to $emailaddress1=whatever@whatever.com

The next customer sends data it submits to $emailaddress2=whoever@whoever.com
The next customer sends data via submit to $emailaddress1=whatever@whatever.com again and so on.

Essentially every other send switches the email address to a different one to the last send.

Thanks in advance
Tilly

Answer

Problem resolved thanks to Boerema plus a few little tweaks of my own.

Basically I just changed the delete part till after the form was processed, sine when the form is displayed it was deleting or creating without submission.

So...

In the form itself the code is...

$filename = '/full/path/to/tempfile/sent.txt'; 
if (file_exists($filename)) { //
$recipients = 'number1emailaddress@test.com';
} else {
$recipients = 'number2emailaddress@test.com';
}

Then after the form has processed

$filename = '/path/to/your/tempfile/sent/sent.txt';

if (file_exists($filename)) {
unlink($filename); //delete the file
} else {
fopen($filename, 'w') or die('Cannot open file:  '.$filename); //implicitly creates file
}

This way the file is only deleted/created on an actual submission, which works perfectly

Many thanks Boerema

Comments