Jump to content

Recommended Posts

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 by techker
Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/#findComment-1553191
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/#findComment-1553194
Share on other sites

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);
}
Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/#findComment-1553218
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/#findComment-1553247
Share on other sites

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 by techker
Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/#findComment-1553248
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/305487-going-nuts-on-this/#findComment-1553253
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.