0

I have made a mass send mail function. I have inserted 3 mails in the database but when I'm using mass mail function, mail sends more then 1 time to every email. I have tried to fix it but I couldn't. I guess the problem is in the ending while loop.

Here is the code:

<html>
<form action="send_mass_mail.php" method="post">
<label>Subject of email:</label><br><input type="text" name="subject" id="subject"/><br>
<label>Body of email:</label><br><input type="textarea" name="body"></label><br>
<input type="submit" name="submit" value="Submit"/>
</form>
</html>

<?php

$user = ""; 
$password = ""; 
$host = ""; 
$dbase = ""; 
$table = "Mail"; 


$from= 'tjaabba.com@news.se';//specify here the address that you want email to be sent from

$subject= $_POST['subject'];
$body= $_POST['body'];

// Connection to DBase 
mysql_connect($host,$user,$password) 
or die("Unable to select database");
mysql_select_db('tjaabba_com');

$query= "SELECT * FROM $table";
$result= mysql_query ($query) 
or die ('Error querying database.');

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

$msg= "Dear mail_form,\n$body";
if(isset($_POST['subject'])){
if(isset($_POST['submit'])){
mail($email, $subject, $body, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
}
}
}

?>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Done any basic debugging, like checking if you've got multiple copies of each email address in your DB? – Marc B Jun 06 '14 at 14:12

2 Answers2

0

The mail function is very unreliable. The best option would be outsource this to a company like mailchimp or madrill that specializes in this. You can find out more in this link

Community
  • 1
  • 1
0

My guess is that it is in the while loop, but the best thing to do is try to debug the problem. You can do this using print_r() and die() functions.

My suggestion would be to place this before the while loop to find out if you have duplicated data or it's some how coming out more than once.

print_r(mysql_fetch_array($result));
die();

Then copy and paste what ever comes up on here, but replace the emails with dummy emails as we can then still get some sort of idea as to what is going on. Or maybe when you do this, you will get an idea as to what is going on.

CheckeredMichael
  • 571
  • 8
  • 30
  • Email sent to: oliverweitman123@hotmail.com Array ( [0] => oliver@tjaabba.com [email] => oliver@tjaabba.com )I think the problem with while loop is that it repeats. Do you thing this will work if i use another loop – Oliver Weitman Jun 06 '14 at 16:53
  • While loops aren't meant to repeat. Are you sure you haven't got duplicated data somewhere? They are supposed to iterate through the data and then keep hitting the next step until there is no more data. – CheckeredMichael Jun 09 '14 at 12:07