Sauced Apples
Sauced Apples

Reputation: 1173

Sending newsletter to all emails from db

I am trying to implode a variable, am I doing it right? The objective is to get all the emails from a table and then go through them one by one.

The page just displays white.

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include_once '../includes/conn.php';

if(!$user->is_loggedin()){
    $user->redirect('../users/login.php');
}

$stmt = $conn->prepare("SELECT * FROM newsletter");
$stmt->execute();


while ($userRow = $stmt->fetch(PDO::FETCH_ASSOC)){
    $addresses[] = $userRow['email'];
}   
$to = implode(",", $addresses);
$subject = "";
$from = "[email protected]";
$headers "From Epic Owl's Newsletter" . $from;
mail($to, $subject, $message, $headers);
?>

Upvotes: 0

Views: 53

Answers (1)

Twisty
Twisty

Reputation: 30893

Send each message uniquely, if you're sending to an entire list:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include_once '../includes/conn.php';

if(!$user->is_loggedin()){
    $user->redirect('../users/login.php');
}

$stmt = $conn->prepare("SELECT * FROM newsletter");
$stmt->execute();

while ($userRow = $stmt->fetch(PDO::FETCH_ASSOC)){
    $to = $userRow['email'];
    $subject = "";
    $from = "[email protected]";
    $headers = "From Epic Owl's Newsletter" . $from;
    mail($to, $subject, $message, $headers);
}
?>

Also if your MTA has limits, you can stagger the sending. For example, if the limit is 500/hour, send 100/15min. This will ensure you do not exceed the limit. I do hope you have an opt-out on there too and your list is Double Opt-In verified. Welcome to becoming a potential Spammer.

Upvotes: 1

Related Questions