benchew0904 Posted January 18, 2012 Share Posted January 18, 2012 I need some help over here. I want to send an email to my friends by checking the database and extract the id. After extracting the id from the database, i want to include a URL with the id. E.g. www.abc.php?35 (35 is the id) www.abc.php?40 (40 is the id) May I know how to send an email with multiple url? Is it using a for loop in the php email function? Your help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/ Share on other sites More sharing options...
joel24 Posted January 18, 2012 Share Posted January 18, 2012 To send one ID to the corresponding email; use a while loop to cycle through the ids from the database and then use the mail() function to send an email to each. Or if you want to send all the ids to one email; use a while loop to cycle through the ids and add it to a string which will be emailed using mail(); Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1308779 Share on other sites More sharing options...
benchew0904 Posted January 18, 2012 Author Share Posted January 18, 2012 how should i go about doing it if i want to send all the ids to one email? Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1308823 Share on other sites More sharing options...
cyberRobot Posted January 18, 2012 Share Posted January 18, 2012 So basically, you want to loop through the content extracted from the database and display a link for each entry. Then you want the user to click each link which would send an e-mail to whoever's entry was clicked. Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1308866 Share on other sites More sharing options...
benchew0904 Posted January 18, 2012 Author Share Posted January 18, 2012 .. So basically, you want to loop through the content extracted from the database and display a link for each entry. Then you want the user to click each link which would send an e-mail to whoever's entry was clicked. Is that correct? No..i'm trying to extract my friend appointment_id and send an email to them if they have any appointment date on the following week. And the email content will be the link to their appointment date to let them confirm their appointment date. E.g. of the email content www.abc.php?40 (40 will be my friend appointment_id) www.abc.php?42 (42 will be my friend appointment_id) Hope you get what i mean Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1308899 Share on other sites More sharing options...
joel24 Posted January 18, 2012 Share Posted January 18, 2012 what are the fields in the db table? where are the emails stored...? just to make sure I'm understanding your intentions; you want to extract the appointments from the database and send an email to each user with their appointments? Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309060 Share on other sites More sharing options...
benchew0904 Posted January 18, 2012 Author Share Posted January 18, 2012 Email are stored in session after customer log in.. They will click on the button to send.. This is the code after clicking on the button <?php include 'config.php'; //my configuration, username, password etc $sql = "SELECT * from customer c WHERE c.uid = " . $_SESSION['uid'] . "and c.appointmentDate BETWEEN CURRENT_DATE AND (CURRENT_DATE + INTERVAL 7 DAY )"; $sql1= exeSelectQuery($sql); if (isset($_POST['submit'])) { //after clicking on the button if ($sql1) { $to = $_SESSION['mail']; $subject = "Appointment Reminder"; $body = "Dear Customer \n\n" . "Please confirm your following appointment date by clicking on the link . \n" . "http://abc.php?40 \n" . #40 is the appointment_id "http://abc.php?42 \n" . #42 is the appointment_id "Regard\n" . "Admin"; $header = 'From: myemail@abc.com' ; $deliver = mail($to, $subject, $body, $header); if ($deliver) { echo "Appointment Reminder Sent. \n"; } else { echo "Appointment Reminder Sending Fail. \n"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309073 Share on other sites More sharing options...
joel24 Posted January 19, 2012 Share Posted January 19, 2012 <?php include 'config.php'; //my configuration, username, password etc $sql = "SELECT * from customer c WHERE c.uid = " . $_SESSION['uid'] . "and c.appointmentDate BETWEEN CURRENT_DATE AND (CURRENT_DATE + INTERVAL 7 DAY )"; $sql1= exeSelectQuery($sql); if (isset($_POST['submit'])) { //after clicking on the button if ($sql1) { $to = $_SESSION['mail']; $subject = "Appointment Reminder"; $body = "Dear Customer \n\n" . "Please confirm your following appointment date by clicking on the link . \n"; while ($row = mysql_fetch_assoc($sql)) { $body .= "http://abc.php?{$row['appointment_id']} \n"; } $body .= "Regard\n" . "Admin"; $header = 'From: myemail@abc.com' ; $deliver = mail($to, $subject, $body, $header); if ($deliver) { echo "Appointment Reminder Sent. \n"; } else { echo "Appointment Reminder Sending Fail. \n"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309088 Share on other sites More sharing options...
benchew0904 Posted January 19, 2012 Author Share Posted January 19, 2012 I tried adding in the while loop but it didnt print out the link.. Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309095 Share on other sites More sharing options...
joel24 Posted January 19, 2012 Share Posted January 19, 2012 You need to put in a html anchor tag to link it... while ($row = mysql_fetch_assoc($sql)) { $body .= "<a href='http://abc.php?{$row['appointment_id']}'>http://abc.php?{$row['appointment_id']}</a> \n"; } #edit you should set the content type to HTML if you're having links in there read this //FROM php.net // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309099 Share on other sites More sharing options...
benchew0904 Posted January 19, 2012 Author Share Posted January 19, 2012 I have edited but it also didn't show the link Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309121 Share on other sites More sharing options...
joel24 Posted January 19, 2012 Share Posted January 19, 2012 what does it display? post your code Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309172 Share on other sites More sharing options...
benchew0904 Posted January 19, 2012 Author Share Posted January 19, 2012 This is my code <?php include 'config.php'; $sql = "SELECT * from customer c WHERE c.uid = " . $_SESSION['uid'] . "and c.appointmentDate BETWEEN CURRENT_DATE AND (CURRENT_DATE + INTERVAL 7 DAY )"; $sql1 = exeSelectQuery($sql); if (isset($_POST['submit'])) { if ($sql1) { $to = $_SESSION['mail']; $subject = "Appointment Reminder"; $body = "Dear Customer <br/><br/>" . "Please confirm your following appointment date by clicking on the link. <br/>"; while ($row = mysqli_fetch_assoc($sql)) { $body .= "<a href='http://abc.php?{$row['appointment_id']}'> http://abc.php?{$row['appointment_id']}</a> <br/>"; } $body .= "Regard<br/>" . "Admin"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: myemail@abc.com' . "\r\n"; $deliver = mail($to, $subject, $body, $headers); if ($deliver) { echo "Appointment Reminder Sent. \n"; } else { echo "Appointment Reminder Sending Fail. \n"; } } } ?> This is the output which I have received. Dear Customer Please confirm your following appointment date by clicking on the link. Yours Sincerely Admin Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309200 Share on other sites More sharing options...
Muddy_Funster Posted January 19, 2012 Share Posted January 19, 2012 change this while ($row = mysqli_fetch_assoc($sql)) { to while ($row = mysqli_fetch_assoc($sql1)) { you are using the wrong variable in your fetch, I suggest using more relative variable names to avoid this issue in the future. Also, checking mysql_num_rows($sql1) will allow you to check for no appointments and handle that accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309205 Share on other sites More sharing options...
benchew0904 Posted January 19, 2012 Author Share Posted January 19, 2012 oh ok.. by the way what you meant by checking mysql_num_rows($sql1)? Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309209 Share on other sites More sharing options...
Muddy_Funster Posted January 19, 2012 Share Posted January 19, 2012 if (mysql_num_rows($sql1) == 0){ echo "you have no appointments in the next 7 days. Thank you for checking with us."; } else{ //produce email } Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309212 Share on other sites More sharing options...
benchew0904 Posted January 20, 2012 Author Share Posted January 20, 2012 Sorry, i'm quite weak in php..May I know which line i should add in the if (mysql_num_rows($sql1) == 0){ Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309443 Share on other sites More sharing options...
cyberRobot Posted January 20, 2012 Share Posted January 20, 2012 Sorry, i'm quite weak in php..May I know which line i should add in the if (mysql_num_rows($sql1) == 0){ That's where you put whatever should happen if no results were found. Quote Link to comment https://forums.phpfreaks.com/topic/255260-sending-email/#findComment-1309527 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.