Jump to content

PHP Mass Mailer


Liam-a-

Recommended Posts

this is my code that i have, could anyone help me. its not sending to any email that is stored in my database :(

<?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

You need to structure your code, with proper indenting, so it is easier to follow.

 

Also, turn on error reporting while in development, so you can see any error messages. Put this at the very beginning of your script.

error_reporting(E_ALL);
ini_set('display_errors', 1);
Here is your code, with line numbers and proper indents:

<?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
Line 5: This should be producing a warning or two. I would change it to if (! isset($_POST['send']))

 

Line 29: $values is defined but never used.

 

Line 30: You don't need strip_slashes here unless magic_quotes_gpc is on (and it should never be on).

 

Line 31: You execute a query here, but never use the results. This is a complete waste.

 

Line 35: You are trying to get the email address from $row but it is not defined.

 

Line 38: You should check for this before you bother executing the query.

 

Lines 41 - 45: HERE IS YOUR PROBLEM. You are walking through the result (list of email addresses) but you are not sending the email in the loop. So, nothing is getting sent.

 

Lines 47-71: This code would send the email, but it is outside the loop. So, if it is sending any emails, it is sending ONLY to the LAST address retrieved from the database.

 

You really need to restructure this code:

<?php
// SETUP - Error reporting, session, config

// POSTED? - If the form was posted, process the user input
	// Check ALL fields for valid data: title, msg
	// VALID DATA? - If the data is valid, send the emails
		// Build the "constant" (common) portions of the email (headers, body, from, etc)
		// Issue the query and loop through the results
			// For each row, build the email and call mail() to send it
				// Keep track of any failures
			// End loop
			// Compose a success message
	// ELSE - Data is not valid
		// Compose an error message
// ELSE - Not Posted
	// Show the success or error message (if present)
	// Show the form
Link to comment
Share on other sites

Can I see your code?

<?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

It sounds like, you didn't even bother trying to read what David wrote :confused:

We are not here to re-write your entire code, just to guide you through it. 

Try to debug the script step by step as David is already suggested and come back again with some specific question.

Edited by jazzman1
Link to comment
Share on other sites

It sounds like, you didn't even bother trying to read what David wrote :confused:

We are not here to re-write your entire code, just to guide you through it. 

Try to debug the script step by step as David is already suggested and come back again with some specific question.

I have been through my debug process on my server, currently cant get access to that code due to not been at home... i followed all of the steps that i was shown and it still didnt work.. i removed the things that i did not need like suggested. it only sends to last email in server not the loop.

Link to comment
Share on other sites

Add this:

$result = mysql_query($getemail);
 
// here

while ($row = mysql_fetch_assoc($result)) {
    $to=$row['email'];
}

echo $to; exit; 

OR, 

$to = array();
while ($row = mysql_fetch_assoc($result)) {
    $to[] = $row['email'];
}

echo '<pre>'.print_r($to, true).'</pre>'; 
		

See the output and post the result here.

Link to comment
Share on other sites

If the emails are coming from your database in the array(), just implode them.

 

For example:

$to = array('example_1@example.com,example_2@example.com,example_3@example.com,example_4@example.com,example_5@example.com');

$emails= implode(',', $to)."\n";

 if(mail($emails, $title,$body,$headers)) {
  echo 'OK.......mails was sent'; // redirect the page if you want it
 } else {
   echo 'ERROR: .........mails failed';   
 }
Link to comment
Share on other sites

At the risk of repeating myself:

 

...

 

Lines 41 - 45: HERE IS YOUR PROBLEM. You are walking through the result (list of email addresses) but you are not sending the email in the loop. So, nothing is getting sent.

 

Lines 47-71: This code would send the email, but it is outside the loop. So, if it is sending any emails, it is sending ONLY to the LAST address retrieved from the database.

 

...

* Line numbers may have changed, I didn't bother to check

 

The function mail is the one that SENDS emails. Since it is NOT IN THE LOOP it is NOT GOING TO SEND FOR EVERY RECORD FROM THE DATABASE.

 

You even have a comment INSIDE OF THE LOOP that says //send email. That is where you need to, uhh, send the email!!

Link to comment
Share on other sites

Why in the loop David? Call the mail function only once and implode outgoing addresses as in my example above.

Then ALL emails will contain ALL of the email addresses you are sending to. Effectively giving everyone on your email list a list of valid, active email addresses (so they can spam their hearts away).

 

It really depends on the application. But if I register at a website, and that website distributes my email address in their email blast, I will be very upset. Heck, I get upset when my sister sends jokes and such to everyone in her address book (I finally convinced her to use a distribution list system).

 

Depending on your audience -- For in-house stuff, I do use multiple names in the TO header, of course everyone at the office already knows everyone else's address -- For a "Family" site, I might consider it, but heck, there are people in my family that I don't want to have my email, or I intentionally give them a different address from the ones I actually like.

 

If this is for a "real" website, your best bet will be to use a queue system or a real list service. Depending on your host, there is likely to be a limit on the number of emails you can send per hour.

 

Jazzman1 - my response was to the OP's failure to try/fix the one spot I identified as the problem. It was not a shot at your solution. As above, your solution will work and I have used it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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