Monday, June 28, 2010

PHP's mail function

Assuming you have gotten to this tutorial, then you have successfully installed PHP, MySQL and Apache on your computer. I am also assuming you remember to turn the services ON before you try this tutorial.

In this tutorial, we will write a simple automated PHP emailer... it is the simplest PHP mail function code I could come up with.

Open Komodo. Select 'file' and create a new PHP file. Remember that in order to work, you need to save your PHP files in /Applications/MAMP/htdocs (for MAC) and C:/XAMPP/htdocs/xampp/ (for Windows). See my older tutorials on configuring your PC or Mac for PHP if you are unfamiliar with either environment or are just starting out.

Once you have your blank PHP file open, type in the following: (due to security restrictions of this site I have to leave spaces between ">","<","?" and "php". Please be sure to OMIT those spaces in your final code).

< ? php

$message = "Take me to your leader. This message was generated by PHP code";
$reply_email = "youremail@gmail.com";
$target_email = "yourtarget@gmail.com";

mail($target_email, "Greetings Earthings", $message, "From: $reply_email");

? >

I would recommend setting the target email to an email address you have access to until you are certain that your code is working correctly. Save it as something like emailer.php in /Applications/MAMP/htdocs (for MAC) and C:/XAMPP/htdocs/xampp/ (for Windows)

To actually execute this little program (on a Mac), go to http://localhost:8888/ to get to the index directory. Click on the name of your file. To execute this code (on a PC) go to http://localhost/xampp/emailer.php or whatever you named it.

If it executes, all you will see is a blank white screen. You will have to check your target email to REALLY know if your code is working properly. Please do not jump into this code until you know for SURE that you have your PHP environment working properly FIRST!

No comments:

Post a Comment