Question

OK I have a simple mail to where I am trying to email all my members based on a simple query. Problem is I must be missing something due to the form just reloads and dose not send anything.

email.php

<form method="post" action="massemail.php">

        Subject:</br>
        <input type="text" name="subject" /><br />

        Message:</br>
        <textarea name="details"></textarea><br />

        <input type="submit" value="Send"/>
    </form>

massemail.php

require 'core/init.php';
require_once "email.php"; 

$message = check_input($_POST['details']);
$subject = check_input($_POST['subject']);

$results = mysql_query('SELECT email from users');

while(list($email) = mysql_fetch_row($results))
{
  mail($email, '$subject', '$message');
}

have tried

    <?php
require 'core/init.php';
require_once "email.php"; 

$message = "test";
$subject = "123";

if ($results = mysql_query('SELECT email from users')) {

    while(list($email) = mysql_fetch_row($results)) {

  mail($email, '$subject', '$message');
}
?>
Was it helpful?

Solution

First, you need to extract the email addresses from each row instead of using the row itself. That can easily be achieved with $row[0] in your case since your query has always one column, though you should probably try to use mysqli instead of the deprecated mysql.

Second, I would recommend calling mail() once only, to send one message to all people rather than one email per person! Why do you want to send the same email again and again like this? It is possible to use a comma-separated list of emails, as seen on the PHP: mail - Manual.

If you want to use mysql, your code could look something like this with PHP Arrays and the handy implode function:

require 'core/init.php'; // calls mysql_connect(...)
require_once "email.php";

$message = check_input($_POST['details']);
$subject = check_input($_POST['subject']);

if (mysql_ping()) { echo 'Ping worked'; } else { echo 'Ping failed'; } // DEBUG
$results = mysql_query('SELECT email from users');

$emails = array();
while($row = mysql_fetch_row($results))
{
    array_push($emails, $row[0]);
}
echo implode(", ", $emails); // DEBUG
if (!empty($emails)) {
    mail(implode(", ", $emails), $subject, $message);
}

It seems to me (while chatting with you) that the whole mechanism of HTML Forms + PHP (GET and/or POST) is not clear to you. You should probably read about all this a bit more, and hopefully the code below helps you:

<?php
// Put requires here
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Demo form methods</h1>
<?php
if ((isset($_POST['subject']) && isset($_POST['body'])) || (isset($_GET['subject']) && isset($_GET['body']))) { 

    // Your code for sending an email could be here
    ?>

<h2>RESULTs</h2>
<ul>
    <li>POST
        <ul>
            <li>suject: <?php echo $_POST['subject']; ?></li>
            <li>suject: <?php echo $_POST['body']; ?></li>
        </ul>
    </li>
    <li>GET
        <ul>
            <li>suject: <?php echo $_GET['subject']; ?></li>
            <li>suject: <?php echo $_GET['body']; ?></li>
        </ul>
    </li>
</ul>

    <?php
    // The code above is just to show you how the variables in GET/POST work

 } else { 

    // Below are two formulaires, you can keep one only of course!
    ?>

<h2>FORMs</h2>
<h3>GET method</h3>
<form method="get" action="email.php">
    Subject: <input name="subject" type="text"><br>
    Body: <input name="body" type="text"><br>
    <input type="submit" value="Send with GET">
</form>
<h3>POST method</h3>
<form method="post" action="email.php">
    Subject: <input name="subject" type="text"><br>
    Body: <input name="body" type="text"><br>
    <input type="submit" value="Send with POST">

</form>
<?php } ?>
</body>
</html>

OTHER TIPS

It's not sending the email because you are not parsing the query results properly.

//connect to MySQL using following line. This is the NEW and better way of doing it.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if ($result = $mysqli->query('SELECT email from users')) { 
        while($row = $result->fetch_object()){ 
            mail($row->email, $subject, $message);
        } 
    } 
    $result->close(); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top