Hrvoje Posted December 27, 2012 Share Posted December 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/ Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 on the line above the mail() line add $to = $row['emailAddress']; Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401467 Share on other sites More sharing options...
Hrvoje Posted December 27, 2012 Author Share Posted December 27, 2012 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"); Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401471 Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 as a debugging step what happens if you set $to to the address you are testing manualy? Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401473 Share on other sites More sharing options...
Hrvoje Posted December 27, 2012 Author Share Posted December 27, 2012 When I set this: $to = "my-email@adress.com"; Everything is fine I get mail Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401475 Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 ok, between the $to=$row['email'] and the mail() line add die('$to is set to -'.$to.'-'); and lets see what comes back. Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401477 Share on other sites More sharing options...
Hrvoje Posted December 27, 2012 Author Share Posted December 27, 2012 Nothing... Only this is shown on the page when I send mail $to is set to -- Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401479 Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 then the data is getting lost somewhere along the line. post your full pcage of code please? Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401486 Share on other sites More sharing options...
trq Posted December 27, 2012 Share Posted December 27, 2012 You never actually execute any database query. See mysql_query. Or better still, take a look at pdo or mysqli. Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401487 Share on other sites More sharing options...
Hrvoje Posted December 27, 2012 Author Share Posted December 27, 2012 (edited) 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 December 27, 2012 by Hrvoje Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401493 Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401500 Share on other sites More sharing options...
Hrvoje Posted December 27, 2012 Author Share Posted December 27, 2012 (edited) 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 December 27, 2012 by Hrvoje Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401504 Share on other sites More sharing options...
Muddy_Funster Posted December 27, 2012 Share Posted December 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401508 Share on other sites More sharing options...
DavidAM Posted December 27, 2012 Share Posted December 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401530 Share on other sites More sharing options...
Hrvoje Posted December 27, 2012 Author Share Posted December 27, 2012 (edited) 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 December 27, 2012 by Hrvoje Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401536 Share on other sites More sharing options...
DavidAM Posted December 27, 2012 Share Posted December 27, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1401542 Share on other sites More sharing options...
Hrvoje Posted December 30, 2012 Author Share Posted December 30, 2012 (edited) 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 December 30, 2012 by Hrvoje Quote Link to comment https://forums.phpfreaks.com/topic/272403-how-to-send-email-to-adress-from-dba/#findComment-1402217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.