Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 469
0 Users 6 Guests Online

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


If you get any errors you can post a comment containing the error.
ShadowMage
Author:
Views:
2400
Rating:
Posted on Sunday 20th May 2007 at 10:35 PM
Diablosblizz
Diablosblizz
Yah I got that now.
Posted on Sunday 20th May 2007 at 09:21 PM
SkillMaster
SkillMaster
whoops
($logged[userlevel] !==6) as opposed to (!$logged[userlevel] == 6)
Posted on Sunday 20th May 2007 at 09:20 PM
SkillMaster
SkillMaster
@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
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
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
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
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
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
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
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?