[PHP] Send mail per form condition

Any form of help/guidance would be appreciated here regarding the alternations between two emails for one region
 
I would simplify that whole thing massively, less lines of code and easier to read.

HTML:
<option value="cpt">Cape Town</option>
 <option value="cy">Bellville</option>
 <option value="pta">Pretoria</option>

Then the PHP code you would just replace the begging piece.

PHP:
$town = $_POST['town'];
$toEmail = $_POST['town']."@myemail.com";
 
I would simplify that whole thing massively, less lines of code and easier to read.

HTML:
Cape Town
 Bellville
 Pretoria

Then the PHP code you would just replace the begging piece.

PHP:
$town = $_POST['town'];
$toEmail = $_POST['town']."@myemail.com";

I ended up doing that at first :P

But now I need to split a region between two emails.
 
Last edited:
I would simplify that whole thing massively, less lines of code and easier to read.

HTML:
<option value="cpt">Cape Town</option>
 <option value="cy">Bellville</option>
 <option value="pta">Pretoria</option>

Then the PHP code you would just replace the begging piece.

PHP:
$town = $_POST['town'];
$toEmail = $_POST['town']."@myemail.com";

But switch case is best in this example because the emails are all different

I just use cpt pta etc for the example purposes.

But I did use your method at first and then I made my scenario to accommodate different emails.

Now I want it to send emails from one region to two emails but not to both. So I need a logic that can devide the incoming form data and mail them 1 to cpt1 the next one to cpt2 the next one to cpt1 the next one to cpt2 and so forth
 
use your db, and have a field recording the last date that specific email was used.

Then, simply query the town, order by "last_date_used" ascending and choose the first result. Update db once email sent successfully.
 
Still need assistance on this anyone willing?

Lets say... Cape Town has two branches across the street of each other.... ( [email protected] [email protected] )

Let's say... If 10 people select cape town the end result should be that each received 5 emails.

The emails should be send as follows:

cpt1
cpt2
cpt1
cpt2
cpt1
cpt2
cpt1
cpt2
cpt1
cpt2

Does this make sense?

I basically want 1 locations mail to be split between two branches, 1 for you, 1 for you, etc etc.

I was thinking of putting them in an array and then use a random number between 1 and 2 to select the mail from the array, but I want to know what would be best here as I actually have no clue.

Currently have this going:

PHP:
$town = $_POST['town'];
switch ($town) {
    case 'capetown':
        $email_address = '[email protected]';
        break;
    case 'bellville':
        $email_address = '[email protected]';
        break;
    case 'pretoria':
       $email_address = '[email protected]';
        break;
    default:
       $email_address = '[email protected]';
}
mail($email_address, etc etc etc )
 
It is up to you to come up with the logic of which email address to send it to, I'd do something simple.
Take the current hour of the day and use modulus, if it's even send it to one address if it's odd send it to the other.
 
It is up to you to come up with the logic of which email address to send it to, I'd do something simple.
Take the current hour of the day and use modulus, if it's even send it to one address if it's odd send it to the other.

Here's the problem. I know the logic, I have an idea but I don't know programing. I don't know what php can and cannot do and if I know I won't have an idea how to do it unless someone showed and explained it first.

FYI: I had the same idea, how to actually do it thou... nother story all together.
 
Did you even read this?

use your db, and have a field recording the last date that specific email was used.

Then, simply query the town, order by "last_date_used" ascending and choose the first result. Update db once email sent successfully.
 
Here's the problem. I know the logic, I have an idea but I don't know programing. I don't know what php can and cannot do and if I know I won't have an idea how to do it unless someone showed and explained it first.

FYI: I had the same idea, how to actually do it thou... nother story all together.

PHP is a bitch, if you were doing ASP.NET I'd have it done for you already :p
 
It is up to you to come up with the logic of which email address to send it to, I'd do something simple.
Take the current hour of the day and use modulus, if it's even send it to one address if it's odd send it to the other.

This is what I had in mind, but have no idea how to do it in php

PHP:
$town_branch = time();

  if (($town_branch % 2) == 1){ 
//odd
  mail(cpt1) ;
}

  if (($a % 2) == 0){   
//even
  mail(cpt2) ;
}


Did you even read this?

a DB for this is overkill.
 
Last edited:
I assume I can make it one by combining it

PHP:
$town_branch = time();

if (($town_branch & 1) {
    //odd
    mail(cpt1) ;
} else { 
    //even
    mail(cpt2) ;
}

Not sure if I am on the right track thou.
 
Querying a db for every email sent doesn't seem like a good idea, rather use a static variable or something similar.

a DB for this is overkill.

Sure, I can understand minimising db calls. However, note:
1. A list of emails for all branches are needed.
2. The last email address used for a particular branch needs to be recorded.

The store for all of this, be it db or otherwise, can be swapped out later once a worthwhile alternative is found, but the rest of the code should work with minimal tweaking if needed. Heck, have a look at memcached (can be shared) or apc_calls (not too sure if it can be shared, but then memcached is made for this) if you loathe using a db for this.
 
Sure, I can understand minimising db calls. However, note:
1. A list of emails for all branches are needed.
2. The last email address used for a particular branch needs to be recorded.

The store for all of this, be it db or otherwise, can be swapped out later once a worthwhile alternative is found, but the rest of the code should work with minimal tweaking if needed. Heck, have a look at memcached (can be shared) or apc_calls (not too sure if it can be shared, but then memcached is made for this) if you loathe using a db for this.

+1 memcached is also a good idea
 
Sure, I can understand minimising db calls. However, note:
1. A list of emails for all branches are needed.
2. The last email address used for a particular branch needs to be recorded.

The store for all of this, be it db or otherwise, can be swapped out later once a worthwhile alternative is found, but the rest of the code should work with minimal tweaking if needed. Heck, have a look at memcached (can be shared) or apc_calls (not too sure if it can be shared, but then memcached is made for this) if you loathe using a db for this.

Not going to lie I contemplated using a db for this for quite some time, but I'm not 100% sure where I'd even begin to query a db

I do however use PDO to insert the form data into the db already, but not to sure how I'd do this thou. Hence why I'm trying to keep it as simplistic as possible as I learn
 
Top
Sign up to the MyBroadband newsletter
X