pcw Posted April 24, 2011 Share Posted April 24, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/ Share on other sites More sharing options...
nzol Posted April 25, 2011 Share Posted April 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205752 Share on other sites More sharing options...
Tonic-_- Posted April 25, 2011 Share Posted April 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205801 Share on other sites More sharing options...
pcw Posted April 25, 2011 Author Share Posted April 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205830 Share on other sites More sharing options...
fugix Posted April 25, 2011 Share Posted April 25, 2011 Firs thong that comes to my mind is to create a while loop to draw out allot the email addresses that you want to send to. Then create the email Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205837 Share on other sites More sharing options...
pcw Posted April 25, 2011 Author Share Posted April 25, 2011 Doesnt this already do that? $getusers = mysql_query("SELECT email FROM members"); while($result = mysql_fetch_array($getusers)) { $emailaddress = $result['emailaddress']; Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205844 Share on other sites More sharing options...
pcw Posted April 25, 2011 Author Share Posted April 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205880 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 mail($to, $subject, $body, $headers) // <--- missing line termination on both lines that use mail() function; Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205931 Share on other sites More sharing options...
pcw Posted April 25, 2011 Author Share Posted April 25, 2011 Thanks Pikachu2000, with that and changing id to name in the form it works :-) Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205982 Share on other sites More sharing options...
Pikachu2000 Posted April 25, 2011 Share Posted April 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1205986 Share on other sites More sharing options...
gideon107540 Posted November 25, 2023 Share Posted November 25, 2023 <?php // Database connection $servername = "localhost"; $username = "u880577714_enbullsnbears"; $password = "Olagoro4"; $dbname = "u880577714_enbullsnbears"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Retrieve email addresses $stmt = $conn->prepare("SELECT email FROM user"); $stmt->execute(); $emails = $stmt->fetchAll(PDO::FETCH_COLUMN); // Send emails $subject = $_POST['subject']; $message = $_POST['message']; $headers = "From: support@en.bullsnbearstraders.online"; foreach ($emails as $email) { mail($email, $subject, $message, $headers); echo "Email sent to: $email<br>"; } } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/234599-send-message-to-all-email-addresses-in-database-table/#findComment-1613110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.