Tuesday, May 15

PEAR Mail for Contact Info Check

I was asked to make an email that would send out every 6 months to see if the user's contact information had changed. So I needed to send an email to everyone in our database comprised of their information. I already mentioned using scheduled tasks, but we've got to make the file for it to run first. I thought it was going to be complicated, but it turned out to be quite easy. All I had to do was put the entire email into a while loop that went through the database. This is where the PEAR mail class definitely outshines PHP mail() because you can set a persistent connection to the server until you're done sending emails.

include 'Mail.php';

$get_contact = mysql_query("SELECT * FROM contact_info");
while($row = mysql_fetch_assoc($get_contact)){
$recipients = $row['email'];

$params['host'] = 'smtp.server.com';
$params['persist'] = true;

$headers['To'] = $row['FirstName'].' '.$row['LastName'].' <'.$row['email'].'>';
$headers['From'] = 'Me <mail@mail.com>';
$headers['Subject'] = 'Please Check Contact Information';

$body = 'Dear '.$row['FirstName'].' '.$row['LastName'].',

Some stuff about you should check your contact info. Here's your contact info';

//then just list their info in the body or add more to the body with .=
//then just mail it like before

$mail_object =& Mail::factory('smtp', $params);
$mail_object->send($recipients, $headers, $body);

} //don't forget you're in a while loop

Of course, if someone's gotten a new email and hasn't updated your list, you're still screwed, but oh well! What can ya do?

PEAR Templates

I recently discovered the place I work for has made their website templates as a PEAR extension. Very cool. I've been in the process of converting my pages. It's nice to work with because, for example, the standard no content template is a whopping 105 lines long. That's really long! The PEAR templates can do the same thing in 6 lines. I've reduced some of my pages from over 300 lines to less than 100. They still look the same and there's all those lines if you view the source on the page, but it's much much easier to work with this way. There's still some bugs, but they're working on that (hopefully).

So, I know this helps no one out there, it's just opinion. I would like to say that I've found the PEAR modules I've used mostly to be helpful (once I figured out how to use them).

Monday, May 7

lols for typos

My friend just showed me this and it was so funny I just had to share.

running Debian/Ubuntu box, type:

sudo apt-get install sl

after it finishes, run sl

read the man page for it for even more silliness

!!!

When is Date() a string?

Simple example says it all:


<?php
include 'Date.php';
$date = new Date();
$d = $date->format('%Y-%m-%D');

if(is_string($date)){
echo "date is a string<br />";
}else echo "date is not a string<br />";

if(is_string($d)){
echo "d is a string<br />";
}else echo "d is not a string<br />";
?>

returns:

date is not a string
d is a string

Friday, May 4

More PEAR Mail Tidbits

Yep, still playing with it. I'm sending emails to people who've decided they want to receive updates from the organization. I did like:

$query = mysql_query("SELECT email FROM info WHERE rcv_updates = 'yes'");
while($row = mysql_fetch_assoc($query)){
$headers['bcc'] .= $row['email'].",";
$recipients .= $row['email'].",";
}
Then all the regular stuff. It was kind of a shot in the dark for me, Not quite sure if it would work. Well, it did. The cool thing was since I didn't specify a To and only bcc's, the To field in the email I got was "undisclosed-recipients:;"

neat

Wednesday, May 2

Scheduled Tasks FTW

So easy thing today. I'd never done it before so it feels like an accomplishment. I need to send automatic emails every 6mo or so... enter Scheduled Tasks. So I created my script, just a php file that would send a mail when ran. Opened scheduled tasks... made the mistake of trying to use the wizard. Bad idea. That wizard is for n00bs so I couldn't figure it out (ha). Here's my steps:

  1. Right click -> new-> task
  2. Name it
  3. Double click it
  4. in run: type -- C:\php5\php.exe "C:\php5\includes\schedtest.php"
  5. set schedule in Schedule tab
  6. set password
That was it. Seriously. That easy. I couldn't believe it, but it actually worked.

Tuesday, May 1

PEAR Date compare()

Alright. I've got this figured out. I'm not sure if this is exactly how you're supposed to do stuff, being new to PEAR as I am, but this seems to work.

The problem:
I have due dates stored in a database. On the webpage, I want to say the due date, then say how many days until the due date (or how many days have passed since the due date.

The code:

We start of creating two date objects, one from the database, one for today.

$due = new Date($row['due_date']);
$d = new Date();
so now we want to compare them. PEAR has a cute little compare function for us. It returns 0 if the dates are equal, -1 if date1 is before date2 and 1 if date1 is after date2.
$comp = Date::compare($due, $d);
So now, we'll get 0, -1 or 1 and we need to handle them. And I want to find out how many days till the project is due.
if($comp == '1'){
$x = Date_Calc::dateToDays($due->day, $due->month, $due->year) - Date_Calc::dateToDays($d->day, $d->month, $d->year);
$daysleft = $x.' days left';
}elseif($comp == -1){
$x = Date_Calc::dateToDays($d->day, $d->month, $d->year) - Date_Calc::dateToDays($due->day, $due->month, $due->year);
$daysleft = $x.' days overdue';
}else $daysleft = 'today';
So the Date_Calc::dateToDays returns a number. It's the number of days since the Date_Calc epoch. It doesn't really matter what it is, it gives us a constant to work with. Just subtracting them returns the number of days. So my page really just says:
<strong>Date Due:</strong> <?=$due->format('%A, %B %e, %Y')?> (<?=$daysleft?>)
and displays:

Date Due: Friday, June 15, 2007 (43 days left)