Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:42:23+00:00 2026-06-11T22:42:23+00:00

So I have been working on a project and I have a function that

  • 0

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!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:42:24+00:00Added an answer on June 11, 2026 at 10:42 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been working on this project for a short while now. I
Hey so I have been working on a project that I want to be
I have been working on a Java project in which the reports will be
I have been working on a project where I have a Worker class that
I have been working on a project in C# (.net4). Project pretty much allows
I have been working on a project on and off, but I haven't touched
Situation I have been working on a project lately where the UI development seems
I have been working on an opencv project for iOS. I was given a
I have been working on a huge ETL project with 150+ tables and during
I have been working on a C# 4.0 WPF project and need to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.