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?

No comments: