Jump to content

Mass email to users in my database


unknown1

Recommended Posts

Hello all, I'm trying to create a script to send mass emails to users in my mysql database.

The issue I'm running into is it seems to either only send the email to the first email or last email ONLY and doesn't cycle through the email in the database like it should.

 

Can someone please explain a better way to write this and maybe let me know how to go about cycling through the emails the right way...

 

 

Thanks in advance.

 

<?php
session_start(); //allow sessions
include("config.php"); //get database information

if(!$_POST[send]){ //if the form was not submitted
        echo "<form action='' method='post' >
<table width='600' border='0' cellspacing='0' cellpadding='0' align='left'>
  <tr>
    <td height='35' colspan='2' align='left'><b>Send a email to all users</b></td>
  </tr>
  <tr>
    <td height='35' align='left'><b>Subject:</b></td>
    <td width='515' align='left'><input type='text' name='title' id='title' size='30' /></td>
  </tr>
  <tr>
    <td width='85' height='150' align='left' valign='top'><b>Message:</b></td>
    <td align='left' valign='top'><textarea name='msg' id='msg' cols='65' rows='9'></textarea></td>
  </tr>
  <tr>
    <td height='35'> </td>
    <td><input type='submit' name='send' id='send' value='Send' /></td>
  </tr>
</table>
</form>"; //return with the form
    }else{ //or if it was
    $supportEmail = 'support@mysite.com';
$title = strip_tags(htmlspecialchars($_POST[title]));
    $date    = date("F j, Y, g:i a");
    $message = stripslashes($_POST[msg]); //message
    $getusers = mysql_query("SELECT * FROM user"); 
$getemail = mysql_query("SELECT email FROM user"); 
$row = mysql_fetch_array($getemail);
$to=$row['email'];

    $link="<a href=\"http://mysite.com/contact.php\">Click here</a>";
        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)){ 

  
	  $send_contact = mail($to,$title,$body,$headers);
}
    
  $body = <<<EOD
<br><hr><br>

=============================== <br>
<br>
Subject: $title <br>
Message: $message <br><br>
This message has been sent by My SIte. Do not reply to this email. $link to contact support. <br>

=============================== <br><br>

EOD;
$headers = "From: $supportEmail\r\n";
$headers .= "Content-type: text/html\r\n";
$send_contact = mail($to,$title,$body,$headers);
// Check, if message sent to your email


if($send_contact){
	echo "<script language='javascript'>
                alert('Your site wide email has been sent.');
                window.location.href = 'http://mysite.com/admin_panel.php?page=mass_email';
              </script>";

}

        } 
    }

?> 

Link to comment
Share on other sites

$query = "SELECT email FROM users";
$result = mysql_query($query);

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

//send email
}

?

 

Because when I go off the 1st query and add the line $to=$row['email']; below the while loop I get the message like 12 time to the first email address only.

Link to comment
Share on other sites

so then send each email in the while loop? or append the to field so its like email1, emial2, email2, etc

 

Below is the current script and it is only sending the message to the last email in the database and skips the rest...

 

 

<?php
session_start(); //allow sessions
include("config.php"); //get database information

if(!$_POST[send]){ //if the form was not submitted
        echo "<form action='' method='post' >
<table width='600' border='0' cellspacing='0' cellpadding='0' align='left'>
  <tr>
    <td height='35' colspan='2' align='left'><b>Send a email to all users</b></td>
  </tr>
  <tr>
    <td height='35' align='left'><b>Subject:</b></td>
    <td width='515' align='left'><input type='text' name='title' id='title' size='30' /></td>
  </tr>
  <tr>
    <td width='85' height='150' align='left' valign='top'><b>Message:</b></td>
    <td align='left' valign='top'><textarea name='msg' id='msg' cols='65' rows='9'></textarea></td>
  </tr>
  <tr>
    <td height='35'> </td>
    <td><input type='submit' name='send' id='send' value='Send' /></td>
  </tr>
</table>
</form>"; //return with the form
    }else{ //or if it was
    $supportEmail = 'support@mysite.com';
$title = strip_tags(htmlspecialchars($_POST[title]));
    $date    = date("F j, Y, g:i a");
    $values  = array();
    $message = stripslashes($_POST[msg]); //message
    $getusers = mysql_query("SELECT * FROM user"); 
$getemail = "SELECT email FROM user"; 
$result = mysql_query($getemail);

$to=$row['email'];

    $link="<a href=\"http://mysite.com/contact.php\">Click here</a>";
        if(empty($message)){
            echo "<a href=\"history.go(-1)\">Go Back</a>You Can Not Send An Empty Message!";
        }else{
while ($row = mysql_fetch_array($result)){
$email = $row['email'];

//send email
}
    
  $body = <<<EOD
<br><hr><br>

=============================== <br>
<br>
Subject: $title <br>
Message: $message <br><br>
This message has been sent by My Site. Do not reply to this email. $link to contact support. <br>

=============================== <br><br>

EOD;
$headers = "From: $supportEmail\r\n";
$headers .= "Content-type: text/html\r\n";
$send_contact = mail($email,$title,$body,$headers);
// Check, if message sent to your email


if($send_contact){
	echo "<script language='javascript'>
                alert('Your site wide email has been sent.');
                window.location.href = 'http://mysite.com/x9y6uw5e8_/admin_panel.php?page=mass_email';
              </script>";

}

        } //end check for empty message
    } //end form check

