I am trying to pull out emails from a SQL database and then send an email to each of those emails using EWS via PHP. But when I put the PHP-EWS code https://github.com/jamesiarmes/php-ews/ inside of a while loop it gives me the error "Cannot redeclare class EwsSendEmail".
Here the layout of the code, it would look like I cannot have the class inside the while loop. But not sure how else I can get all the data from the SQL table and send a unique email for each and I am not very familiar with the use of a class. The code works fine if I only pull one record from the SQL by adding TOP 1 after SELECT. I do appreciate any help.
<?php
$sql = "SELECT FIELDNAME FROM TABLE";
$stmt = sqlsrv_query( $conn, $sql);
if ($stmt === false ) {
die (print_r( sqlsrv_errors(), true));
}
while ( $column = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH)) {
// Do some data processing here
require_once($ewspath.'EWSType/IncludetheEWSFilesHere.php');
class EwsSendEmail
{
protected function sendEmail()
{
$server = 'myserver';
$username = 'myuser';
$password = 'mypass';
$ews = new ExchangeWebServices($server, $username, $password);
// Process the EWS Code Here
$response = $ews->CreateItem($message);
//var_dump($response);
}
function __construct()
{
$this->sendEmail();
}
}
$page = new EwsSendEmail();
}
?>