leemo Posted February 27, 2020 Share Posted February 27, 2020 (edited) The code below currently sends a separate Email with data from each row of a table when a Save All button is pressed. So, if I have 4 rows of data, 4 Email will be sent. I would prefer to receive one Email with 4 sections of data. Any help greatly appreciated. $email="to@email.com"; $from="from@email.com"; $msg=""; $subject="Registers Info for: ".$values["first_name"]." ".$values["last_name"].""; $msg.= "Student: ".$values["first_name"]." ".$values["last_name"]."\r\n"; $msg.= "Absent or Present: ".$values["attendance_status"]."\r\n"; $msg.= "Class: ".$values["classname"]."\r\n"; $msg.= "Class Date: ".$values["attendance_date"]."\r\n"; $msg.= "Amount Received: ".$values["cash_received"]."\r\n"; $msg.= "Paid For: ".$values["cash_whatfor"]."\r\n"; $ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from)); if(!$ret["Sent"]) echo $ret["message"]; Edited February 27, 2020 by leemo Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/ Share on other sites More sharing options...
requinix Posted February 27, 2020 Share Posted February 27, 2020 You haven't shown the loop that must be present, but regardless Move the variable initialization to before the loop, keep inside the loop the thing that builds the body of the email message, and move the actual sending of the email to after the loop. Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1574967 Share on other sites More sharing options...
ginerjm Posted February 27, 2020 Share Posted February 27, 2020 Build the message while looping thru your query results. THEN fill out the email and send it. Simple, huh? Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1574969 Share on other sites More sharing options...
leemo Posted March 1, 2020 Author Share Posted March 1, 2020 Thanks for your replies. I'm trying so hard as a noob to work my way around things on my own and it's a great feeling when things work. It took me 2 days to get as far as this 🙂 I've tried the magic curly brackets before and after the actual message section, do I also need to add something similar to this: while ($msg = mysqli_fetch_array($result)) - before the message section and I'm guessing I need something at the end also? Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575008 Share on other sites More sharing options...
ginerjm Posted March 1, 2020 Share Posted March 1, 2020 Don't know what code you are referring to. Would be nice if you posted that block so we all know what you are looking at. Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575014 Share on other sites More sharing options...
leemo Posted March 2, 2020 Author Share Posted March 2, 2020 (edited) { $email="to@email.com"; $from="from@email.com"; $msg=""; $subject="Registers Info for: ".$values["classname"]." ".$values["attendance_date"].""; while ($msg = mysqli_fetch_array($result)) { $msg.= "Student: ".$values["first_name"]." ".$values["last_name"]."\r\n"; $msg.= "Absent or Present: ".$values["attendance_status"]."\r\n"; $msg.= "Class: ".$values["classname"]."\r\n"; $msg.= "Class Date: ".$values["attendance_date"]."\r\n"; $msg.= "Amount Received: ".$values["cash_received"]."\r\n"; $msg.= "Paid For: ".$values["cash_whatfor"]."\r\n"; } $ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from)); } This isn't correct, but I'm doing my best! Edited March 2, 2020 by leemo Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575032 Share on other sites More sharing options...
ginerjm Posted March 2, 2020 Share Posted March 2, 2020 I don't know what "runner_mail" is, but the code itself seems pretty ok. Be sure that your "from" email is using your domain name so that your mail doesn't look like spam Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575036 Share on other sites More sharing options...
leemo Posted March 2, 2020 Author Share Posted March 2, 2020 No Emails were sent using the previous block of code. The code is an After Record Updated event with PHP Runner software Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575040 Share on other sites More sharing options...
ginerjm Posted March 2, 2020 Share Posted March 2, 2020 (edited) Then maybe you need to find a forum that pertains to that product. I never heard of it much less no how to program around it. Edited March 2, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575045 Share on other sites More sharing options...
requinix Posted March 2, 2020 Share Posted March 2, 2020 $subject="Registers Info for: ".$values["classname"]." ".$values["attendance_date"].""; while ($msg = mysqli_fetch_array($result)) { $values isn't available to use until you get to the while loop. You didn't post the part of the code that shows what query is being run so I don't know whether you're doing a query that makes sense for that subject line - that is, whether it's a query for a specific class and attendance date. (Which used to be the student name in your original version...?) So if you want the classname and attendance_date, you should get those values from, presumably, the same place that the query was getting them from. Either that, or you send one email each. You'd be sending multiple emails, but only one for each classname/attendance_date combination. Again, not sure since you haven't posted the code that would help explain what's going on. Hint. Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575047 Share on other sites More sharing options...
Barand Posted March 2, 2020 Share Posted March 2, 2020 (edited) 1. As you are putting multiple names in the message it doesn't make sense to include (one of) them in the subject. 2. Your while loop should be while ($values = mysqli_fetch_array($result)) { otherwise you trash the $msg variable by overwriting it. Edited March 2, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575050 Share on other sites More sharing options...
leemo Posted March 3, 2020 Author Share Posted March 3, 2020 As stated in the first post I am receiving all the information correctly but in separate Emails. My results are between 1-20 records, which of course is 1-20 Emails. Requinix I have changed the subject variable to make more sense if receving one Email. I need to loop the information in the $msg.= sections into one email. I have added: while ($values = mysqli_fetch_array($result)) and put the area I need looped in curly brackets, not sure if that is the correct way to do it. But, as the code stands below I am not receiving any Emails at all. $email="lee@fisteps.com"; $from="db@fisteps.com"; $msg=""; $subject="Registers Info for: ".$values["classname"]." ".$values["attendance_date"].""; while ($values = mysqli_fetch_array($result)) { $msg.= "Student: ".$values["first_name"]." ".$values["last_name"]."\r\n"; $msg.= "Absent or Present: ".$values["attendance_status"]."\r\n"; $msg.= "Class: ".$values["classname"]."\r\n"; $msg.= "Class Date: ".$values["attendance_date"]."\r\n"; $msg.= "Amount Received: ".$values["cash_received"]."\r\n"; $msg.= "Paid For: ".$values["cash_whatfor"]."\r\n"; } $ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from)); SELECT attendance.attendance_id, attendance.student_id, attendance.class_id, attendance.attendance_status, attendance.attendance_date, attendance.cash_received, attendance.cash_whatfor, students.last_name, students.first_name, classes.classname FROM attendance INNER JOIN classes ON attendance.class_id = classes.class_id INNER JOIN students ON attendance.student_id = students.student_id ORDER BY students.last_name Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575080 Share on other sites More sharing options...
Barand Posted March 3, 2020 Share Posted March 3, 2020 Take $values out of the subject Quote $subject="Registers Info" for: ".$values["classname"]." ".$values["attendance_date"]."" ; Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575082 Share on other sites More sharing options...
phppup Posted March 4, 2020 Share Posted March 4, 2020 (edited) It sounds like you've got something working. (which is a plus) and now just need to tweak it to for your specifications. There are many more qualified coders here than me, but here's my opinion. If I understand your dilemma, you are receiving one email per student but would prefer receive a single email with the information of all the students. I would suggest adding an IF statement that surrounds the entirety of your applicable code. You can either ADD another column (maybe MEMBER) and sort through with the IF statement. Or, analyze an existing column that is applicant (maybe attendanc_date). So, if attendence_date is March 1 [2020-03-01] THEN put the six lines of $msg into the body of the email, otherwise, skip the row. You will want to add an additional \n at the end of your $msg to make the email body more presentable and break up the information. Also, in this instance, make sure your$ret is pristine correctly to avoid sending multiple emails with the same content. Good luck. Edited March 4, 2020 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575130 Share on other sites More sharing options...
phppup Posted March 4, 2020 Share Posted March 4, 2020 Last line should read: make sure your $ret is positoned correctly to avoid sending multiple emails with the same content. Quote Link to comment https://forums.phpfreaks.com/topic/310148-send-email-with-new-row-data-one-email-only/#findComment-1575131 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.