?> 

Link to comment
Share on other sites

so then send each email in the while loop? or append the to field so its like email1, emial2, email2, etc

 

Below is the current script and it is only sending the message to the last email in the database and skips the rest...

 

 

<?php
session_start(); //allow sessions
include("config.php"); //get database information

if(!$_POST[send]){ //if the form was not submitted
        echo "<form action='' method='post' >
<table width='600' border='0' cellspacing='0' cellpadding='0' align='left'>
  <tr>
    <td height='35' colspan='2' align='left'><b>Send a email to all users</b></td>
  </tr>
  <tr>
    <td height='35' align='left'><b>Subject:</b></td>
    <td width='515' align='left'><input type='text' name='title' id='title' size='30' /></td>
  </tr>
  <tr>
    <td width='85' height='150' align='left' valign='top'><b>Message:</b></td>
    <td align='left' valign='top'><textarea name='msg' id='msg' cols='65' rows='9'></textarea></td>
  </tr>
  <tr>
    <td height='35'> </td>
    <td><input type='submit' name='send' id='send' value='Send' /></td>
  </tr>
</table>
</form>"; //return with the form
    }else{ //or if it was
    $supportEmail = 'support@mysite.com';
$title = strip_tags(htmlspecialchars($_POST[title]));
    $date    = date("F j, Y, g:i a");
    $values  = array();
    $message = stripslashes($_POST[msg]); //message
    $getusers = mysql_query("SELECT * FROM user"); 
$getemail = "SELECT email FROM user"; 
$result = mysql_query($getemail);

$to=$row['email'];

    $link="<a href=\"http://mysite.com/contact.php\">Click here</a>";
        if(empty($message)){
            echo "<a href=\"history.go(-1)\">Go Back</a>You Can Not Send An Empty Message!";
        }else{
while ($row = mysql_fetch_array($result)){
$email = $row['email'];

//send email
}
    
  $body = <<<EOD
<br><hr><br>

=============================== <br>
<br>
Subject: $title <br>
Message: $message <br><br>
This message has been sent by My Site. Do not reply to this email. $link to contact support. <br>

=============================== <br><br>

EOD;
$headers = "From: $supportEmail\r\n";
$headers .= "Content-type: text/html\r\n";
$send_contact = mail($email,$title,$body,$headers);
// Check, if message sent to your email


if($send_contact){
	echo "<script language='javascript'>
                alert('Your site wide email has been sent.');
                window.location.href = 'http://mysite.com/x9y6uw5e8_/admin_panel.php?page=mass_email';
              </script>";

}

        } //end check for empty message
    } //end form check

?> 

 

it only sends to one email because you keep overwriting the $email variable. Did you read my post. you should either store each email in an array, and use a foreach to loop through each email, and email each person, or concatenate each email to one to string.

while($row = mysql_fetch_array($query)){
$email = $row['email'];

send = mail($email,$title,$body,$headers);
}

 

obviously you would have to define the headers and body and stuff before you send them email

Link to comment
Share on other sites

so then send each email in the while loop? or append the to field so its like email1, emial2, email2, etc

 

Below is the current script and it is only sending the message to the last email in the database and skips the rest...

 

 

<?php
session_start(); //allow sessions
include("config.php"); //get database information

if(!$_POST[send]){ //if the form was not submitted
        echo "<form action='' method='post' >
<table width='600' border='0' cellspacing='0' cellpadding='0' align='left'>
  <tr>
    <td height='35' colspan='2' align='left'><b>Send a email to all users</b></td>
  </tr>
  <tr>
    <td height='35' align='left'><b>Subject:</b></td>
    <td width='515' align='left'><input type='text' name='title' id='title' size='30' /></td>
  </tr>
  <tr>
    <td width='85' height='150' align='left' valign='top'><b>Message:</b></td>
    <td align='left' valign='top'><textarea name='msg' id='msg' cols='65' rows='9'></textarea></td>
  </tr>
  <tr>
    <td height='35'> </td>
    <td><input type='submit' name='send' id='send' value='Send' /></td>
  </tr>
</table>
</form>"; //return with the form
    }else{ //or if it was
    $supportEmail = 'support@mysite.com';
$title = strip_tags(htmlspecialchars($_POST[title]));
    $date    = date("F j, Y, g:i a");
    $values  = array();
    $message = stripslashes($_POST[msg]); //message
    $getusers = mysql_query("SELECT * FROM user"); 
$getemail = "SELECT email FROM user"; 
$result = mysql_query($getemail);

$to=$row['email'];

    $link="<a href=\"http://mysite.com/contact.php\">Click here</a>";
        if(empty($message)){
            echo "<a href=\"history.go(-1)\">Go Back</a>You Can Not Send An Empty Message!";
        }else{
while ($row = mysql_fetch_array($result)){
$email = $row['email'];

//send email
}
    
  $body = <<<EOD
<br><hr><br>

=============================== <br>
<br>
Subject: $title <br>
Message: $message <br><br>
This message has been sent by My Site. Do not reply to this email. $link to contact support. <br>

=============================== <br><br>

EOD;
$headers = "From: $supportEmail\r\n";
$headers .= "Content-type: text/html\r\n";
$send_contact = mail($email,$title,$body,$headers);
// Check, if message sent to your email


if($send_contact){
	echo "<script language='javascript'>
                alert('Your site wide email has been sent.');
                window.location.href = 'http://mysite.com/x9y6uw5e8_/admin_panel.php?page=mass_email';
              </script>";

}

        } //end check for empty message
    } //end form check

