Freedom-n-Democrazy Posted September 23, 2011 Share Posted September 23, 2011 I'm setting up a newsletter thing for my website. I have a newsletter table in MySQL: +----------------+------+--------+ | email | mens | womens | +----------------+------+--------+ | [email protected] | 1 | 1 | | [email protected] | 1 | 0 | +----------------+------+--------+ I am using a HTML form and this PHP code I learn't from the manual, which sends out e-mail's. PHP: if ($_POST['newsletter'] == 'Mens') { $to = ''; $subject = $_POST['subject']; $body = $_POST['body']; $header = 'From: Me Someone <[email protected]>'; mail($to, $subject, $body, $header); } What I want to do with the above code is send out an e-mail to all the e-mails in my MySQL database that are tagged '1' under mens. How would I go about doing this? I'm guessing I will have to use a MySQL query in the $to = ''; that goes something like this: $to = '$query (select from `newsletters` where `email` = 1'); ? Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/ Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 Hold up, trying: select email from newsletters where mens = 1; Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272011 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 Damn! Whats wrong with this? $to = ($query (select email from newsletters where mens = 1)); EDIT: Also tried this but fail: $query = "select email from newsletters where mens = 1"; mysql_query ($query); $to = $query; EDIT: Also set, but fail: mysql_query ($to); Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272015 Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 You have to actually execute the query. $sql = "SELECT `email` FROM `newsletters` WHERE `mens` = 1"; $sql_query = mysql_query($sql); Then its just a matter of iterating over the results and sending out the emails.. Or join the results together and send 1 email to all recipients. Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272023 Share on other sites More sharing options...
TOA Posted September 23, 2011 Share Posted September 23, 2011 Your main problem was you weren't calling the mysql_query function, and you needed quotes around your sql string. You also would have gotten a resource id error because you needed to deal with the result accordingly. Try this $sql = 'select email from newsletters where mens = 1'; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)) { $to[] = $row['email']; } $tostring = implode(';', $to); // had a comma in here should be semi-colon to separate email addresses *Edit* You have to actually execute the query. $sql = "SELECT `email` FROM `newsletters` WHERE `mens` = 1"; $sql_query = mysql_query($sql); Then its just a matter of iterating over the results and sending out the emails.. Or join the results together and send 1 email to all recipients. Beat me to it. My example joins them together Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272027 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 Cool guys, thanks. I'm not using the following code, but its still not working. <?php $link = mysql_connect('localhost', 'testusr', 'testpw'); mysql_select_db('testdb', $link); if ($_POST['newsletter'] == 'Mens') { $query = "select email from newsletters where mens = 1"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $to[] = $row['email']; } $tostring = implode(';', $to); $subject = $_POST['subject']; $body = $_POST['body']; $header = 'From: Me Someone <[email protected]>'; mail($to, $subject, $body, $header); } echo 'Done.'; mysql_query ($query); mysql_close($link); ?> Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272035 Share on other sites More sharing options...
TOA Posted September 23, 2011 Share Posted September 23, 2011 Can you be more specific? I made a typo when I posted it, the comma in the implode should be a semi-colon. But we need more details Edit: also, use $tostring instead of $to in the mail function Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272037 Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 You are or you arent using that code? Im a little confused.. If you ARE using that code.. you are giving the "to" parameter of the mail function an array.. You either need to use the $tostring as defined earlier or change this line mail($to, $subject, $body, $header); to mail(join(',',$to), $subject, $body, $header); DevilsAdvocate: you were right the first time.. Its a comma to separate emails in the mail() function Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272039 Share on other sites More sharing options...
TOA Posted September 23, 2011 Share Posted September 23, 2011 DevilsAdvocate: you were right the first time.. Its a comma to separate emails in the mail() function Ah, so it is! Should have trusted the gut Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272041 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 Sorry for the slow response, my net is capped at 72kbps until the end of the months, and thats kiloBITS!!! :'( The code worked fine, thanks guys the help was really appreciated! The only problem I have is that when the mail is received, I can see all the e-mail addresses it was sent to. Is it possible to stop this? I'm guessing with some sort of loop or something. To be honest that is beyond me and just an educated guess, but I am all ears on advice how to go about achieving it. Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272049 Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 You will have to create a loop and iterate over the $to array that you created and just let the mail chug. Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272051 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 WOW LOL What does that mean? lol sorry bro I've only been using PHP for a month and I'm not that 1337 yet. haha Could you please give me an example of those terms you used so I have an understanding of their logic? Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272055 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 I'm guess something like: foreach ($email as $tostring); mail($tostring, $subject, $body, $header)}; ? Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272057 Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 Wrap a foreach loop around the mail function. foreach ($to as $email) { mail($email, $subject, $body, $header); } Or you could do it straight out of the database where the $to array is generated.. Either way. // Version 1 // while ($row = mysql_fetch_assoc($result)) { $to[] = $row['email']; } $subject = $_POST['subject']; $body = $_POST['body']; $header = 'From: Me Someone <[email protected]>'; foreach ($to as $mail) { mail($mail, $subject, $body, $header); } // Version 2 // $subject = $_POST['subject']; $body = $_POST['body']; $header = 'From: Me Someone <[email protected]>'; while ($row = mysql_fetch_assoc($result)) { mail($row['email'], $subject, $body, $header); } Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272060 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 I noticed $tostring = implode(';', $to); is missing from version 1 and 2. Is that correct.? Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272063 Share on other sites More sharing options...
TOA Posted September 23, 2011 Share Posted September 23, 2011 I noticed $tostring = implode(';', $to); is missing from version 1 and 2. Is that correct.? Correct. What he gave you will complete the script after your query. Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272067 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 23, 2011 Author Share Posted September 23, 2011 Fantastic! Thanks guys so much for your help, especially you Buddski! Wow PHP is such an amazing product. Every day I continue to get blown away how powerful it is! Coming from a bit of a Python background, I use to fear PHP... but now I see it as the most awesome thing to come along since I found Linux! Thanks again guys! I hope you enjoy your days! Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272073 Share on other sites More sharing options...
TOA Posted September 23, 2011 Share Posted September 23, 2011 Thanks guys so much for your help, especially you Buddski! Shafted! All because he kept beating me to the punch Kidding. Glad it helped Link to comment https://forums.phpfreaks.com/topic/247710-sending-e-mail-from-php-using-an-e-mail-list-from-a-mysql-table/#findComment-1272078 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.