[PHP] Send mail per form condition

Modulus by 3?

Currently on the mobile phone, modulus is so confusing at time or I'm just tired.

Tell me if I am or am not on the right track here please.

3 branches

PHP:
if ($hour % 3 = 0) { mail(cpt1) }
if ($hour % 3 = 1) { mail(cpt2) }
if ($hour % 3 = 2) { mail(cpt3) }

4 branches

PHP:
if ($hour % 4 = 0) { mail(cpt1) }
if ($hour % 4 = 1) { mail(cpt2) }
if ($hour % 4 = 2) { mail(cpt3) }
if ($hour % 4 = 3) { mail(cpt4) }
 
Currently on the mobile phone, modulus is so confusing at time or I'm just tired.

Tell me if I am or am not on the right track here please.

3 branches

PHP:
if ($hour % 3 = 0) { mail(cpt1) }
if ($hour % 3 = 1) { mail(cpt2) }
if ($hour % 3 = 2) { mail(cpt3) }

4 branches

PHP:
if ($hour % 4 = 0) { mail(cpt1) }
if ($hour % 4 = 1) { mail(cpt2) }
if ($hour % 4 = 2) { mail(cpt3) }
if ($hour % 4 = 3) { mail(cpt4) }

If you're planning for that amount of branches or more, rather go with a memcache the distribution of mail will be fairer.
 
If you're planning for that amount of branches or more, rather go with a memcache the distribution of mail will be fairer.

Have absolutely zero idea of that :confused: this teaching one self without human interaction or mentorship sure sucks ballz to be honest.


Is memcache and memcached the same thing also does this come standard in php or will I need to install a specific module. Keep in mind my server(hosting environment) is a shared one.
 
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

quick and dirty PDO for the drift, db table and structure implied
PHP:
$dbh = new PDO(...);
//your inserting code here
$town = $_POST['town'];
$emailSql = "select email from branch_emails where town = :town order by date_last_used asc limit 1";
$getEmail = $dbh->prepare($emailSql);
$getEmail->execute(array(':town' => $town));
if ($getEmail->rowCount > 0)
{
    $row = $getEmail->fetch(PDO::FETCH_ASSOC);
    if (mail($row['email'], etc.))
    {
      $updateSql = "update branch_emails set date_last_used = NOW() where town = :town limit 1";
      $branchUpdate = $dbh->prepare($updateSql);
      $branchUpdate->execute(array(':town' => $town));
    }
    else
      //email not sent
}
else
{
    //let user know town doesn't exist/send to default mailbox/user possibly trying funny stuff
}

read up on memcached and/or apc and use those later on.
 
quick and dirty PDO for the drift, db table and structure implied
PHP:
$dbh = new PDO(...);
//your inserting code here
$town = $_POST['town'];
$emailSql = "select email from branch_emails where town = :town order by date_last_used asc limit 1";
$getEmail = $dbh->prepare($emailSql);
$getEmail->execute(array(':town' => $town));
if ($getEmail->rowCount > 0)
{
    $row = $getEmail->fetch(PDO::FETCH_ASSOC);
    if (mail($row['email'], etc.))
    {
      $updateSql = "update branch_emails set date_last_used = NOW() where town = :town limit 1";
      $branchUpdate = $dbh->prepare($updateSql);
      $branchUpdate->execute(array(':town' => $town));
    }
    else
      //email not sent
}
else
{
    //let user know town doesn't exist/send to default mailbox/user possibly trying funny stuff
}

read up on memcached and/or apc and use those later on.

My DB looks like this:

PHP:
// Database handeling - inserting the form data into the DB

if (isset($_POST["submit"])) {
    require_once 'db.php';
            $name = $_POST['name'];
            $surname = $_POST['surname'];
            $number = $_POST['number'];
            $visitor_email = $_POST['email'];
            $town = $_POST['town'];
            $intention = $_POST['intention'];
            $message = $_POST['message'];

$statement = $db->prepare("INSERT INTO users (`name`, `surname`, `number`, `email`, `send_to`, `town`, `intention`, `message`) VALUES (:name, :surname, :number, :email, :send_to, :town, :intention, :message)");
$statement->execute(array(':name' => $name, ':surname' => $surname, ':number' => $number, ':email' => $visitor_email, ':send_to' => $to_email_address, ':town' => $town, ':intention' => $intention, ':message' => $message));
    }

$Send_to is the mail of the branch it was send to ( gotten from the switch case earlier )
 
Top
Sign up to the MyBroadband newsletter
X