Tuesday, April 19, 2011

Sending Email with PHP

With server-side programming you can do just about anything, and that includes sending email. Try this little PHP program, by changing the contents of demo.php to the following and then surfing to the demo.php page:
<?php
$to = "robert@the-web-book.com";
$subject = "Hello";
$message = "This is my Web server, sending me email on ";
$message .= date("D");
$message .= " at ";
$message .= date("H:i");
$m = mail($to,$subject,$message);
echo "Return code from mail was " . $m;
exit();
?>
You should see a message which says "Return code from mail was 1". To understand why, and what this program does, let’s examine it in detail.
The first line of code creates a new variable called $to, which is set to the "to" address of the email we’re going to be sending. In this case, I’m sending the message to myself. If you want to try this example, please use your own email address rather than mine!
The next line sets the subject of the message in a similar way, and then we create $message, which is the text of the actual message we’ll be sending.

But what about $message .= date("D");? Are we setting $message to something else entirely? No. Take a closer look and you’ll see we’re using .= rather than = on its own. The .= symbol means that PHP should add the new text to the end of what’s in that variable already. But what text to add? In this case we’re using the date() function and specifying "D" as what’s known as the argument. The date() function returns lots of time- and daterelated information, such as the current day, date, hour, minute, second, year, and so on.
There are dozens of possible things that it can return, each symbolized by their own letter (and these are case-sensitive, by the way, so d is not the same as D).
In this case, D returns the current day of the week as a 3-letter abbreviation. So, this line is adding a 3-letter version of today’s day onto the end of the $message string which we created in the line above. Thus, if today happens to be Friday, $message will now be "This is my Web server, sending me email on Fri".

The next line adds " at " to the $message string. So now it contains "This is my Web server, sending me email on Fri at ".
Finally we add the current time to the end of the message that we’ll be emailing. We use the date() function again, with H and i. H returns the current hour in 24-hour clock format, and i returns the number of minutes. Anything that isn’t a letter, within the bracketed part of the date() function, gets returned as-is. So if the time happened to be 9pm, date("H:i") would return 21:00.
PHP is particularly good at processing and manipulating strings and dates. Whatever you need to do with a string (count the number of characters, convert it to upper or lower case, remove the last 4 characters, etc etc), PHP has a function to do it. As for dates, there are even functions which will, if you supply a date and a latitude/longitude location, tell you the time of the sunrise or sunset.

Right, back to our program. Next, we use the mail() command to send the message. It’s as simple as specifying, in the correct order, the "to" address, the "from" address, and the text of the message to be sent. But note the $m = bit at the start, though. The mail() function returns a value to indicate whether the message was successfully sent to the mail server for delivery. We capture this into the $m variable, and display it in the next line.
Finally, the exit() statement ends the PHP program. You don’t need to use this if it’s unambiguous, as it is here. The program is clearly going to end, as there’s no more PHP code left to run.


0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms