View Full Version : Send Me An Email Of Birthdays
tomyknoker
03-13-2007, 04:32 PM
I'm not sure how this would work automatically but I was wondering how I can send myself an email say 2 weeks before a member in my database is about to have a birthday? I'm assuming the query would need to be running constantly so would it be possible?
boxxertrumps
03-13-2007, 05:04 PM
Implausible, or at least, not a good idea.
you cant have one script running forever. the server would end up crashing.
tomyknoker
03-13-2007, 05:07 PM
No I'll set up the script and then run it as a cron job, that should work I think...
thetestingsite
03-14-2007, 07:08 PM
Cron would work (or scheduled tasks in a Windows environment), although it is not recommended (due to it being a resource hog). However, if you would like something like this, the script could be something similar to this:
bday.php
<?php
$to = "me@mydomain.com"; //your email address
$subject = "Member's birthdays"; //subject of email
include('dbconnect.php');
$year = date('Y');
$month = date('m');
$results = mysql_query("SELECT * FROM `table` WHERE `birthday` LIKE '$year-$month%'");
if (mysql_num_rows($results) < 1) {
die('There are no members with birthdays this month');
}
else {
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['birthday']) <= (time() + 86400*14)) {
$bdays[] = Array($qry['Name'] => $qry['birthday']);
}
}
$message = 'Below is a list of the members and their birthdays that are coming up in the next couple of weeks. \r\n';
$message .= 'The birthdays are as follows: \r\n';
$message .= '-----------------------------------------\r\n\r\n';
foreach ($bdays as $name => $bdate) {
$message .= 'Name (Birthday): '.$name.' ('.$bdate.') \r\n';
}
$from = "From: PHP Script <noreply@mydomain.com";
mail($to, $subject, $message, $from);
}
?>
Note: not tested, but should work with little modification.
Hope this helps.
tomyknoker
03-14-2007, 08:38 PM
I had a quick question when I am doing includes, is there any difference i doint them like this include
include 'dbconnect.php'; or like this
include('dbconnect.php');
thetestingsite
03-14-2007, 08:41 PM
I don't think that it matters, I've just always done include('somefile.txt'); as compared to your way above. I believe they all work the same, but then again I'm not 100% sure. You may want to look at the php.net website and look for this.
Hope this helps.
tomyknoker
03-14-2007, 08:43 PM
It said there was an error on line 23, I change the names to be what's in my database columns yea?
$bdays[] = $qry['FirstName'] => $qry['DateOfBirth'];
thetestingsite
03-14-2007, 08:46 PM
Try this:
$bdays[] = Array($qry['FirstName'] => $qry['DateOfBirth']);
Then in the code above the while statement, get rid of this:
$bdays = Array();
Hope this helps.
tomyknoker
03-14-2007, 08:50 PM
Cool, well it ran and the result was "There are no members with birthdays this month", but there are about 1000 members so I find that hard to believe... But also on my other birthday post in this forum, no matter what month I change it too it tells me there aren't any bdays... Any ideas?
thetestingsite
03-14-2007, 08:55 PM
What's the link to the other post? Also, I just made that script when I posted it, so it is not tested. Just a quick question, how is the birthdate field formed? (Datetime, text, timestamp, etc?). This could be why you are not getting results because it is searching for any birthdays like this:
2007-03*
2007 is the year, 03 is the month (for March), and * is the wildcard
tomyknoker
03-14-2007, 09:01 PM
Actually the other post will answer your question too http://www.dynamicdrive.com/forums/showthread.php?t=18497&page=2 :)
thetestingsite
03-14-2007, 11:44 PM
Just came to mind, but did you change the following parts in red?
$results = mysql_query("SELECT * FROM `table` WHERE `birthday` LIKE '$year-$month%'");
and
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['birthday']) <= (time() + 86400*14)) {
$bdays[] = Array($qry['Name'] => $qry['birthday']);
}
}
Other than that (that is, if you did change those items above), the script should work.
Hope this helps
tomyknoker
03-14-2007, 11:51 PM
Hmmm yea I did change those bits, but now when I ran it the top line of this is producing a "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource"
if (mysql_num_rows($results) < 1) {
die('There are no members with birthdays this month');
}
else {
$bdays = Array();
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['DateOfBirth']) <= (time() + 86400*14)) {
$bdays[] = Array($qry['FirstName'] => $qry['DateOfBirth']);
}
}
thetestingsite
03-14-2007, 11:57 PM
Well then it is something in this part of the code.
$results = mysql_query("SELECT * FROM `table` WHERE `birthday` LIKE '$year-$month%'");
or, did you make sure to make the mysql connection and select the database? If not, then that would be the cause of the error.
tomyknoker
03-15-2007, 03:40 AM
Ok I got it to email me... But the formatting is all wrong, first to get it to send anything through I query
LIKE '%$month%'", and I recieve an email but it look slike this
Below is a list of the members and their birthdays that are coming up in the next couple of weeks. \r\nThe birthdays are as follows: \r\n-----------------------------------------\r\n\r\nName (Birthday): 0 (Array) \r\nName (Birthday): 1 (Array) \r\nName (Birthday): 2 (Array) \r\nName (Birthday): 3 (Array) \r\nName (Birthday): 4 (Array) \r\nName (Birthday): 5 (Array) \r\nName (Birthday): 6 (Array) \r\nName (Birthday): 7 (Array) \r\nName (Birthday): 8 (Array) \r\nName (Birthday): 9 (Array) \r\nName (Birthday): 10 (Array) \r\nName (Birthday): 11 (Array) \r\nName (Birthday): 12 (Array) \r\nName (Birthday): 13 (Array) \r\nName (Birthday): 14 (Array) \r\nName (Birthday): 15 (Array) \r\nName (Birthday): 16 (Array) \r\nName (Birthday): 17 (Array) \r\nName (Birthday): 18 (Array) \r\nName (Birthday): 19 (Array) \r\nName (Birthday): 20 (Array) \r\nName (Birthday): 21 (Array) Any ideas on this one?
tomyknoker
03-16-2007, 01:27 AM
Hiya any ideas why the email is doing the above? I got so close!
thetestingsite
03-16-2007, 01:31 AM
Sorry, didn't see that you posted anything since my last post. Anyways, I was looking it over and I though of a somewhat better way to do this. Take a look at the following code:
<?php
$to = "me@mydomain.com"; //your email address
$subject = "Member's birthdays"; //subject of email
include('dbconnect.php');
$year = date('Y');
$month = date('m');
$results = mysql_query("SELECT * FROM `table` WHERE `birthday` LIKE '%$month%'");
if (mysql_num_rows($results) < 1) {
die('There are no members with birthdays this month');
}
else {
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['birthday']) <= (time() + 86400*14)) {
$bdays .= 'Name: '.$qry['Name'].' ('.$qry['birthday'].') \r\n';
}
}
$message = 'Below is a list of the members and their birthdays that are coming up in the next couple of weeks. \r\n';
$message .= 'The birthdays are as follows: \r\n';
$message .= '-----------------------------------------\r\n\r\n';
$message .= $bdays;
}
$from = "From: PHP Script <noreply@mydomain.com";
mail($to, $subject, $message, $from);
}
?>
That should take care of the formatting (in the email) part. If not, we may need to send some more header information in the email. Hope this helps.
tomyknoker
03-16-2007, 01:37 AM
Actually I just had a thought, If I get the email every week or even every two weeks, the 14 day will make birthdays overlap, so I think I want to change it slighty if possible. I think what I'll do it make it email me on the 14th of every month, all the members who are having birthdays for the next month... Any idea how to do that?
vaibhav24in
03-16-2007, 09:27 AM
I am not sure.. how does it work but have a look at the codes of phpbb3 forum script. this script sends email automatically who have birthday in your database. If you have someidea about php you will understand the codings. i know it is bit difficult task but will yield you the result of your query for sure
Regards
tomyknoker
03-19-2007, 01:26 AM
Ok have it all up and running, it just does not like the
r/n/, it seems to display them in the message. I tried adding <br> tags but they also just displayed in the message...
tomyknoker
03-20-2007, 12:38 AM
Any ideas about this one?
thetestingsite
03-20-2007, 12:44 AM
Well, it should be
\r\n
If you wanted to put the <br> in it, then you would have to put in the MIME type for the email itself. Try this instead:
$message = 'Below is a list of the members and their birthdays that are coming up in the next couple of weeks. \r\n';
$message .= 'The birthdays are as follows:
';
$message .= '-----------------------------------------
';
$message .= $bdays.'
';
Notice the spaces between the lines. Hope this helps.
or try something like this:
$messages = <<<HERE
Below is a list of the members and their birthdays that are coming up in the next couple of weeks.
The birthdays are as follows:
-----------------------------------------
$bdays
HERE;
tomyknoker
03-20-2007, 12:55 AM
Ah ok the first option didn't change anything, the second option did put the first 2 lines seperately but still displayed the birthdays all together... Any ideas?
thetestingsite
03-20-2007, 12:57 AM
Try replacing this line:
$bdays .= 'Name: '.$qry['Name'].' ('.$qry['birthday'].') \r\n';
with this:
$bdays .= <<<HERE
Name: $qry['Name'] ($qry['birthday'])
HERE;
That should do it. Hope this helps.
tomyknoker
03-20-2007, 01:03 AM
Hmmmm new error is this...
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in \emailBirthdays.php on line 24
PHP Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in \emailBirthdays.php on line 24
Any ideas?
thetestingsite
03-20-2007, 01:07 AM
What's line around line 24? If it is the $bdays variable, try taking out the single quotes in the $qry variables to see if that is it. Either way, post the code so we can see what could be the cause of it.
tomyknoker
03-21-2007, 11:18 AM
Sorry only just saw this... Ok the whole page looks like this
<?php
$to = "email@mydomain.com"; //your email address
$subject = "Member's birthdays"; //subject of email
/* connect to the mysql database and use a query to get the members info */
include 'library/config.php';
include 'library/opendb.php';
$year = date('Y');
$month = date('m');
$results = mysql_query("SELECT * FROM `tblmembers` WHERE `DateOfBirth` LIKE '%$month%'");
if (mysql_num_rows($results) < 1) {
die('There are no members with birthdays this month');
}
else {
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['DateOfBirth']) <= (time() + 86400*14)) {
$bdays .= 'Name: '.$qry['FirstName'].' '.$qry['LastName'].' ('.$qry['DateOfBirth'].') '.$qry['Email'].' \r\n';
}
}
$message = <<<HERE
Below is a list of the members and their birthdays that are coming up in the next couple of weeks.
The birthdays are as follows:
-----------------------------------------
$bdays
HERE;
}
$from = "From: PHP Script <noreply@mydomain.com";
{
mail($to, $subject, $message, $from);
}
?>
thetestingsite
03-29-2007, 12:54 AM
on this line:
$bdays .= 'Name: '.$qry['FirstName'].' '.$qry['LastName'].' ('.$qry['DateOfBirth'].') '.$qry['Email'].' \r\n';
Change to this:
$bdays .= 'Name: '.$qry["FirstName"].' '.$qry["LastName"].' ('.$qry["DateOfBirth"].') '.$qry["Email"].' \r\n';
That should take care of the error message you get. Hope this helps.
tomyknoker
03-29-2007, 01:00 AM
Thanks that fixed the error, but it still just displays them all one after the other on one big line and shows the "\r\n", I was hoping to be able to get them, to display like and also with the DOB lookinbg like that below, it displays it like YYYY-MM-DD...
Name: John Smith (17-01-1980) johnsmith@domain.com
Thanks again!
thetestingsite
03-29-2007, 01:29 AM
Try replacing the while loop:
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['DateOfBirth']) <= (time() + 86400*14)) {
$bdays .= 'Name: '.$qry['FirstName'].' '.$qry['LastName'].' ('.$qry['DateOfBirth'].') '.$qry['Email'].' \r\n';
}
}
with this:
while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['DateOfBirth']) <= (time() + 86400*14)) {
$bdays .= 'Name: '.$qry["FirstName"].' '.$qry["LastName"].' ('.date('d-m-Y', strtotime($qry["DateOfBirth"]).') '.$qry["Email"].'
';
}
}
Not tested, but should work. Hope this helps
tomyknoker
03-29-2007, 01:34 AM
Hiya ok it doesn't like something on this line... I think it must be an ' but tried a few different additions and still no luck
$bdays .= 'Name: '.$qry["FirstName"].' '.$qry["LastName"].' ('.date('d-m-Y', strtotime($qry["DateOfBirth"]).') '.$qry["Email"].'';
thetestingsite
03-29-2007, 01:38 AM
Ah yes, forgot to the single quotes :). Try this:
$bdays .= 'Name: '.$qry["FirstName"].' '.$qry["LastName"].' ('.date("d-m-Y", strtotime($qry["DateOfBirth"])).') '.$qry["Email"].'
';
(Note: the parts in red are what I have changed.
tomyknoker
03-29-2007, 01:59 AM
Ug it still just displays the same, it's ignored that line. They all display in one big line and the date didn't change very wierd...
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.