parallax Posted July 20, 2006 Share Posted July 20, 2006 Hi,I'm pretty new to php, but today I have been trying to create a very simple newsletter-script. So far I have managed to create the subscription/unsubscription feature, which adds/removes the email to/from a database.I am now stuck on trying to code sending out the newsletter to the recipents. This is what I have got so far:[code]<?php require_once("db.php"); ?><?phpif ($_POST['submit'] == TRUE) { $subject = mysql_real_escape_string($_POST['Subject']); $message = mysql_real_escape_string($_POST['Message']); $query = "SELECT * FROM newsletter"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $recipents = $row['Email']. "; "; } mail('$recipents', '$subject', '$message'); echo "Mail sent!";}else{ ?> <h3>Send Newsletter</h3> <form method="post" action="<?php echo $PHP_SELF ?>"> Subject: <input name="Subject" size="44" maxlength="255"> <br> Message: <textarea name="Message" rows="20" cols="40"></textarea> <br> <input type="submit" name="submit" value="Send Newsletter"> </form> <?}?>[/code]Basically I am trying to create an array of the emails from the database, which I have done (if I print $recipents it shows the list of emails) - and then send out one message to all the recipents. When I click on the submit-button, no mail has been sent, but the line "Mail sent!" is still printed. I am not sure if this could be a simple syntax error or a problem with my array or whatever. But the mail function is working on the server (I'm using it in the sub/unsub. features too).Maybe I am just too tired ::), but can anyone see the problem? Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/ Share on other sites More sharing options...
MaaSTaaR Posted July 20, 2006 Share Posted July 20, 2006 if you want to make an array values the emails you should do something like this :[code=php:0]<?php$emails = array();$query = mysql_query('SELECT * FROM table');while ($r = mysql_fetch_array($query)){ $emails[] = $r;}?>[/code]then you will have $emails array which store all email , now try to print one of these emails :[code=php:0]<?phpecho $emails[0]['Email']?>[/code]so use "for" or "while" loop :) Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61218 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 You also need to seperate the emails with a comma instead of a semi-colon.See: http://us2.php.net/manual/en/function.mail.php Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61219 Share on other sites More sharing options...
parallax Posted July 20, 2006 Author Share Posted July 20, 2006 I see, thanks for the tip. Now I am one step closer to the solution. But how would I go about combining the items of an array, separating them with a ',' and saving them as a variable to use in the mail-statement? Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61222 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 http://us2.php.net/implodeImplode the array. :)Cheers Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61224 Share on other sites More sharing options...
MaaSTaaR Posted July 20, 2006 Share Posted July 20, 2006 if you want the emails store in the variable like 'em@em1.com,em@em2.com' try this :[code]$query = mysql_query('SELECT * FROM table');$mails = '';while ($r = mysql_fetch_array($query)){ $mails .= $r['Email'] . ',';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61225 Share on other sites More sharing options...
parallax Posted July 20, 2006 Author Share Posted July 20, 2006 Alright, one final question: Doesn't it matter if there is an extra comma at the end? Wouldn't it skip the subject in the mail-statement? ??? Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61232 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 No, an extra comma shouldn't matter. An easy way to avoid that would be to add your email onto the end outside of the loop if you are really afraid of it breaking. I'd suggest you give it a try and see what happens though. Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61234 Share on other sites More sharing options...
parallax Posted July 20, 2006 Author Share Posted July 20, 2006 Still no mails. This is what the code looks like at the moment:[code] $subject = mysql_real_escape_string($_POST['Subject']); $message = mysql_real_escape_string($_POST['Message']); $query = mysql_query('SELECT * FROM newsletter'); $mails = ''; while ($r = mysql_fetch_array($query)) { $mails .= $r['Email'] . ','; } mail('$mails', '$subject', '$message'); echo "Mail was sent:<br>"; //just to check echo "$mails";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61237 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 Did that code echo "$mails" correctly? Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61238 Share on other sites More sharing options...
parallax Posted July 20, 2006 Author Share Posted July 20, 2006 Yep, it does in fact print out both my emails, with a comma between and at the end. Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61239 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 Aha! I have it. Take out the ' ' around your variables. Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61241 Share on other sites More sharing options...
parallax Posted July 20, 2006 Author Share Posted July 20, 2006 Yesss, it works now! Thank you again ;) Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61243 Share on other sites More sharing options...
ChaosXero Posted July 20, 2006 Share Posted July 20, 2006 :) No problem. Because of this, I know understand the mail function! Hooray! Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61249 Share on other sites More sharing options...
parallax Posted July 20, 2006 Author Share Posted July 20, 2006 Do you know why the message of the mail gets outputted like this:line1\r\nline2\r\nline3\r\n\r\nline5\r\nline6 Instead of actual breaks? Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61300 Share on other sites More sharing options...
ryanlwh Posted July 20, 2006 Share Posted July 20, 2006 [code] $subject = mysql_real_escape_string($_POST['Subject']); $message = mysql_real_escape_string($_POST['Message']);[/code]If you're not going to insert the subject and message into the database, you don't need to use mysql_real_escape_string. Quote Link to comment https://forums.phpfreaks.com/topic/15181-mailarray-help/#findComment-61318 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.