Jump to content

Archived

This topic is now archived and is closed to further replies.

hashstar

how to get users email address from mysql database

Recommended Posts

Hi there,

 

I am trying to use some php code to get a users email address from mysql database, can someone please check my code and see where i am going wrong?

 

<?php

$con = mysql_connect("*","*","*");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ogs_mailinglist1", $con);

$result = mysql_query("SELECT * FROM mailinglist");

while($row = mysql_fetch_array($result))

{
  echo $row['email'] . " " ;
  echo "<br />";
  }

// Grab our config settings
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');

// Grab the FreakMailer class
require_once($_SERVER['DOCUMENT_ROOT'].'/include/MailClass.inc');

// instantiate the class
$mailer = new FreakMailer();

// Set the subject
$mailer->Subject = 'This is a test';

// Body
$mailer->Body = 'This is a test of my mail system!';

// Get the user's Email
$sql = mysql_query("SELECT email FROM users WHERE 1");

while($row = mysql_fetch_object($result))



mysql_close($con);

if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}
else
{
    echo 'Mail sent!';
}
$mailer->ClearAddresses();
$mailer->ClearAttachments();
?>

 

 

I think the problem is with this part:

 

// Get the user's Email

$sql = mysql_query("SELECT email FROM users WHERE 1");

 

while($row = mysql_fetch_object($result))

 

 

but i don't know how to correct it  :(

 

 

Thanks,

 

Hash

Share this post


Link to post
Share on other sites

You can remove the WHERE clause, it does nothing.

 

Also, how do you know your not getting any result? You don't actually do anything with it.

Share this post


Link to post
Share on other sites

oh, and ps. Stop posting your database credentials on a public forum unless you want bad stuff to happen to your data.

Share this post


Link to post
Share on other sites

You can remove the WHERE clause, it does nothing.

 

Also, how do you know your not getting any result? You don't actually do anything with it.

 

Oh ok, i don't know if i am getting a result  :shrug:

 

i thought that this part would send the email

 

if(!$mailer->Send())

 

 

but i am not getting an email so assumed that it was a problem with getting the email from the database...

 

 

 

p.s sorry about posting database info, will be more careful in future

Share this post


Link to post
Share on other sites

Why don't you try echo the query to see if you're getting a result?

 

Share this post


Link to post
Share on other sites

Hi NLT,

 

i added:

 

 

echo $row['email']

 

and it prints the email on the screen so i know i am deftly getting the data from the database. But it won't send the email...

Share this post


Link to post
Share on other sites

Are you using this on a server with SMTP?

Share this post


Link to post
Share on other sites

So, somehow you now need to pass the email address into the $mailer object. We would have no idea how to do that within seeing the class.

Share this post


Link to post
Share on other sites

NLT:  yep i am using this on a server with SMTP

 

Thorpe:  Ah ok, this is the code from my MailClass file i am using if it's any help? 

 

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/include/phpmailer.class.php');

class FreakMailer extends PHPMailer
{
    var $priority = 3;
    var $to_name;
    var $to_email;
    var $From = null;
    var $FromName = null;
    var $Sender = null;

    function FreakMailer()
    {
        global $site;

        // Comes from config.php $site array

        if($site['smtp_mode'] == 'enabled')
        {
            $this->Host = $site['smtp_host'];
            $this->Port = $site['smtp_port'];
            if($site['smtp_username'] != '')
            {
                $this->SMTPAuth = true;
                $this->Username = $site['smtp_username'];
                $this->Password = $site['smtp_password'];
            }
            $this->Mailer = "smtp";
        }
        if(!$this->From)
        {
            $this->From = $site['from_email'];
        }
        if(!$this->FromName)
        {
            $this-> FromName = $site['from_name'];
        }
        if(!$this->Sender)
        {
            $this->Sender = $site['from_email'];
        }
        $this->Priority = $this->priority;
    }
}
?>

 

 

 

Share this post


Link to post
Share on other sites

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