Jump to content

Send message to all email addresses in database table


pcw
 Share

Recommended Posts

Hi, I got this script which I would like to send a message to all email addresses in the database or send to approved members only. Once I got the first part of this script working I can get the rest to work when sending to approved members.

 

Here is the form which passes the message to the script.

 

<?php 

include_once("data/server.php");

$all = 'unchecked';
$approved = 'unchecked';


if (isset($_POST['Submit1'])) {

$selected_radio = $_POST['Mailto'];

	if ($selected_radio == 'all') {
		$all = 'checked';

	}
	else if ($selected_radio == 'approved') {
		$approved = 'checked';
	}
}

?>

<div id="pageNav">
    <div id="sectionLinks"> <a href="admin.php?cmd=database&username=admin">Database</a> <a href="admin.php?cmd=uploads&username=admin">Uploads</a> <a href="admin.php?cmd=editTemplate&username=admin">Templates </a> <a href="admin.php?cmd=email&username=admin">Email</a> <a href="admin.php?cmd=messaging&username=admin">Messaging</a> <a href="admin.php?cmd=protected&username=admin">Protected Directory</a> <a href="admin.php?cmd=filesanddir&username=admin">Files and Directories</a> <a href="admin.php?cmd=usertracking&username=admin">User Tracking</a> <a href="admin.php?cmd=advanced&username=admin">Advanced</a> <a href="admin.php?cmd=uploads&username=admin">Uploads</a> <a href="admin.php?cmd=subscriptions&username=admin">Subscriptions</a> <a href="admin.php?cmd=applications&username=admin">Applications</a> <a href="admin.php?cmd=searchForms&username=admin">Search Form Builder</a> <a href="admin.php?cmd=categories&username=admin">Categories</a> <a href="admin.php?cmd=phpBB&username=admin">phpBB</a> <a href="admin.php?cmd=mySQL&username=admin">MySQL</a></div>
  </div>
  <div id="content">
    
    <div class="page">


<form action=templates/admin/msgtest.php method=POST>
<table>
<tr>
<td>Your Message:</td>
<tr><td> </td><td> </td></tr>
<tr><td><TEXTAREA NAME="message" COLS=40 ROWS=6></TEXTAREA></td></tr>
</table>
<table>
<tr>
<td><input type = 'Radio' Name ='Mailto' value= 'yes'
<?PHP print $all; ?> 
</td>
<td>Send Message to all members</td>
</tr>
<tr>
<td><input type = 'Radio' Name ='Mailto' value= 'yes'
<?PHP print $approved; ?>
</td>
<td>Send Message to Approved members only</td>
</tr>
<tr><td colspan=2><input type=submit name=submit value=Submit></td></tr>
</table>
</form>



</table>
    </div>
    
  </div>  
</div>


 

And here is the code I have got so far but it doesnt work

 

<?php

include_once("../../data/server.php");
include("../../data/mysql.php");

if (isset($_POST['Submit1'])) {

$selected_radio = $_POST['Mailto'];

	if ($selected_radio == 'all') {

$mysqlPassword = (base64_decode($mysqlpword));

$con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error());
mysql_select_db("$dbname", $con) or die(mysql_error());

$q2 = "select * from games_newsletter ";
$result = mysql_query("SELECT email FROM profiles");   

while ($email = mysql_fetch_assoc($result)) {

$to = $email;
$subject = "Test Message";
$body = "This is a test message";
$headers = "From: payments@tropicsbay.co.uk\r\n" .
     "X-Mailer: php";
if (mail($to, $subject, $body, $headers)) {
   
// Redirect back to manage page
header("Location: admin.php?cmd=messaging");


} else {

echo "Approval Message Failed";

}
}
mysql_close($con);
}
}

?>

 

As always, any help is much appreciated

 

Paul

Link to comment
Share on other sites

So what you need is:

 

FOR ALL USERS:

 

send.html

 

<form action="process.php" method="POST"><br />
<b>Send message to all users:</b>
<br />Subject: <input type="text" id="subject">
<br />Message:<br />
<input type="textarea" id="message" <br /><input type="Submit">
</form>

 

 

process.php

<?php 

include('headers.php');

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT * FROM users");

while($result = mysql_fetch_array($getusers)) { 
$emailaddress = $result['emailaddress'];

mail($emailaddress,"Subject","Message","From:whoever@whatever.com");
}

 

 

FOR AUTHENTICATED USERS ONLY:

 

send.html

 

<form action="process.php" method="POST"><br />
<b>Send message to authenticated users:</b>
<br />Subject: <input type="text" id="subject">
<br />Message:<br />
<input type="textarea" id="message" <br /><input type="Submit">
</form>

 

process.php

 

<?php 

include('headers.php');

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT * FROM users WHERE authenticated='yes'");

while($result = mysql_fetch_array($getusers)) { 
$emailaddress = $result['emailaddress'];

mail($emailaddress,"Subject","Message","From:whoever@whatever.com");
}

 

Hope this is what you want.

Link to comment
Share on other sites

I'd also add sleep(2); at the end inside the while loop so that you don't clog up your mail server or if you mail to the same email service it doesn't throw it up as spam because it sent 2 emails within under a second to two different clients. Just a suggestion.

Link to comment
Share on other sites

Hi guys,

 

Thanks for your replies. I have made a few changes, such as the select field in the message form instead of radio fields, but I am having some problems getting it to work. I have changed users to members as that is the name of the table in my database.

 

So here is what I have got:

 

Form

 

<div id="pageNav">
    <div id="sectionLinks"> <a href="admin.php?cmd=database&username=admin">Database</a> <a href="admin.php?cmd=uploads&username=admin">Uploads</a> <a href="admin.php?cmd=editTemplate&username=admin">Templates </a> <a href="admin.php?cmd=email&username=admin">Email</a> <a href="admin.php?cmd=messaging&username=admin">Messaging</a> <a href="admin.php?cmd=protected&username=admin">Protected Directory</a> <a href="admin.php?cmd=filesanddir&username=admin">Files and Directories</a> <a href="admin.php?cmd=usertracking&username=admin">User Tracking</a> <a href="admin.php?cmd=advanced&username=admin">Advanced</a> <a href="admin.php?cmd=uploads&username=admin">Uploads</a> <a href="admin.php?cmd=subscriptions&username=admin">Subscriptions</a> <a href="admin.php?cmd=applications&username=admin">Applications</a> <a href="admin.php?cmd=searchForms&username=admin">Search Form Builder</a> <a href="admin.php?cmd=categories&username=admin">Categories</a> <a href="admin.php?cmd=phpBB&username=admin">phpBB</a> <a href="admin.php?cmd=mySQL&username=admin">MySQL</a></div>
  </div>
  <div id="content">
    
    <div class="page">


<form action=templates/admin/msgtest.php method=POST>
<table>
<tr>
<td>Your Message:</td>
<tr><td> </td><td> </td></tr>
<tr><td>Subject:</td><td> <input type="text" id="subject"></td></tr>
<tr><td>Message</td><td>Message:<textarea id="message"></textarea></td></tr>
<tr><td>Send to:</td><td><select name=sendTo>
<option></option>
<option>All Members</option>
<option>Approved Members</option>
</select>
<tr><td colspan=2><input type=submit name=submit value=Submit></td></tr>
</table>
</form>



</table>
    </div>
    
  </div>  
</div>


 

Script

<?php

include_once("../../data/server.php");
include("../../data/mysql.php");

$sendTo = $_POST['sendTo'];

if ($sendTo == 'All Members') {

$mysqlPassword = (base64_decode($mysqlpword));

$con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error());
mysql_select_db("$dbname", $con) or die(mysql_error());

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT email FROM members");

while($result = mysql_fetch_array($getusers)) { 
$emailaddress = $result['emailaddress'];

mail($emailaddress,"$subject","$message","From:$adminEmail");
}
mysql_close($con);

} elseif ($sendTo == 'Approved Members') {

$mysqlPassword = (base64_decode($mysqlpword));

$con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error());
mysql_select_db("$dbname", $con) or die(mysql_error());

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT email FROM members WHERE approved='yes'");

while($result = mysql_fetch_array($getusers)) { 
$emailaddress = $result['emailaddress'];

mail($emailaddress,"$subject","$message","From:$adminEmail");

}

}
mysql_close($con);


?>

Thanks for your help

Link to comment
Share on other sites

Ok made a few changes now, but keep getting errors to say there is an unextpected { on line 29 and the 30 but I cant seem to work out where to put them.

 

<?php

include_once("../../data/server.php");
include("../../data/mysql.php");

$sendTo = $_POST['sendTo'];

if ($sendTo == "All Members") {

$mysqlPassword = (base64_decode($mysqlpword));

$con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error());
mysql_select_db("$dbname", $con) or die(mysql_error());

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT email FROM members");

while($result = mysql_fetch_array($getusers)) { 
$emailaddress = $result['email'];

$to = "$emailaddress";
$subject = "$subject";
$body = "$message";
$headers = "From: $adminEmail\r\n" .
     "X-Mailer: php";
mail($to, $subject, $body, $headers)
}
} elseif ($sendTo == "Approved Members") {

$mysqlPassword = (base64_decode($mysqlpword));

$con = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die(mysql_error());
mysql_select_db("$dbname", $con) or die(mysql_error());

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT email FROM members WHERE approved='yes'");

while($result = mysql_fetch_array($getusers)) { 
$emailaddress = $result['email'];

$to = "$emailaddress";
$subject = "$subject";
$body = "$message";
$headers = "From: $adminEmail\r\n" .
     "X-Mailer: php";
mail($to, $subject, $body, $headers)

}
mysql_close($con);

}

?>

 

Help please

Link to comment
Share on other sites

Worth mentioning is that the mail() function in php isn't very good for sending a lot of emails at once. From the manual entry:

 

  Note:

 

    It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

 

    For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.