?> 

 

it only sends to one email because you keep overwriting the $email variable. Did you read my post. you should either store each email in an array, and use a foreach to loop through each email, and email each person, or concatenate each email to one to string.

while($row = mysql_fetch_array($query)){
$email = $row['email'];

send = mail($email,$title,$body,$headers);
}

 

obviously you would have to define the headers and body and stuff before you send them email

 

No I didn't see that... Thanks!!

Link to comment
Share on other sites

Now I have a new issue...

After the 3rd message gets send the script stops and give me an error.

Warning: mail() [function.mail]: Failed to connect to mailserver at "relay-hosting.secureserver.net" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

 

 

Any ideas? or is this a server issue that limits to amount of emails sent out... if so how to I set a limit so it only sends out so many messages at a time or within a certain time frame or something.

 

 

 

Link to comment
Share on other sites

Do you understand arrays? Using mysql_fetch_assoc();

 

$sql = mysql_query("SELECT email FROM table")
while($line = mysql_fetch_assoc($sql)){

   $email[]  = $line;

}

 

If wrote out the array in code  this what email would be:

 

$email = array(1=>array('email'=>"email1@example.com"),
             2=>array('email'=>"email2@example.com"),
             3=>array('email'=>"email3@example.com")
            );

 

print_r($email) would show.

 

Array
(
    [1] => Array
        (
            [email] => email1@example.com
        )

    [2] => Array
        (
            [email] => email2@example.com
        )

    [3] => Array
        (
            [email] => email3@example.com
        )

)

 

 

 

Then you access each email like this.

foreach($email as $e){

//Remove whitespace in case there is some.
$e['email'] = trim($e['email']);

$headers =  "From: Example <example@example.com>\n"
                 ."Reply-To: example@example.com\n"
                 ."X-Mailer: PHP/".phpversion();
$message = "Hello this is message\n\n"
                 ."I am sending you\n\n";

mail($e['email'],"Title Test",$message,$headers);

}

 

Maby to avoid issues that your having put usleep() or sleep() at the end foreach, so its not sending them too fast?

 

foreach($email as $e){

//Remove whitespace in case there is some.
$e['email'] = trim($e['email']);

$headers =  "From: Example <example@example.com>\n"
                 ."Reply-To: example@example.com\n"
                 ."X-Mailer: PHP/".phpversion();
$message = "Hello this is message\n\n"
                 ."I am sending you\n\n";

mail($e['email'],"Title Test",$message,$headers);
// Stop from sending too fast.
usleep(500000); // wait for half a second
}

 

Link to comment
Share on other sites

  • 3 years later...

Hey guys.. could you explain the exact same way.. but with links to visit instead of emails???

 

like the post above of 10 pages it only visits the very last 10th page.

 

 

im using..

 

header('location:' "$email"); // which where it says email,.. for mine is $link

 

and also including the usleep would help.

 

Im only getting.. www. Array .com when I try to compile it.

Edited by 0o0o0
Link to comment
Share on other sites

I will admit im not good with arrays, .. attempted this.. Its spaghetti from so many attempts.. I'll fix it up nice when I get it workin.. still only visits 10th page only.. (table and db replaced with.. example names).

 

$sql = mysql_query("SELECT pagenum FROM forumtest");

while($line = mysql_fetch_assoc($sql)){	

$pagenum[] = $line;	
}

foreach($pagenum as $e){	

$d = 'http://www.exampleforum.com/php.aspx?Id=';
$f = '&order=1';

$e = $e['pagenum'];
$g = "$d$e$f";

header('location:'.$g);

usleep(3000000); // 3000000 wait for 3 seconds

}

 

ok fine what im trying to do is make a single button or page url submission, for after I create websites for clients.. simply click and she goes. The ones out there now are sending urls to spammers, and emails to spammer companies.. When you do a website for someone and 3 weeks later they say the mail is discusting, repetitive they dont know whats business and whats junk. I just want to make my own private and control the places it submits too. Without giving some third party my clients new url and email address.

 

help me thanks!

Edited by 0o0o0
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.