4
\$\begingroup\$

I am making a PHP script which fetches the data of the emails whose sending date and time has been reached and then send emails to those records. I am following the steps below and integrating it in an API, that is.

GitHub code

Steps:

  1. Get the communication records whose time has reached.
  2. Run the loop of above and get the email addresses.
  3. Run the loop of email addresses and send emails.

This is my PHP code:

<?php
include('iSDK/src/isdk.php');
$myApp = new iSDK();
// Test Connnection
if ($myApp->cfgCon("connectionName")) {
    echo "Connected...";
} else {
    echo "Not Connected...";
}
$query = mysql_query('SELECT * FROM `emails` WHERE `senddate` <= NOW()');
while ($row = mysql_fetch_assoc($query)) {
    $conDat = array('Email' => $row['email']);
    $conID = $myApp->addCon($conDat);
    $clist = array($conID);
    $email = $myApp->sendEmail($clist, '[email protected]', '~Contact.Email~',
        'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');
}
?>

What changes should be made in it?

\$\endgroup\$
0

2 Answers 2

3
\$\begingroup\$
  1. Use PDO, not mysql_* as they are deprecated and dangerous.
  2. Break up your code into functions, this makes it easier to call and test.
  3. Throw and catch exceptions.
  4. You can omit the ending ?>

NOTE: This is untested code as I don't have the isdk.php file.

    <?php

    require('iSDK/src/isdk.php');

    $iSDK = new iSDK();

    try{
        checkConnection($iSDK);
        performQuery($iSDK, getDBConnection("", "", "", ""));
    } catch(RuntimeException $e){
        die($e->getMessage());
    }

    function checkConnection(&$iSDK){
        # Throw an exception if we don't have a connection, otherwise return true
        if (!$iSDK->cfgCon("connectionName")) {
            throw new RuntimeException('Could not obtain connection.');
        }
    }

    function performQuery(&$iSDK, $PDO){
        $sth = $PDO->prepare("SELECT * FROM `emails` WHERE `senddate` <= NOW()");
        $sth->setFetchMode(PDO::FETCH_ASSOC); # setting the fetch mode
        $sth->execute();

        while($row = $sth->fetch()) {
            $conDat = array('Email' => $row['email']);
            $conID = $iSDK->addCon($conDat);
            $clist = array($conID);
            $email = $iSDK->sendEmail($clist, '[email protected]', '~Contact.Email~', 'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');
        }
    }

    function getDBConnection($host, $dbname, $user, $pass){
        try {
            return new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        } catch(PDOException $e) {
            die($e->getMessage());
        }
    }
\$\endgroup\$
11
  • \$\begingroup\$ This code is having some syntax error, check it. you can get ISDK from here github.com/infusionsoft/PHP-iSDK \$\endgroup\$ Mar 13, 2014 at 11:52
  • \$\begingroup\$ Even if I had the iSDK lib, I don't have your Db structure, nor am I going to rebuild it. As I stated, "This is untested code as I don't have the isdk.php file.". If this has errors, post them and I will deduce and fix. \$\endgroup\$
    – jsanc623
    Mar 13, 2014 at 14:06
  • \$\begingroup\$ function performQuery(&iSDK, $PDO){ $sth = $PDO->("SELECT * FROM emails WHERE senddate <= NOW()"); These lines indicating syntax error. I tried to solve it but could not. \$\endgroup\$ Mar 13, 2014 at 14:59
  • \$\begingroup\$ "&iSDK" is a reference to a constant. Correct way is "&$iSDK". Simple fix. noticed I missed the prepare on $PDO in line after that, also fixed. I just tried the code and it ran with no syntax error: i.imgur.com/Oc2bsAC.png (exited out with caught exception). \$\endgroup\$
    – jsanc623
    Mar 13, 2014 at 15:20
  • 1
    \$\begingroup\$ @jsanc623 good call \$\endgroup\$
    – azngunit81
    Mar 14, 2014 at 14:15
2
\$\begingroup\$
// Test Connnection
if ($myApp->cfgCon("connectionName")) {
    echo "Connected...";
} else {
    echo "Not Connected...";
}

Does it make sense to continue the program and run the SQL query if it's not connected? If not stop it somehow.

$query = mysql_query('SELECT * FROM `emails` WHERE `senddate` <= NOW()');
while ($row = mysql_fetch_assoc($query)) {
    $conDat = array('Email' => $row['email']);
    $conID = $myApp->addCon($conDat);
    $clist = array($conID);
    $email = $myApp->sendEmail($clist, '[email protected]', '~Contact.Email~', 'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');
}

The $email variable is unused here. If it's not neccesary remove it:

$myApp->sendEmail($clist, '...', '...', 'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');

Anyway, you might want to print out some log file about the sending errors/successes for debugging or reporting purposes.

\$\endgroup\$
2
  • \$\begingroup\$ Is my code performing the actions which am supposed to do? or is there something wrong in logic.? \$\endgroup\$ Mar 4, 2014 at 11:45
  • \$\begingroup\$ @Kamran: I've found only these things. It looks OK although I have no time to test it and I'm not familiar with the used APIs (iSDK, MySQL). \$\endgroup\$
    – palacsint
    Mar 4, 2014 at 11:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.