Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MYSQL - Can't find why this code sends mail more than twice
    primarykey
    data
    text
    <p>Alright, I'm starting to pull my hair and I need some help :)</p> <p>Here is my file which is used to select activated emails from users and send them some sort of newsletter.</p> <p><strong>Content of the newsletter.php</strong></p> <pre><code>&lt;?php //Include configuration file include 'config/config.php'; $pdo = new PDO("mysql:host=localhost;dbname=$db", $username, $password); $pdo-&gt;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; } } ?&gt; &lt;form action="newsletter.php" method="post"&gt; &lt;label&gt;Naslov newsletter-a: &lt;?php echo '&lt;span class="error"&gt;'.$errorSubject.'&lt;/span&gt;';?&gt;&lt;/label&gt; &lt;input type="text" class="linput rounded" name="newsletterSubject"&gt; &lt;label&gt;Sadržaj newsletter-a: &lt;?php echo '&lt;span class="error"&gt;'.$errorContent.'&lt;/span&gt;';?&gt;&lt;/label&gt; &lt;textarea name="newsletterContent" class="rounded"&gt;&lt;/textarea&gt;&lt;br&gt; &lt;input type="submit" class="submit button rounded" name="newsletterSend" value="Pošalji newsletter korisnicima"&gt; &lt;/form&gt; &lt;?php if (!$errorFlag) { echo ' &lt;div class="heading"&gt; &lt;h1&gt;Sending statistic&lt;/h1&gt; &lt;/div&gt;'; $query = $pdo-&gt;prepare('SELECT email FROM users WHERE active=:active'); $query-&gt;bindValue(':active', '1'); $query-&gt;execute(); $query-&gt;setFetchMode(PDO::FETCH_ASSOC); $i=1; while($row = $query-&gt;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 = '&lt;html&gt;&lt;body&gt;'; $message .= $newsletterContent; $message .= '&lt;/body&gt;&lt;/html&gt;'; mail($mail, $newsletterSubject, $message, $headers); $i++; } } } ?&gt; </code></pre> <p>After selecting active emails from database, array $to contains: </p> <pre><code>Array ( [0] =&gt; somemail1@domain.com [1] =&gt; somemail2@domain.com ) </code></pre> <p>And that is correct, but both emails will receive 2 emails, so 4 in total. Normally one email should receive one newsletter.</p> <p>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. <strong>So to sum up, this file sends two emails per one email in database.</strong></p> <p>I tried to create test file with same array and this is content of it:</p> <p><strong>test.php</strong></p> <pre><code>&lt;?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 = '&lt;html&gt;&lt;body&gt;'; $message .= $newsletterContent; $message .= '&lt;/body&gt;&lt;/html&gt;'; mail($mail, $newsletterSubject, $message, $headers); $i++; } ?&gt; </code></pre> <p>This test file sends normal email - one email per one email. So server configuration should be okay.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload