Within a PHP statement I want to open a MySQL table called calendar events and send any events for Today +1 in an email. Using an array I need to be able capture multiple events for a day. Using an array how do I capture multiple events within a single email (body). Also if I set this up on a Cron job, will the Cron job tell me if the query ran successfully.
Thus my email might be:
Dear Blog member,
On [date]
The following events are scheduled:
[Venue] [event] [start time] end time]
[Venue] [event] [start time] end time]
[Venue] [event] [start time] end time]
You speak like you’re not really a PHP/MySQL guy.
How is your knowledge on the language as the answer may be different according to your experience.
I have read Kevin Yanks book on PHP/SQL programming so I suppose that does not make me an expert, but I am at a level that I can slog my way thru…I am trying to improve my skills so any links etc. would be great…
In his book and when I search the web most php email code seems to iterate one record at a time and place that data found in that one record in the email and then move onto the next record/email.
I want to potentially grab 3 records (Calendar Events) from a table and place those 3 records within the body of the email…
Hope the following will help you or point you to right direction.
$date = date('Y-m-d', strtotime("tomorrow"));
$sql = "SELECT * FROM tblevents WHERE eventdate='$date'";
$result = mysql_query($sql) or die(mysql_error());
$events = '[Venue] - [event] - [start time] - [end time]' . "\
";
if(mysql_num_rows($result) >= 1){
while($event = mysql_fetch_object($result)){
$events .= $event->venue . ' - ' . $event->event . ' - ' . $event->start_time . ' - ' . $event->end_time . "\
";
}
}
$subject = "Events for tomorrow";
$toemail = 'test@test.com';
$emailbody = "Dear Member,
On $date
The following events are scheduled:
$events
Thank you.
Event Team
";
if(mail($toemail, $subject, $emailbody)){
echo 'Email sent';
}
else{
echo 'Not sent because of some problem.';
}
Let me know if anything you want more! But try yourself to understand the functions used above in PHP Manual.
Raju,
This code worked great. The last item I am wondering now is if there is any way of better aligning the data when there multiple events. In the case if 1 row has a long venue name then this particlar row does not align with other rows from a column width perspective…
For that to make it better looking, you go for using some third party library/classes which send the HTML formatted emails. I would recommend you to use PHPMailer. See some examples here:
http://phpmailer.worxware.com/index.php?pg=examples
Or search for some others in Google.
Raj,
I tried PHP mailer, but now everything is on 1 line…i tried /n and i get an error in the code. Do I need to use HTML type formatting within this code such as <p>
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Events for tomorrow";
$mail->Body ="Dear Family Member," "\
"
Yes when you set this to true:
$mail->IsHTML(true); // set email format to HTML
Then you can send HTML emails with complete set of HTML tags and all. So format the email body with HTML (CSS is not supported fully).