Welcome! Log In Create A New Profile

Advanced

Multiple Recipients

Posted by Shaun2010 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/17/2019 02:24PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Multiple Recipients
April 26, 2011 11:56AM
The first person to help me figure out why this doesn't send 1 email to multiple recipients gets a smileys with beer

Language: PHP
if ($_POST['cid'] != '') { $course_info = "SELECT sno, cid FROM course_student WHERE cid=".$_POST['cid']; $course_info_result = mysql_query($course_info) or die ("Could not retrieve info from course_student for mail: " . mysql_error()); while($row_course_student = mysql_fetch_array($course_info_result)){ $sno = $row_course_student['sno']; $cid = $row_course_student['cid'];   $student_info = "SELECT fname, sname, email, contact_flag FROM student WHERE sno = $sno AND contact_flag = 'y'"; $result = mysql_query($student_info) or die ("Could not retrieve student info for mail: " . mysql_error()); $num = mysql_num_rows($result); $i = 0; while($i < $num){ $row = mysql_fetch_array($result); $mail = $row['email'];   $fname = mysql_result($result, $i, 'fname'); $sname = mysql_result($result, $i, 'sname'); $email .= mysql_result($result, $i, 'email').","; $text .= $fname . ' ' . $sname . ' - ' . $mail. "\n"; $i++; } $course_name = "SELECT cid, cname FROM course WHERE cid = $cid"; $course_name_query = mysql_query($course_name) or die ("Could not retrieve course name from database: " . mysql_error()); while($row_course = mysql_fetch_array($course_name_query)){ $cname = $row_course['cname']; } $from = 'From: Info <info@spargweb.co.za>' . "\r\n"; $to = $email; $subject = "Successfully Registered"; $body = "Hi $fname $sname,\n\nHere is a list of all students currently studying $cname\n\n$text"; mail($to, $subject, $body, $from); header("location:student_man.php"); } }

Thanks!

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar
Mac
Re: Multiple Recipients
April 26, 2011 01:46PM
I assume from your code you want to send the contact information of all students to all students at once..

Here below, just for fun, but also to point out errors:

Sending 1 email at a time to all students - ok you would not want to send a student his name and email since they KNOW it but it for for purposes of illustration - you can add another message

Language: PHP
if ($_POST['cid'] != '') { $course_info = "SELECT sno FROM course_student WHERE cid=".$_POST['cid']; $course_info_result = mysql_query($course_info) or die ("Could not retrieve info from course_student for mail: " . mysql_error()); while($row_course_student = mysql_fetch_array($course_info_result)){ $sno = $row_course_student['sno'];   //why do you want to select cid again - you already have it! //do some error checking which you can remove later echo "Sno $sno taking cid $cid =";   $student_info = "SELECT fname, sname, email, contact_flag FROM student WHERE (sno = '$sno' AND contact_flag = 'y')"; $result = mysql_query($student_info) or die ("Could not retrieve student info for mail: " . mysql_error()); $num = mysql_num_rows($result); $i = 0; while($i < $num){ // remove these lines //line $row = mysql_fetch_array($result) //$email = $row['email']; //probably you copied it from elsewhere and forgot it here   $fname = mysql_result($result, $i, 'fname'); $sname = mysql_result($result, $i, 'sname'); // and call it email instead of mail - you are already using the word mail as in the mail function - and get rid of the .= $email = mysql_result($result, $i, 'email').","; $i++; }   // do some error checking again echo "has a fname $fname, a sname $sname and an email $email<br>";   $course_name = "SELECT cid, cname FROM course WHERE cid = '$cid'"; $course_name_query = mysql_query($course_name) or die ("Could not retrieve course name from database: " . mysql_error()); while($row_course = mysql_fetch_array($course_name_query)){ $cname = $row_course['cname']; } $from = 'From: Info <info@spargweb.co.za>' . "\r\n"; $to = $email; $subject = "Successfully Registered"; $body = "Hi $fname $sname,\n\nHere is a list of all students currently studying $cname\n\n$text";   echo "sent to $to<br>";   // stop the mail function here for error checking - it just adds errors you know exists and cluters the screen   //mail($to, $subject, $body, $from);   //you cannot add a header here. //header("location:student_man.php"); }   }

Now the above to all recipients at once. I have taken the post function out here for testing purposes, but assume the cid is 8.
Language: PHP
$course_name = "SELECT * FROM course WHERE cid = '8'"; $course_name_query = mysql_query($course_name) or die ("Could not retrieve course name from database: " . mysql_error()); while($row_course = mysql_fetch_array($course_name_query)){ $cname = $row_course['cname']; }   $find_sno = "SELECT sno FROM course_student WHERE cid='8'"; $result = mysql_query($find_sno) or die ("Could not retrieve info from course_student for mail: " . mysql_error()); $num = mysql_num_rows($result);   $i = 0; while($i < $num){ $sno = mysql_result($result, $i, 'sno');   echo "Sno $sno<br>";   $student_info = "SELECT * FROM student WHERE sno = '$sno' AND contact_flag = 'y'"; $result2 = mysql_query($student_info) or die ("Could not retrieve student info for mail: " . mysql_error()); $num2 = mysql_num_rows($result2); $i2 = 0; while($i2 < $num2){ $fname = mysql_result($result2, $i2, 'fname'); $sname = mysql_result($result2, $i2, 'sname'); $email = mysql_result($result2, $i2, 'email'); $all_email .= $email.","; $text .= $fname . ' ' . $sname . ' - ' . $email. "\n"; $i2++; }   $i++; }       $from = 'From: Info <info@spargweb.co.za>' . "\r\n"; $to = $all_email; $subject = "Successfully Registered"; $body = "Hi $fname $sname,\n\nHere is a list of all students currently studying $cname\n\n$text"; echo "Sent to $to<br>"; echo "$text";   //mail($to, $subject, $body, $from); //header("location:student_man.php");
avatar
Mac
Re: Multiple Recipients
April 26, 2011 01:48PM
Of course it could be done with joins, but always test procedural as here above, then refine using a join.
avatar Re: Multiple Recipients
April 26, 2011 02:30PM
greek
avatar Re: Multiple Recipients
April 26, 2011 02:43PM
Thanks Mac

Makes perfect sense smiling smiley guess I owe you that beer now tongue sticking out smiley

A word to the wise ain't necessary - it's the stupid ones that need the advice.
Student Number:72793775
avatar
Mac
Re: Multiple Recipients
April 26, 2011 02:50PM
You've already paid for that beer viz-a-viz your course fees smiling smiley
Re: Multiple Recipients
May 10, 2011 02:43PM
I wish I had looked at this before I coded!!
Sorry, only registered users may post in this forum.

Click here to login