patternphpMinor
To send the email on some given date and time
Viewed 0 times
theemaildatetimesomeandsendgiven
Problem
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:
This is my PHP code:
What changes should be made in it?
GitHub code
Steps:
- Get the communication records whose time has reached.
- Run the loop of above and get the email addresses.
- Run the loop of email addresses and send emails.
This is my PHP code:
cfgCon("connectionName")) {
echo "Connected...";
} else {
echo "Not Connected...";
}
$query = mysql_query('SELECT * FROM `emails` WHERE `senddate` $row['email']);
$conID = $myApp->addCon($conDat);
$clist = array($conID);
$email = $myApp->sendEmail($clist, 'kamranasadi431@gmail.com', '~Contact.Email~',
'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');
}
?>What changes should be made in it?
Solution
- Use PDO, not mysql_* as they are deprecated and dangerous.
- Break up your code into functions, this makes it easier to call and test.
- Throw and catch exceptions.
- You can omit the ending ?>
NOTE: This is untested code as I don't have the isdk.php file.
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` 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, 'kamranasadi431@gmail.com', '~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());
}
}Code Snippets
<?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, 'kamranasadi431@gmail.com', '~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());
}
}Context
StackExchange Code Review Q#43394, answer score: 3
Revisions (0)
No revisions yet.