Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Sending mail to all email addresses in mysql database

    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 Code:
    <?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);
    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Nealec View Post
    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 Code:
    <?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.
    PHP Code:
    //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?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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() loop:
    PHP Code:
    $mail->Send(); 
    See the PHPMailer tutorial in their docs.

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Ok sorry to be a pain but where exactly do i put the $mail->Send(); ? (I am what you might call a noob.)

  • #5
    New Coder
    Join Date
    Feb 2011
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    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() loop:
    PHP Code:
    $mail->Send(); 
    See the PHPMailer tutorial 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.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Nealec View Post
    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.

  • #8
    New Coder
    Join Date
    Feb 2011
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #9
    New Coder
    Join Date
    Feb 2011
    Posts
    95
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    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.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Post your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •