Reputation: 2801
So I have been working on a project and I have a function that will send emails out to employees whose employer is chosen. Now this is working except the mail function (I am using winhost and we need to include Mail.php in order for the mailing function to work) It will sometimes send 3 emails out instead of 2 and sometimes 1 email instead of 2.
The code :
if (isset($_POST['openemailmem'])){
$memberuser = $_POST['openemailmemusername'];
$sql = "SELECT email, username, password, status FROM csvdata WHERE memberview =:user ";
$getinfo=$DBH->prepare($sql);
$getinfo->execute(array(':user' => $memberuser));
while ($row = $getinfo->fetch(PDO::FETCH_ASSOC)) {
$check = $row;
$newEmployeeEmail = $check['email'];
$csvusername = $check['username'];
$password = $check['password'];
$status = $check['status'];
if ($status == "Open"){
echo "tesing";
$from = "the email of where it is coming from is here but i removed";
$to = $newEmployeeEmail;
if (!empty($_POST['cc'])){
$cc = $_POST['cc'];
}
if (!empty($_POST['ccsend'])){
$cc = $_POST['ccsend'];
$to .= ", $cc";
}
$subject = "removed msg";
$body = "removed msg";
$host = "i removed";
$username = "i removed";
$password = "i removed";
$headers = array ('From' => $from, 'To' => $to,'Cc' => $cc, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
}
}
header("Location: I removed this.php?getmsg=12");
exit;
}
Thank you for all your time!!!
Upvotes: 0
Views: 224
Reputation: 11485
Your solution is most likely in your logs. If exceptions are thrown, you can find them in there.
Additionally a suggestion:
Add logging before and after the loop. So your example becomes:
if (isset($_POST['openemailmem']))
{
$memberuser = $_POST['openemailmemusername'];
$sql = "SELECT email, username, password, status "
. "FROM csvdata WHERE memberview =:user ";
$getinfo = $DBH->prepare($sql);
$getinfo->execute(array(':user' => $memberuser));
$log_file = "/home/my/transaction.log";
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "Starting loop...";
error_log($message, 3, $log_file);
while ($row = $getinfo->fetch(PDO::FETCH_ASSOC))
{
$check = $row;
$newEmployeeEmail = $check['email'];
$csvusername = $check['username'];
$password = $check['password'];
$status = $check['status'];
if ($status == "Open")
{
echo "tesing";
$from = "the email of where it is coming from is here but i removed";
$to = $newEmployeeEmail;
if (!empty($_POST['cc']))
{
$cc = $_POST['cc'];
}
if (!empty($_POST['ccsend']))
{
$cc = $_POST['ccsend'];
$to .= ", $cc";
}
$subject = "removed msg";
$body = "removed msg";
$host = "i removed";
$username = "i removed";
$password = "i removed";
$headers = array(
'From' => $from,
'To' => $to,
'Cc' => $cc,
'Subject' => $subject,
);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "Before connecting to server - " . $to;
error_log($message, 3, $log_file);
$smtp = Mail::factory(
'smtp',
array(
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password,
)
);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "After connecting to server - " . $to;
error_log($message, 3, $log_file);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "Before sending email - " . $to;
error_log($message, 3, $log_file);
$mail = $smtp->send($to, $headers, $body);
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "After sending email - " . $to;
error_log($message, 3, $log_file);
}
}
$now = "[" . date("Ymd-His") . "] ";
$message = $now . "End of loop";
error_log($message, 3, $log_file);
header("Location: I removed this.php?getmsg=12");
exit;
}
You might find that your function times out, depending of how long it takes the mailer to dispatch the email to the relevant host.
From what I see for every user there is a (potentially) unique connection to a mail server and then the email is sent. If for whatever reason there is a delay in establishing that connection then your script could very well time out.
You can adjust the error logging method above to use microtime
so that you can see the real time between each iteration of your code - of course you will need to add more logging in between blocks of code.
If you find out that it is indeed a delay in the mail server connection etc. you could (if your requirements allow you to) connect once to one server and send your emails from there.
HTH
Upvotes: 1