Jump to content

Recommended Posts

How can I send email to one adress from my database...

 

Selecting table with that email...

include "connect.php";
$sql="SELECT * FROM emailadress";
while ($row=mysql_fetch_array($sql))
{
}

 

Here I need to echo somehow that mail adress in this $to variable

 

mail($to, $subject, $msg, "Od: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

 

Please help

Thanks

Link to comment
https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/
Share on other sites

Thanks but nothing happens.

Email is sent successfully but have not received nothing.

Email adress in dba is correct.

 

Table name is emailadress, field is email

 

include "connect.php";
$sql="SELECT * FROM emailadress";
while ($row=mysql_fetch_array($sql))
{
}

$to = $row['email'];
mail($to, $subject, $msg, "Od: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

Ok, thanks.

 

So this is the code:

 

<?PHP
 $error = ''; // error message
 $name	 = ''; // sender's name
 $email = ''; // sender's email address
 $subject = ''; // subject
 $message = ''; // the message itself
 $spamcheck = ''; // Spam check
 if(isset($_POST['send']))
 {
 $name	 = $_POST['name'];
 $email = $_POST['email'];
 $subject = $_POST['subject'];
 $message = $_POST['message'];
 $spamcheck = $_POST['spamcheck'];
 if(trim($name) == '')
 {
 $error = '<div class="errormsg">Molimo unesite Vaše Ime!</div>';
 }
 else if(trim($email) == '')
 {
 $error = '<div class="errormsg">Molimo unesite Vašu email adresu!</div>';
 }
 else if(!isEmail($email))
 {
 $error = '<div class="errormsg">Unjeli ste krivu Email adresu!</div>';
 }
 if(trim($subject) == '')
 {
 $error = '<div class="errormsg">Molimo unesite naslov poruke!</div>';
 }
 else if(trim($message) == '')
 {
 $error = '<div class="errormsg">Molimo unesite poruku!</div>';
 }
 else if(trim($spamcheck) == '')
 {
 $error = '<div class="errormsg">Molimo unesite broj provjere!</div>';
 }
 else if(trim($spamcheck) != '5')
 {
 $error = '<div class="errormsg">Provjera: Broj je netoèan! 2 + 3 = ???</div>';
 }
 if($error == '')
 {
 if(get_magic_quotes_gpc())
 {
 $message = stripslashes($message);
 }

 $subject = 'Contact email : ' . $subject;
 $msg	 = "Od : $name \r\nE-mail : $email \r\nSubject : $subject \r\n\n" . "Poruka : \r\n$message";


 include "connect.php";
 $sql="SELECT * FROM emailadress";
 while ($row=mysql_fetch_array($sql))
 {
 }

 $to = $row['email'];
 die('$to is set to -'.$to.'-');
 mail($to, $subject, $msg, "Od: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

 ?>

<p>Thanks <b><?PHP echo $name;?></b></p>
<p>Success</p>

<?PHP
		 }
	 }
	 if(!isset($_POST['send']) || $error != '')
	 {
?>

<?PHP echo $error;?>
<form method="post" name="contFrm" id="contFrm" action="">
<label><span class="required">*</span> Ime i Prezime:</label>
<input name="name" type="text" class="box" id="name" size="40" value="<?PHP echo $name;?>" />
<label><span class="required">*</span> E-mail: </label>
<input name="email" type="text" class="box" id="email" size="40" value="<?PHP echo $email;?>" />
<label><span class="required">*</span> Naslov: </label>
<input name="subject" type="text" class="box" id="subject" size="40" value="<?PHP echo $subject;?>" />
<label><span class="required">*</span> Poruka: </label>
<textarea name="message" cols="37" rows="10" id="message"><?PHP echo $message;?></textarea>
<label><span class="required">*</span> Provjera: <b>2 + 3=</b></label>
<input name="spamcheck" type="text" class="box" id="spamcheck" size="4" value="<?PHP echo $spamcheck;?>" />
<input name="send" type="submit" class="button" id="send" value="" />
</form>
<?PHP
	 }
	 function isEmail($email)
	 {
		 return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
				 ,$email));
	 }
	 ?>

Edited by Hrvoje

How are you expecting this script to work? is it supposed to send a mail for every entry in the database or for a single, specific entry?

 

What's happening just now is that the value in $row['email'] is that of the last record returned from the table, which in this case must have no email address.

This would be the original:

 

<?PHP
        $error = ''; // error message
        $name   = ''; // sender's name
        $email = ''; // sender's email address
        $subject = ''; // subject
        $message = ''; // the message itself
        $spamcheck = ''; // Spam check
        if(isset($_POST['send']))
        {
        $name   = $_POST['name'];
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];
        $spamcheck = $_POST['spamcheck'];
        if(trim($name) == '')
        {
        $error = '<div class="errormsg">Molimo unesite Vaše Ime!</div>';
        }
        else if(trim($email) == '')
        {
        $error = '<div class="errormsg">Molimo unesite Vašu email adresu!</div>';
        }
        else if(!isEmail($email))
        {
        $error = '<div class="errormsg">Unjeli ste krivu Email adresu!</div>';
        }
        if(trim($subject) == '')
        {
        $error = '<div class="errormsg">Molimo unesite naslov poruke!</div>';
        }
        else if(trim($message) == '')
        {
        $error = '<div class="errormsg">Molimo unesite poruku!</div>';
        }
        else if(trim($spamcheck) == '')
        {
        $error = '<div class="errormsg">Molimo unesite broj provjere!</div>';
        }
        else if(trim($spamcheck) != '5')
        {
        $error = '<div class="errormsg">Provjera: Broj je netoèan! 2 + 3 = ???</div>';
        }
        if($error == '')
        {
        if(get_magic_quotes_gpc())
        {
        $message = stripslashes($message);
        }

        $to = "mail-test@mail-test.com";

        $subject = 'Contact email : ' . $subject;
        $msg    = "Od : $name \r\nE-mail : $email \r\nSubject : $subject \r\n\n" . "Poruka : \r\n$message";

        mail($to, $subject, $msg, "Od: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

        ?>

<p>Thanks <b><?PHP echo $name;?></b></p>
<p>Success</p>

<?PHP
                        }
                }
                if(!isset($_POST['send']) || $error != '')
                {
?>

<?PHP echo $error;?>
<form method="post" name="contFrm" id="contFrm" action="">
<label><span class="required">*</span> Ime i Prezime:</label>
<input name="name" type="text" class="box" id="name" size="40" value="<?PHP echo $name;?>" />
<label><span class="required">*</span> E-mail: </label>
<input name="email" type="text" class="box" id="email" size="40" value="<?PHP echo $email;?>" />
<label><span class="required">*</span> Naslov: </label>
<input name="subject" type="text" class="box" id="subject" size="40" value="<?PHP echo $subject;?>" />
<label><span class="required">*</span> Poruka: </label>
<textarea name="message" cols="37" rows="10" id="message"><?PHP echo $message;?></textarea>
<label><span class="required">*</span> Provjera: <b>2 + 3=</b></label>
<input name="spamcheck" type="text" class="box" id="spamcheck" size="4" value="<?PHP echo $spamcheck;?>" />
<input name="send" type="submit" class="button" id="send" value="" />
</form>
<?PHP
                }
                function isEmail($email)
                {
                        return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
                                        ,$email));
                }
                ?>

 

Now I want to send mail with that, but how to select email adress from database like this method:

include "connect.php";
$sql="SELECT * FROM emailadress";
while ($row=mysql_fetch_array($sql))
{
}

 

I have only one record in that table (one email adress)

Edited by Hrvoje

as trq pointed out - you need to actually execute the query using

mysql_query($sql) or die(mysql_error());

before you can access the results in the while loop. That you don't have any errors telling you this is a bit concerning. You should alwaus have error reporting turned on for development - if this is a development enviroment please find and edit your php.ini file to include E_ALL and E_STRICT for error reporting.

What's happening just now is that the value in $row['email'] is that of the last record returned from the table, which in this case must have no email address.

 

Actually with that code:

        $sql="SELECT * FROM emailadress";
        while ($row=mysql_fetch_array($sql))
        {
        }

        $to = $row['email'];
        die('$to is set to -'.$to.'-');

 

$row will be false after the loop, so the assignment $to = $row['email'] will be throwing an error since the index ("email") does not exist (because $row is not an array).

 

As trq stated, the query has not been executed. And as Muddy_Funster pointed out, It is not clear what you are trying to do. You are selecting ALL ROWS in the table, but there is NOTHING inside the WHILE loop to collect any data. If you are trying to send email to a single user, you need to add a condition to the query (WHERE UserID = 4 -- or whatever) and then you do not need the WHILE loop.

I want to send email trough this script, I need to select only one mail adress from table, and than send it to that mail adres.

It will be only one email address in that table.

Value of the field email will change ex.. every two days but always with ID 1

 

$sql="SELECT * FROM emailtable WHERE id =1";

Edited by Hrvoje

After building the correct SQL query, you need to execute the query. See the manual mysql_query.

 

If the query succeeds, you can then fetch the data (see mysql_fetch_array or mysql_fetch_assoc).

 

The fetch will return an array of columns selected. You can then assign the value from the email column to the variable -- $to = $row['email'] or you can use the array directly in the mail() function -- mail($row['email'], ....

 

Note: You should not use SELECT * unless you actually need EVERY column in the table. Just select the columns you are going to use. All of the data is returned to the script, so you are wasting resources (time and memory) if you select more than you need.

 

Also, you need to review your headers. I have never seen Od : in the spec. Is that supposed to be To:, or what?

Thanks to all...

I solved my problem:

 

include "connect.php";
$sql="SELECT email FROM emailuser WHERE id=1";
$result = mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
...
}

 

$to = $row['email'];

 

mail($to, ....);

 

Best regards

Hrvoje

Edited by Hrvoje
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.