Mass Email


In this short tutorial you will learn how to send a mass email to your website members.

Create a new file and call it: massmail.php

In this file you will want to code something like:
PHP Code
  1. <?php
  2. session_start(); //allow sessions
  3. include("config.php"); //get database information
  4. if($logged[username] && $logged[userlevel] != 6){ //check if logged in and admin
  5. echo "You Are Not An Admin!"; //the person is not an admin
  6. }elseif(!$logged[username]){ //check if logged in or not
  7. echo "You Are Not Logged In"; //user not logged in
  8. }else{ //said user is logged in and is an admin
  9. if(!$_POST[send]){ //if the form was not submitted
  10. echo "<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
  11. <b>Subject</b>:
  12. <input type=\"text\" name=\"title\" value=\"Untitled Message\" size=\"15\">
  13. <b>Message</b>:
  14. <textarea cols=\"58\" rows=\"10\" name=\"msg\"></textarea>
  15. <input type=\"submit\" name=\"send\" value=\"Send Message\">
  16. </form>"; //return with the form
  17. }else{ //or if it was
  18. $title = strip_tags(htmlspecialchars($_POST[title])); //title
  19. $message = stripslashes($_POST[msg]); //message
  20. $getusers = mysql_query("SELECT * FROM members"); //get all users
  21. $headers = "From: NoReply@$_SERVER[SERVER_NAME]"; //who is it from?
  22. if(empty($message)){
  23. echo "<a href=\"history.go(-1)\">Go Back</a>You Can Not Send An Empty Message!";
  24. }else{
  25. while($r = mysql_fetch_array($getusers)){ //lets repeat for each email found
  26. mail($r[email], $title, $message, $headers); //send mail to user
  27. } //end while
  28. echo "<a href=\"history.go(-1)\">Go Back</a>Email Sent!"; //success!
  29. } //end check for empty message
  30. } //end form check
  31. } //end login and level check
  32. ?>


If you get any errors you can post a comment containing the error.
ShadowMage's Avatar
Author:
Views:
2,675
Rating:
Posted on Sunday 20th May 2007 at 10:35 PM
Diablosblizz
Diablosblizz's Avatar
Yah I got that now.
Posted on Sunday 20th May 2007 at 09:21 PM
SkillMaster
SkillMaster's Avatar
whoops
($logged[userlevel] !==6) as opposed to (!$logged[userlevel] == 6)
Posted on Sunday 20th May 2007 at 09:20 PM
SkillMaster
SkillMaster's Avatar
@Diabloblizz see the "!" means not so if(!$logged[userlevel] == 6) means if the user is not level 6
Posted on Saturday 19th May 2007 at 01:56 PM
DanielXP
DanielXP's Avatar
Updated all of the errors.

Before posting scripts please make sure,
1) It goes with our user system
2) You have test it
3) It works
Posted on Friday 18th May 2007 at 10:11 PM
Diablosblizz
Diablosblizz's Avatar
Also, to remove the slashes replace:

Code
$message = $_POST[msg]; //message


With:

Code
$message = stripslashes($_POST[msg]); //message


Tada, no more 's!
Posted on Friday 18th May 2007 at 09:56 PM
Diablosblizz
Diablosblizz's Avatar
Code
if($logged[username] && $logged[level] !== 6){ //check if logged in and admin
echo "You Are Not An Admin!"; //the person is not an admin


Yes, admin level is 6, but if you have your user on 6 then you'll show them the NOT A ADMIN page.
Posted on Friday 18th May 2007 at 09:56 PM
ShadowMage
ShadowMage's Avatar
Also!

where ever you see:
Code
$logged[level]


replace with:
Code
$logged[userlevel]
Posted on Friday 18th May 2007 at 09:54 PM
ShadowMage
ShadowMage's Avatar
Find:
Code
while($r = mysql_fetch_array($getusers)){ //lets repeat for each email found
mail($r[email], $title, $message, $headers); //send mail to user
echo "<a href="history.go(-1)">Go Back</a>Email Sent!"; //success!
} //end while


Replace With:
Code
while($r = mysql_fetch_array($getusers)){ //lets repeat for each email found
mail($r[email], $title, $message, $headers); //send mail to user
} //end while
echo "<a href="history.go(-1)">Go Back</a>Email Sent!"; //success!


the second one find:
Code
$message = nl2br($_POST[msg]); //message


Replace With:
Code
$message = $_POST[msg]; //message


For the RMB usersystem level 6 is admin and thats what i have on there.
Posted on Friday 18th May 2007 at 09:49 PM
Diablosblizz
Diablosblizz's Avatar
Three problems I have found in this code:

1. when you send the message I get this:

Code
Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!Go BackEmail Sent!


Thats 10 - I have 10 members, so if I have 1000 members, that will show up 1000 times :|.

2. When you do a space (enter) in the message I get:

Code
If you are seeing this message, and are not a admin / mod - you shouldn't be registered. Your ACCOUNT has been banned.<br />
<br />
KIDDING! This is a mass email, and I am trying it out!


The <br />'s. Thats not good.

3. The one above this comment.
Posted on Friday 18th May 2007 at 09:42 PM
Diablosblizz
Diablosblizz's Avatar
This does not work for me. Shouldn't:

PHP Code
  1. if($logged[username] && $logged[level] !== 6){


Shouldn't the 6 be a 2 because 6 = admin and 2 = member.

I say this because after words you say that the user is NOT a admin, which they are if they're user level is 6. I never tried fixing it because I edited for my use.

Isn't this right?