Jump to content

Recommended Posts

Im having a problem with a mailing list i need to make for someone,

I have to have the same email sent to all the emails in the database

what am i doing wrong

<html>
<head><title>Send Emails</title></head>
<body>
<?php

if(empty($_POST['emailcontents'])) {
        echo "Please place email contents below";
} else {
        $host = "localhost";
        $db = "massmailer";
        $user = "maileradmin";
        $pass = "mailerpass";

        mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer');
        mysql_select_db($db) or die('Could not Select the Database');

        $result = mysql_query("SELECT * FROM emails") or die("Could not select table");
        mysql_fetch_assoc($result);

        for($i=0; $i<=1000; $i++) {
        echo "Emailing: $i";
        mail($result['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: testing@test.com") or die("Could not send email");
        echo "Email Successfully Sent to".$contact['emailaddr'];
        }

}
?>
<form action="" method="post">
        <label>Contents: </label><br />
        <textarea name="emailcontents"></textarea><br />
        <input type="submit" value="Send Email to All" name="btnsendemail" />
</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/
Share on other sites

I solved it :)

<html>
<head><title>Send Emails</title></head>
<body>
<?php

if(empty($_POST['emailcontents'])) {
        echo "Please place email contents below";
} else {
        $host = "localhost";
        $db = "massmailer";
        $user = "maileradmin";
        $pass = "mailerpass";

        mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer');
        mysql_select_db($db) or die('Could not Select the Database');

        $result = mysql_query("SELECT * FROM emails") or die("Could not select table");
        $row = mysql_fetch_assoc($result);
        $nrows = mysql_num_rows($result);
        for($i=1; $i<=$nrows; $i++) {
        mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@spacenetmedia.com") or die("Could not send email");
        echo "Email Successfully Sent to: ".$row['emailaddr']."<br />";
        }

}
?>
<form action="" method="post">
        <label>Contents: </label><br />
        <textarea name="emailcontents"></textarea><br />
        <input type="submit" value="Send Email to All" name="btnsendemail" />
</form>

</body>
</html>

Your code would only send to 1 person (but the person would be e-mailed 1,000 times).  I'm a little concerned with help here as I just want to make sure that you are not spamming.  This concern comes from the loop maxing out at 1,000 (meaning that you are just gonna send 1,000) e-mails without any concern for how many people are actually on your mailing list.  In any case here is some code broken up and explained line by line...

 

//create the mysql query
$result = mysql_query("SELECT * FROM emails") or die("Could not select table");

//this sends the actual query to the database and stores the entire result in the "$result" variable
//note that this will have to be broken up later into individual lines
$result = mysql_query($query);
        
//this just counts the actual number of matches from the query
$num_results = mysql_num_rows($result);

//create and initialize a counter
$count = 0;

//this for loop uses the number of matches and then goes through them one at a time
for($i = 0; $i < $num_results; $i++)
{
   //this stores a single row of results in the variable "$row"
   $row = mysql_fetch_array($result);

   //increase count number and print to screen
   $count++;
   echo "<b>Emailing user number:</b> $count <br />";

   //generate and send e-mail
   mail($row["emailaddr"], "Test from Frank", $_POST['emailcontents'], "From: testing@test.com") or die("Could not send email");
   echo "Email Successfully Sent to ".$row["emailaddr"]."<br /><br />\n";

} //end of for($i = 0; $i < $num_results; $i++)

 

If this code doesn't work then you need to look specifically at the name of the database and/or the column name(s).  Your code would be more efficient as well if instead of using the "SELECT * FROM emails"  you used a specific column name like "SELECT emailaddr FROM emails"

Sorry... while I was writing the code for my response you had already solved it.  Hopefully this helps someone else at the very least =).

 

Also for future reference please make detailed notes as to what the fault(s) were with your code and exactly what you did to change them.  This will allow future users to learn easier from these forums =)

Im having a new problem, now how do i make it go through my list of emails, right now all the emails go to just one email

<html>
<head><title>Send Emails</title></head>
<body>
<?php

if(empty($_POST['emailcontents'])) {
        echo "Please place email contents below";
} else {
        $host = "localhost";
        $db = "massmailer";
        $user = "maileradmin";
        $pass = "mailerpass";

        mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer');
        mysql_select_db($db) or die('Could not Select the Database');

        $result = mysql_query("SELECT * FROM emails") or die("Could not select table");
        $row = mysql_fetch_assoc($result);
        $nrows = mysql_num_rows($result);
        for($i=1; $i<=$nrows; $i++) {
        mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@spacenetmedia.com") or die("Could not send email");
        echo "Email Successfully Sent to: ".$row['emailaddr']."<br />";
        }

}
?>
<form action="" method="post">
        <label>Contents: </label><br />
        <textarea name="emailcontents"></textarea><br />
        <input type="submit" value="Send Email to All" name="btnsendemail" />
</form>
<a href="addemail.php">Add an Email to the List</a>
</body>
</html>

You have this marked as "SOLVED" yet you still have a question so I'm going to assume that you mistakenly marked it and have yet to figure out the issue...

 

The problem with your code is that you only fetch a row from the result 1 time before the loop.  You need to also fetch a row in the loop so as it loops through it grabs the very next email in the list.

 

So... either used the code that I already provided you OR you could modify your loop to look like this...

 

<?php

for($i=1; $i<=$nrows; $i++) 
{
   mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@spacenetmedia.com") or die("Could not send email");
   echo "Email Successfully Sent to: ".$row['emailaddr']."<br />";
   $row = mysql_fetch_assoc($result);
}
?>

 

Let me know if you need anything else.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.