techker Posted October 29, 2017 Share Posted October 29, 2017 (edited) Hey guys..spent a few hours on this and going nuts..lol can somebody help? explanation: I need to loop in database and check to see if any clients are expired if they are expired or going to be expired soon to send a notification by email Problem 1 : can only use SMTP since they disabled Mail() on my server... Problem 2 don't know how to send to multiple people.. So i got the connection (Mysqli) got the query part and also the array in my for loop: $remind_query1 = "SELECT * From Client_Data"; //Sql query to find users that reminders dates match current date. if($run1 = $con->query($remind_query1)) { $rows = $run1->num_rows; for ($j = 0; $j < $rows; ++$j)//loop through each user in results and send a reminder email. { $run1->data_seek($j); $row = $run1->fetch_array(MYSQLI_NUM); $to = $row[4]; $Date = $row[2]; $Name = $row[1]; echo($to); echo($Name); echo "<br>"; //echo "<meta http-equiv=Refresh content=1;url=send3.php?ClientName=$Name&ClientEmail=$to>"; } } So i have a send.php with mail function for smtp i was thinking of passing the array to that page and the it could send the emails that is were im stuck.. Edited October 29, 2017 by techker Quote Link to comment Share on other sites More sharing options...
techker Posted October 29, 2017 Author Share Posted October 29, 2017 ok i got the the triage part done!lol if($today >= $expire || $today < $startdate ){ echo $to . " " . $Name. " " .$today; //echo "expired"; //echo "<meta http-equiv=Refresh content=1;url=send1.php?ClientName=$Client&ClientEmail=$ClientE&Expire=$DatabaseDate>"; } From there i need to send it to a page that will email all... Quote Link to comment Share on other sites More sharing options...
requinix Posted October 29, 2017 Share Posted October 29, 2017 Use whatever you want to send using an SMTP server - mail() via SMTP servers instead of a local sendmail may still work. If you have that working to send one email then all you have to do is put that code into the loop so it sends once for each person. Quote Link to comment Share on other sites More sharing options...
techker Posted October 29, 2017 Author Share Posted October 29, 2017 im lost with smtp..mail function failed.. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 29, 2017 Share Posted October 29, 2017 Actually I think the SMTP thing is for Windows only... Use something like PHPMailer. Quote Link to comment Share on other sites More sharing options...
kicken Posted October 29, 2017 Share Posted October 29, 2017 So i have a send.php with mail function for smtp i was thinking of passing the array to that page and the it could send the emails that is were im stuck.. Trying to split this across two different pages is silly, just put the code to send an email in your loop. Create a function for sending the email to keep things tidy. Also as mentioned, use a library like PHPMailer or SwiftMailer to send the email. For example: $transport = new Swift_SmtpTransport('host', 25); $transport->setUsername('username'); $transport->setPassword('password'); $mailer = new Swift_Mailer($transport); $remind_query1 = "SELECT * From Client_Data"; //Sql query to find users that reminders dates match current date. if($run1 = $con->query($remind_query1)) { $rows = $run1->num_rows; for ($j = 0; $j < $rows; ++$j)//loop through each user in results and send a reminder email. { $run1->data_seek($j); $row = $run1->fetch_array(MYSQLI_NUM); $to = $row[4]; $Date = $row[2]; $Name = $row[1]; sendMessage($mailer, $to, $Name, $Date); } } function sendMessage($mailer, $to, $name, $reminderDate){ $message = new Swift_Message($subj); $message->addTo($to, $name); $message->setBody('Email message content.', 'text/plain'); $message->setFrom('your.address@example.com'], 'You'); return $mailer->send($message); } Quote Link to comment Share on other sites More sharing options...
techker Posted October 29, 2017 Author Share Posted October 29, 2017 thx for the info i can't install anything on the server..im not the admin i have a shared hosting account. if i run it i get an error 500.is there a way to put the files in the root of the server? Quote Link to comment Share on other sites More sharing options...
requinix Posted October 29, 2017 Share Posted October 29, 2017 Neither PHPMailer nor SwiftMailer require "installation". They're just code. Please try researching things. Quote Link to comment Share on other sites More sharing options...
techker Posted October 29, 2017 Author Share Posted October 29, 2017 i will.thx Quote Link to comment Share on other sites More sharing options...
techker Posted October 29, 2017 Author Share Posted October 29, 2017 i downloaded PHPmailer and tried out there test folder i keep on getting this Could not execute: /usr/sbin/sendmail i chequed the php info and it is listed in it.... Quote Link to comment Share on other sites More sharing options...
requinix Posted October 30, 2017 Share Posted October 30, 2017 Whatever happened to you using SMTP? Didn't you say something about mail being disabled? Quote Link to comment Share on other sites More sharing options...
techker Posted October 30, 2017 Author Share Posted October 30, 2017 my hosting fixed it.but last question i have a folder phpmailer in the root of the site im doing. do i need all of its content or just the required require 'class.phpmailer.php'; ? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 30, 2017 Share Posted October 30, 2017 When you say you have stored the php mailer code in the 'root' of the site, you don't mean the HTML root, do you? It s/b outside of that area in a place where you most likely store most of your general-purpose PHP code. Maybe even in a sub-folder of that place just to keep it better organized. Quote Link to comment Share on other sites More sharing options...
techker Posted October 30, 2017 Author Share Posted October 30, 2017 (edited) ya i got it going. i need to incluse the phpmailer in all the sites i do. i got the mailer to work for one client.still need to to an array and send emails out to all emails in the array.. this is the page that gathers info : $con=mysqli_connect("localhost","8","8","8"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $todayDate = date("Y-m-d"); //Add $dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month"); //echo "After adding one month: ".date('l dS \o\f F Y', $dateOneMonthAdded)."<br>"; $fiveDays = date ("Y-m-d", strtotime ($today ."-3 days")); $fiveDays3 = date ("Y-m-d", strtotime ($today ."+3 days")); $sql = 'SELECT * FROM Client_Data'; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $ClientDate = $row['Date_registered']; $dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($ClientDate)) . "+1 month"); $ID = $row['Cid']; $Client = $row['ClientID']; $ClientE = $row['Email']; $DatabaseDate = $row['Date_registered']; $DateEx = date('l dS \o\f F Y', $dateOneMonthAdded); $startdate = date("Y-m-d"); $expire = strtotime($startdate. ' + 3 days' ); $today = $row['Date_registered']; if($today >= $expire || $today < $startdate ){ echo "expired"; echo "<meta http-equiv=Refresh content=1;url=test.php?ClientName=$Client&ClientEmail=$ClientE&Expire=$DatabaseDate&CID=$ID>"; } else { echo "active"; } } } mysqli_close($con); send require 'phpmailer/class.phpmailer.php'; $mail = new PHPMailer;$ClientEmail = $_GET['ClientEmail'];$CLientName = $_GET['ClientName'];$DateExpiring = $_GET['Expire'];$ID = $_GET['ID']; $mail->IsSMTP(); // Set mailer to use SMTP $mail->Host = 'mail.tech-xxx.info'; // Specify main and backup server $mail->Port = 2525; // Set the SMTP port $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'test@x-xx.info'; // SMTP username $mail->Password = 'qwer12345'; // SMTP password $mail->SMTPSecure = 'plain'; // Enable encryption, 'ssl' also accepted $mail->From = 'test@xx-xxx.info'; $mail->FromName = 'noreply@xx-xx.info'; $mail->AddAddress($ClientEmail, 'xx'); // Add a recipient //$mail->AddAddress('techker@xxxxx.com'); // Name is optional $mail->IsHTML(true); // Set email format to HTML $mail->Subject = 'Membership'; $mail->Body = $CLientName. " Your membership is set to expire on ".$DateExpiring."<br>"; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->Send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit; } echo 'Message has been sent'; Edited October 30, 2017 by techker Quote Link to comment Share on other sites More sharing options...
gizmola Posted October 31, 2017 Share Posted October 31, 2017 I strongly advise that you look into functions or classes with member functions(methods). For example, imagine the function you might require and the parameters you would need to pass to it, which is no doubt very similar to the series of GET params in your Send script. Rather than putting all the code in a script that you then aren't sure how to execute, things would make a lot more sense if you instead moved all that code into a function perhaps named something like "SendExpirationEmail". This would be called inside your mysql fetch script, and you'd simply be passing the parameters you read from the database, and in turn the function would send an email with each row read. As you have demonstrated, you understand how to include/require what you need from a script in order to make it available in another. Quote Link to comment Share on other sites More sharing options...
techker Posted October 31, 2017 Author Share Posted October 31, 2017 cool thx for the info. Quote Link to comment 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.