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 8380093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:22:08+00:00 2026-06-09T16:22:08+00:00

Alright, I’m starting to pull my hair and I need some help :) Here

  • 0

Alright, I’m starting to pull my hair and I need some help 🙂

Here is my file which is used to select activated emails from users and send them some sort of newsletter.

Content of the newsletter.php

<?php
//Include configuration file
include 'config/config.php'; 
$pdo = new PDO("mysql:host=localhost;dbname=$db", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Define messages
//Error messages
define('ERROR_MESSAGE_SUBJECT', 'Enter subject');
define('ERROR_MESSAGE_CONTENT', 'Enter some content');

//Define variables
$errorFlag = false;
$to = array();

//Grab variables
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $newsletterSubject = check_input($_POST['newsletterSubject']);
    $newsletterContent = $_POST['newsletterContent'];       

    if(!$newsletterSubject) {
        $errorSubject = ERROR_MESSAGE_SUBJECT;  
        $errorFlag = true;
    }
    if(!$newsletterContent) {
        $errorContent = ERROR_MESSAGE_CONTENT;
        $errorFlag = true;  
    }
}
?>
<form action="newsletter.php" method="post">
    <label>Naslov newsletter-a: <?php echo '<span class="error">'.$errorSubject.'</span>';?></label>
    <input type="text" class="linput rounded" name="newsletterSubject">

    <label>Sadržaj newsletter-a: <?php echo '<span class="error">'.$errorContent.'</span>';?></label>
    <textarea name="newsletterContent" class="rounded"></textarea><br>

    <input type="submit" class="submit button rounded" name="newsletterSend" value="Pošalji newsletter korisnicima">
</form>
<?php
                if (!$errorFlag) {                  
                    echo '
                        <div class="heading">
                            <h1>Sending statistic</h1>
                        </div>';

                    $query = $pdo->prepare('SELECT email FROM users WHERE active=:active');
                    $query->bindValue(':active', '1');  
                    $query->execute();
                    $query->setFetchMode(PDO::FETCH_ASSOC);
                    $i=1;
                    while($row = $query->fetch()) { 
                        $to[] = $row['email'];
                    }
                    print_r($to);

                    if(!empty($to)) {
                        foreach($to as $mail) {
                            $headers = "From: " . $fromEmail . "\r\n";
                            $headers .= "Reply-To: ". $fromEmail . "\r\n";
                            $headers .= "MIME-Version: 1.0\r\n";
                            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                            $message = '<html><body>';
                            $message .= $newsletterContent;
                            $message .= '</body></html>';
                            mail($mail, $newsletterSubject, $message, $headers);
                            $i++;
                        }
                    }
                }
              ?>

After selecting active emails from database, array $to contains:

Array ( [0] => somemail1@domain.com [1] => somemail2@domain.com ) 

And that is correct, but both emails will receive 2 emails, so 4 in total. Normally one email should receive one newsletter.

And there is also something else strange, when first newsletter is received, it contains subject and message. However second newsletter doesnt contain anything except ‘to’ field.
So to sum up, this file sends two emails per one email in database.

I tried to create test file with same array and this is content of it:

test.php

<?php
$fromEmail = 'from@mydomain.com';
$to = array('somemail1@domain.com', 'somemail2@domain.com');
print_r($to);

foreach($to as $mail) {
    $headers = "From: " . $fromEmail . "\r\n";
    $headers .= "Reply-To: ". $fromEmail . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $message = '<html><body>';
    $message .= $newsletterContent;
    $message .= '</body></html>';
    mail($mail, $newsletterSubject, $message, $headers);
    $i++;
}   
?>

This test file sends normal email – one email per one email. So server configuration should be okay.

  • 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-09T16:22:09+00:00Added an answer on June 9, 2026 at 4:22 pm

    It looks like it’s because your send code isn’t inside the code block which checks that it is POST, so it sends once when you load the page and again when you fill in the form and submit it.

    Move the whole if (!$errorFlag) block into the if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) block.

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

Sidebar

Related Questions

Alright so i need to open a .txt file that will be created in
Alright, hoping someone can help. I've got a page used to search for employees.
Alright I don't see why this isnt working. It seems pretty simple. Here is
Alright I created some custom classes for my project sourced from this tutorial ,
Alright, here I am again trying to write code from scratch and I can't
Alright, here's what I'm trying to do. I'm attempting to write a quick build
Alright, here is my problem i'm trying to solve. I have an index page
Alright here is what I am attempting to do. I have a form written
Alright, I or someone I work with broke the syntax here somewhere, and I'm
Alright, I have some questions... When you have a moment, can you take a

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.