...

View Full Version : Sending mail to all email addresses in mysql database



Nealec
01-09-2012, 08:14 PM
I have no idea what im doing wrong but this just isnt working, theres no error messages but im not getting any of the emails. Here is all the code that i had anything to do with editing, i have removed any personal info from it.


<?php
$dbhost = 'localhost';
$dbuser = ' ';
$dbpass = ' ';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

mysql_select_db(" ", $conn);

$result = mysql_query("SELECT ****, ********** FROM **** WHERE name='*****'");

while($row = mysql_fetch_array($result))
{
$message =
"<p><u><b><font size=\"5\" color=\"#800080\">********************</font></b></u></p>
<p><b><font color=\"#800080\" size=\"4\">**************************************************</font></b></p>
<p><b><font color=\"#800080\">*********</font></b> <b><font color=\"#333333\">**********</font></b></p>
<p><b><font color=\"#800080\">********</font></b> <font color=\"#333333\"><b>***********</b></font></p>
<p><font color=\"#800080\"><b>*******</b></font> <font color=\"#333333\"><a href=\"***********************************************</a></font></p>
<p>&nbsp;</p>";
}

$query=mysql_query('SELECT `email`,`name` FROM `users`'); //grab emails and names from database
while($row = mysql_fetch_array($query)) //start a loop to send an email to each individual
{
//mail function with $row['email'] as the email address
//I'm using phpmailer as an example here - - >
include_once('phpMailer/class.phpmailer.php');

$mail = new PHPMailer(); // defaults to using php "mail()"

$body = $message; //message inside the email

$mail->From = "**************"; //email address that the email is being sent from
$mail->FromName = "*************"; //more in depth for who the mail is from.

$mail->Subject = "***********************************"; //The subject for the message

//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test


$mail->AddAddress($row[email], $row['name']);
}

mysql_close($conn);
?>

BluePanther
01-09-2012, 08:27 PM
I have no idea what im doing wrong but this just isnt working, theres no error messages but im not getting any of the emails. Here is all the code that i had anything to do with editing, i have removed any personal info from it.


<?php
$dbhost = 'localhost';
$dbuser = ' ';
$dbpass = ' ';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

mysql_select_db(" ", $conn);

$result = mysql_query("SELECT ****, ********** FROM **** WHERE name='*****'");

while($row = mysql_fetch_array($result))
{
$message =
"<p><u><b><font size=\"5\" color=\"#800080\">********************</font></b></u></p>
<p><b><font color=\"#800080\" size=\"4\">**************************************************</font></b></p>
<p><b><font color=\"#800080\">*********</font></b> <b><font color=\"#333333\">**********</font></b></p>
<p><b><font color=\"#800080\">********</font></b> <font color=\"#333333\"><b>***********</b></font></p>
<p><font color=\"#800080\"><b>*******</b></font> <font color=\"#333333\"><a href=\"***********************************************</a></font></p>
<p>&nbsp;</p>";
}

$query=mysql_query('SELECT `email`,`name` FROM `users`'); //grab emails and names from database
while($row = mysql_fetch_array($query)) //start a loop to send an email to each individual
{
//mail function with $row['email'] as the email address
//I'm using phpmailer as an example here - - >
include_once('phpMailer/class.phpmailer.php');

$mail = new PHPMailer(); // defaults to using php "mail()"

$body = $message; //message inside the email

$mail->From = "**************"; //email address that the email is being sent from
$mail->FromName = "*************"; //more in depth for who the mail is from.

$mail->Subject = "***********************************"; //The subject for the message

//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test


$mail->AddAddress($row[email], $row['name']);
}

mysql_close($conn);
?>

Never used that class before, but I don't think you should be redefining the subject and from - as well as generating a new instance of the class. Move all that out the loop.


//mail function with $row['email'] as the email address
//I'm using phpmailer as an example here - - >
include_once('phpMailer/class.phpmailer.php');

$mail = new PHPMailer(); // defaults to using php "mail()"

$body = $message; //message inside the email

$mail->From = "**************"; //email address that the email is being sent from
$mail->FromName = "*************"; //more in depth for who the mail is from.

$mail->Subject = "***********************************"; //The subject for the message

//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test


$query=mysql_query('SELECT `email`,`name` FROM `users`'); //grab emails and names from database
while($row = mysql_fetch_array($query)) //start a loop to send an email to each individual
{
$mail->AddAddress($row[email], $row['name']);
}

Also, it's hard to tell from your censoring but do you really need two queries? Do they fetch entirely different things from different tables?

Inigoesdr
01-09-2012, 08:47 PM
You are doing everything except assigning the body content or sending. You need to call the Send() method to initiate the send at the end of your while() (http://php.net/while) loop:

$mail->Send();
See the PHPMailer tutorial (http://phpmailer.worxware.com/index.php?pg=tutorial#2) in their docs.

Nealec
01-09-2012, 09:01 PM
Ok sorry to be a pain but where exactly do i put the $mail->Send(); ? (I am what you might call a noob.)

Nealec
01-09-2012, 09:30 PM
You are doing everything except assigning the body content or sending. You need to call the Send() method to initiate the send at the end of your while() (http://php.net/while) loop:

$mail->Send();
See the PHPMailer tutorial (http://phpmailer.worxware.com/index.php?pg=tutorial#2) in their docs.

Ok i found where to put the $mail->Send(); in the tutorial you provided now im getting an error: Message was not sent.Mailer error: Message body empty.

What am i doing wrong? i get the feeling its something to do with where the $message variable is but nothing ive tried so far has worked.

tangoforce
01-09-2012, 09:30 PM
Am I the only one who's noticed the missing quotes here?:

$mail->AddAddress($row[email], $row['name']);

Should be:
$mail->AddAddress($row['email'], $row['name']);

Otherwise php will scream somewhere about undelcared constants and how its wonderfully assuming it to be one etc. Might not show on screen but its enough to put the spanner in.

Inigoesdr
01-09-2012, 09:33 PM
What am i doing wrong? i get the feeling its something to do with where the $message variable is but nothing ive tried so far has worked.

Go back and look at the tutorial again. Specifically look at the example where they assign a string to $mail->Body.

Nealec
01-09-2012, 09:34 PM
Thanks i have put them back in, dnt know where they went but it hasn't fixed anything but thanks for pointing that out anyway.

Nealec
01-09-2012, 09:53 PM
Go back and look at the tutorial again. Specifically look at the example where they assign a string to $mail->Body.

I have gone back and had a look and in the end i copied and pasted the

$mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";

from the tutorial and put it in the exact same place and im still getting the error. Message was not sent.Mailer error: Message body empty

Im 100% sure that what your saying must be right but im just not getting it.

Inigoesdr
01-10-2012, 03:24 AM
Post your code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum