Thursday 25 September 2014

PHP Mailer Script Step by Step

Introduction

This tutorial describes how to use PHPMailer and explains the use of the class functions. by Tom Klingenberg

Contents

  1. #PHPMailer: What is it? What does it do? Who needs it? Brief examples of PHPMailer's features.
  2. #First_time: Sending your first email with PHPMailer. Tells you how to start. Anyone who completely understands the source on the PHPMailer homepage or README can skip this section.
  3. #Using_Attachments: Sending mails with file attachments: filesystem, database or inline.
  4. #Using_HTML_Mail: Using the class' integrated HTML Email features.
  5. #Support: Where do I get help?: How to find support resources: PHPMailer website, mailing list etc.
  6. #Other_Resources: Email and PHP Resources: There is a lot of information in the net about email, php and PHPMailer.

PHPMailer

PHPMailer is a PHP class for PHP (www.php.net) that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments. PHPMailer supports nearly all possiblities to send email: mail(), Sendmail, qmail & direct to SMTP server. You can use any feature of SMTP-based e-mail, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send e-mail within PHP.
As you may know, it is simply to send mails with the PHP mail() function. So why use PHPMailer? Isn't it slower? Yes that's true, but PHPMailer makes it easy to send e-mail, makes it possible to attach files, send HTML e-mail, etc. With PHPMailer you can even use your own SMTP server and avoid Sendmail routines used by the mail() function on Unix platforms.
This tutorial explains how to implement the class into your script or website and how to build an e-mail application. If you just want to send simple e-mail and you have no problems with the mail() function, it's suggested that you continue to use mail(), instead of this class.

First time

For those who have not used PHP's mail() function before, this section will provide information about email and e-mailing in general, in addition to explaining how to get started with PHPMailer.
Before continuing, please be sure that PHPMailer is installed correctly. If you feel uncertain, please read the installion instructions that accompany the package. If you're still not sure, you can verify that you've installed PHPMailer correctly with this little script:
<?phprequire("class.phpmailer.php");
$mail
= new PHPMailer();
?>
Save it as a PHP document in the same directory where you've saved class.phpmailer.php. If no errors result from running the script, your installation has been done correctly. If you switched error messages and warnings off, then go ahead and set it on; refer to your PHP manual for more on this topic.
This snippet of code is also the start of any use of PHPMailer. require("class.phpmailer.php"); includes the source code of the class and $mail = new PHPMailer(); creates a new instance of the class the class as $mail. All features, functions and methods of the class maybe accessed via the variable (object) $mail.
Merely creating an instance of the class is only the first step, of course. Let's go ahead and send out the first mail. For this you'll require the basics: An recipient address, a from address, a subject and the text of the message (called "mail-body" or simply "body" for short).
You will also need to select a method of delivering the message. We need a program to communicate with an SMTP server which, in turn, sends the mail out to the internet. In the Unix world, Sendmail is very popular and used widely.
PHPMailer provides several methods of sending mail. It's best to start with SMTP, though (as mentioned above) the mail() function, Sendmail or qmail are also possible. When using SMTP, you'll need a valid SMTP server; this may well be the same one that you use in your personal mail client.
Having gathered the necessary information to send your first e-mail, you'd use that information in place of the example information provided here:

<?php
require("class.phpmailer.php");

$mail
= new PHPMailer();

$mail
->IsSMTP();  // telling the class to use SMTP
$mail
->Host     = "smtp.example.com"; // SMTP server

$mail
->From     = "from@example.com";
$mail
->AddAddress("myfriend@example.net");

$mail
->Subject  = "First PHPMailer Message";
$mail
->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail
->WordWrap = 50;
if(!$mail->Send()) {
  echo
'Message was not sent.';
  echo
'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo
'Message has been sent.';
}
?>
Save this as a php file and change the values to your values, of course. Here is an explanation of what each stanza in this script does:
$mail->IsSMTP();
This sets up STMP-Server as method to send out email(s).
$mail->Host = "smtp.example.com";
Setting smtp.example.com as the SMTP server. Just replace it with your own SMTP server address. You can even specify more then one: just separate them with a semicolon (;): "smtp.example.com;smtp2.example.com". If the first one fails, the second one will be used, instead.
Enter the address that the e-mail should appear to come from. You can use any address that the SMTP server will accept as valid.
$mail->From = "from@example.com";
If displaying only an e-mail address in the "From" field is too simple, you can add another line of code to give the address an associated name. This will add 'Your Name' to the from address, so that the recipient will know the name of the person who sent the e-mail. The following line
$mail->FromName = "Your Name";
Andy Prevost: with PHPMailer version 5.0.0, you can now achieve this with one command:

$mail->SetFrom("from@example.com","Your Name");
The following will add the to address, the address to which the e-mail will be sent. You must use a valid e-mail here, of course, if only so that you can verify that your PHPMailer test worked. It's best to use your own e-mail address here for this inintial test. As with the "From" field, you may provide a name for the recipient. This is done somewhat differently:
$mail->AddAddress("myfriend@example.net","Friend's name");
$mail
->AddAddress("myfriend@example.net");
Setting the subject and body is done next. $mail->WordWrap = 50; is a feature of PHPMailer to word-wrap your message limiting all lines to a maximum length of X characters, even if not set as body= .... Admittedly, there's not much point in wrapping the text in a message as brief as the one in this example.
Finally, we send out the e-mail, once all necessary information has been provided. This is done with $return = $mail->Send();. In this example script, it's combined with an error message; if Send() fails, it'll return false and you can catch it and display an error message. This is done in the last lines. Or, in case of success, it displays a kind of "done " message.

Using Attachments

Sending plain text e-mails is often insufficient. Perhaps you need to attach something to your mail, such as an image or an audio file. Or perhaps you need to attach multiple files.
There are two ways of attaching something to your mail: You can simply attach a file from the filesystem or you can attach (binary) data stored in a variable. The latter is called Stringattachment. This makes it possible put extract data from a database and attach it to an e-mail, without ever having to actually save it as a file.

File Attachments

The command to attach a file can be placed anywhere between $mail = new PHPMailer(); and !$mail->Send(); and it's called AddAttachment($path);. This single line will add the attachment to your mail.
$path is the path of the filename. It can be a relative one (from your script, not the PHPMailer class) or a full path to the file you want to attach.
If you want more options or you want to specify encoding and the MIME type of the file, then you can use three more parameters, all of which are optional:
AddAttachment($path,$name,$encoding,$type);
$name is an optional parameter, used to set the name of the file that will be embedded within the e-mail. The person who will recieve your mail will then only see this name, rather than the original filename.
$encoding is a little more technical, but with this parameter you can set the type of encoding of the attachment. The default is base64. Other types that are supported include: 7bit, 8bit, binary & quoted-printable. Please refer to your SMTP documentation about encoding and the differences between types. In general, mail servers will convert encodings they don't want to handle into their preferred encoding type.
$type is the MIME type of the attached file. Content types are defined not necessarily by file suffixes (i.e., .GIF or .MP3), but, rather, a MIME type (MIME = Multipurpose Internet Mail Extensions) is used. This parameter makes it possible change the MIME type of an attachment from the default value of application/octet-stream (which works with every kind of file) to a more specific MIME type, such as image/jpeg for a .JPG photo, for instance.

String Attachments

String attachments have been supported since PHPMailer version 1.29. This method works much like AddAttachment(), and is called with AddStringAttachment($string,$filename,$encoding,$type). The string data is passed to the method with the first parameter, $string. Because the string will become a standard file (which is what the attachment will be when received via e-mail), the $filename parameter is required. It's used to provide that filename for the string data.
The rest is just the same as described in detail above.
So, why use AddStringAttachment() instead of AddAttachment()? Is it for text-only files? No, not at all. It's primarily for databases. Data stored in a database is always stored as a string (or perhaps, in the case of binary data, as as a BLOB: Binary Large OBject). You could query your database for an image stored as a BLOG and pass the resulting string to the AddStringAttachment().

Inline Attachments

There is an additional way to add an attachment. If you want to make a HTML e-mail with images incorporated into the desk, it's necessary to attach the image and then link the tag to it. For example, if you add an image as inline attachment with the CID my-photo, you would access it within the HTML e-mail with `&ltimg src="cid:my-photo" alt="my-photo" />.
In detail, here is the function to add an inline attachment:
$mail->AddEmbeddedImage(filename, cid, name);
By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');
For more Information about HTML Email, see the section Using HTML E-Mail.

Handling Attachments

If you want to attach multiple files (or strings), just call AddAttachment() or AddStringAttachment() multiple times. All attachments (file, string, and inline) may be stripped from an e-mail by invoking ClearAttachments().

Using HTML Email

Sending out HTML e-mail is a simple enough task with PHPMailer, though it can require significant knowledge of HTML. In particular, mail clients vary greatly in their rendering of HTML e-mail, with some refusing to show it entirely. For those mail clients which are not able to display HTML, you can provide an alternate e-mail body containing the message as plain text.
First we'll create a basic HTML message:

<?php
require("class.phpmailer.php");

$mail
= new PHPMailer();

$mail
->IsSMTP();  // telling the class to use SMTP
$mail
->Host     = "smtp.example.com"; // SMTP server

$mail
->From     = "from@example.com";
$mail
->AddAddress("myfriend@example.net");

$mail
->Subject  = "An HTML Message";
$mail
->Body     = "Hello, <b>my friend</b>! \n\n This message uses HTML entities!";
?>
   
It's as easy as creating a plain text e-mail. Simply use a string with embedded HTML for $mail->body.
Before sending this out, we have to modify the PHPMailer object to indicate that the message should be interpreted as HTML. Technically-speaking, the message body has to be set up as multipart/alternative. This is done with:
$mail->IsHTML(true);
To make ensure that the recipient will be able to read the e-mail, even if his e-mail client doesn't support HTML, use $mail->AltBody="Hello, my friend! \n\n" This message uses HTML entities, but you prefer plain text !"; to set the alternative body (AltBody in short). If you use this feature, the mail will be automatically set as multipart/alternative, making it unnecessary to specify $mail->IsHTML(true);.
And that's how to create an HTML email. Simply apply the send command and the mail will be sent. It's recommended that you avoid using HTML messages, because these messages are often used for viruses and exploits, they are much larger than plain text mails, and they're not standarized in the way the plain text mail is. On the other hand, even attachments are used for viruses, but nobody would propose that we stop sending mails with attachments. Do what fits your needs: PHPMailer can be your solution, whatever you decide.
If you want to send out a HTML email with pictures, Flash animations or whatever else, PHPMailer supports this as well.
Andy Prevost: Please note that it is not possible to send anything but pure W3C compliant HTML inline with your emails. That means no scripts, no Flash, etc. ... these can be included as attachments, but not as inline viewable objects – other than images (JPG, JPEG, PNG, & GIF). While some email clients may be able to display some objects such as other image types, the vast majority of email clients will either block them, crash, or remove them entirely as inline objects.
Adding an inline picture to your HTML e-mail, for example, is explained in the Inline Attachments section. Briefly, here are 2 lines of code you'd use to insert an image before sending out an e-mail:
 
$mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png");
$mail
->Body = 'Embedded Image: <img alt="PHPMailer" src="cid:my-attach"> Here is an image!';

PHP Mail Script with SMTP Authentication


<?php  
//new function  
 
$to = "you@your-domainname.com";  
$nameto = "Who To";  
$from = "script@your-domainname.com";  
$namefrom = "Who From";  
$subject = "Hello World Again!";  
$message = "World, Hello!";  
authSendEmail($from$namefrom$to$nameto$subject$message);  
?>  
 
 
<?php  
/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */   
 
//This will send an email using auth smtp and output a log array  
//logArray - connection,   
 
function authSendEmail($from$namefrom$to$nameto$subject$message)  
{  
//SMTP + SERVER DETAILS  
/* * * * CONFIGURATION START * * * */ 
$smtpServer = "mail.ukdns.biz";  
$port = "25";  
$timeout = "30";  
$username = "your-email-address@domain.com";  
$password = "Your-POP3-Password";  
$localhost = "mail.ukdns.biz";  
$newLine = "\r\n";  
/* * * * CONFIGURATION END * * * * */ 
 
//Connect to the host on the specified port  
$smtpConnect = fsockopen($smtpServer$port$errno$errstr$timeout);  
$smtpResponse = fgets($smtpConnect, 515);  
if(empty($smtpConnect))   
{  
$output = "Failed to connect: $smtpResponse";  
return $output;  
}  
else 
{  
$logArray['connection'] = "Connected: $smtpResponse";  
}  
 
//Request Auth Login  
fputs($smtpConnect,"AUTH LOGIN" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authrequest'] = "$smtpResponse";  
 
//Send username  
fputs($smtpConnect, base64_encode($username) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authusername'] = "$smtpResponse";  
 
//Send password  
fputs($smtpConnect, base64_encode($password) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authpassword'] = "$smtpResponse";  
 
//Say Hello to SMTP  
fputs($smtpConnect"HELO $localhost" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['heloresponse'] = "$smtpResponse";  
 
//Email From  
fputs($smtpConnect"MAIL FROM: $from" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailfromresponse'] = "$smtpResponse";  
 
//Email To  
fputs($smtpConnect"RCPT TO: $to" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailtoresponse'] = "$smtpResponse";  
 
//The Email  
fputs($smtpConnect"DATA" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data1response'] = "$smtpResponse";  
 
//Construct Headers  
$headers = "MIME-Version: 1.0" . $newLine;  
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
$headers .= "To: $nameto <$to>" . $newLine;  
$headers .= "From: $namefrom <$from>" . $newLine;  
 
fputs($smtpConnect"To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data2response'] = "$smtpResponse";  
 
// Say Bye to SMTP  
fputs($smtpConnect,"QUIT" . $newLine);   
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['quitresponse'] = "$smtpResponse";  

//insert var_dump here -- uncomment out the next line for debug info
//var_dump($logArray);
}  
?>

Simple Form to Email PHP Contact Form

Below you can find a basic website form using only HTML (for the form) and PHP for the form processing. Along-side the HTML form you will find a basic PHP script which will capture the form submissions and send the form contents to your email address.
The form we provide below is a "bare-bone" version. This will allow you to edit the form to fit with your own website design. You could edit the form in dreamweaver, HTML-Kit, iweb or any other HTML editor of your choice.

If you don't want to spend hours learning to make your own forms, you can use our form builder to create forms in only a few minutes.
It's unbelievably easy to use!

Basic website form HTML

 

Below is the HTML form sample. As we mentioned above, you can edit the style of this to match your websites design.
File Name: contactform.htm (you can change the filename to anything you like)


<form name="contactform" method="post" action="send_form_email.php">
<table width="450px">
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Comments *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
 </td>
</tr>
</table>
</form>


The PHP Code which captures and Emails your website form

The PHP code below is very basic - it will capture the form fields specified in the HTML form above (first_name, last_name, email, telephone and comments). The fields are then sent off to your email address in plain text.
Note: You need to edit 2 parts of the script below. You need to tell it your email address (this will not be available for anyone to see, it is only used by the server to send your email). You can also specify an email subject line (or just leave the one which is there).
File Name: send_form_email.php (you must use this filename exactly)


<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "you@yourdomain.com";
    $email_subject = "Your email subject line";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

Save the files above. Once you edit the form to fit with your design, you are ready to put it live.


Example Form


Saturday 20 September 2014

Creating a simple module/Developing a Basic Module

A module is a lightweight and flexible extension. They are used for small bits of the page that are generally less complex and are able to be seen across different components.
You can see many examples of modules in the standard Joomla! install: - menus - Latest News - Login form - and many more.
This tutorial will explain how to go about creating a simple Hello World module. Through this tutorial you will learn the basic file structure of a module. This basic structure can then be expanded to produce more elaborate modules.

File Structure

There are four basic files that are used in the standard pattern of module development:
  • mod_helloworld.php - This file is the main entry point for the module. It will perform any necessary initialization routines, call helper routines to collect any necessary data, and include the template which will display the module output.
  • mod_helloworld.xml - This file contains information about the module. It defines the files that need to be installed by the Joomla! installer and specifies configuration parameters for the module.
  • helper.php - This file contains the helper class which is used to do the actual work in retrieving the information to be displayed in the module (usually from the database or some other source).
  • tmpl/default.php - This is the module template. This file will take the data collected by mod_helloworld.php and generate the HTML to be displayed on the page.

Creating mod_helloworld.php

The mod_helloworld.php file will perform three tasks:
  • include the helper.php file which contains the class to be used to collect the necessary data
  • invoke the appropriate helper class method to retrieve the data
  • include the template to display the output.
The helper class is defined in our helper.php file. This file is included with a require_once statement:
require_once( dirname(__FILE__).'/helper.php' );
require_once is used because our helper functions are defined within a class, and we only want the class defined once.
Our helper class has not been defined yet, but when it is, it will contain one method: getHello(). For our basic example, it is not really necessary to do this - the “Hello, World” message that this method returns could simply be included in the template. We use a helper class here to demonstrate this basic technique.
Our module currently does not use any parameters, but we will pass them to the helper method anyway so that it can be used later if we decide to expand the functionality of our module.
The helper class method is invoked in the following way:
$hello = modHelloWorldHelper::getHello( $params );

Completed mod_helloworld.php file

The complete mod_helloworld.php file is as follows:

<?php
/**
* Hello World! Module Entry Point
*
* @package Joomla.Tutorials
* @subpackage Modules
* @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
* @license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Include the syndicate functions only once
require_once( dirname(__FILE__).'/helper.php' );

$hello = modHelloWorldHelper::getHello( $params );
require( JModuleHelper::getLayoutPath( 'mod_helloworld' ) );
?> 
 
The one line that we haven’t explained so far is the first line. This line checks to make sure that this file is being included from the Joomla! application. This is necessary to prevent variable injection and other potential security concerns.

Creating helper.php

The helper.php file contains that helper class that is used to retrieve the data to be displayed in the module output. As stated earlier, our helper class will have one method: getHello(). This method will return the ‘Hello, World’ message.
Here is the code for the helper.php file:

<?php
/**
* Helper class for Hello World! module
*
* @package Joomla.Tutorials
* @subpackage Modules
* @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
* @license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

class modHelloWorldHelper
{
/**
* Retrieves the hello message
*
* @param array $params An object containing the module parameters
* @access public
*/

public static function getHello( $params )
{
return 'Hello, World!';
}
}
?> 
 
There is no rule stating that we must name our helper class as we have, but it is helpful to do this so that it is easily identifiable and locateable. Note that it is required to be in this format if you plan to use the com_ajax plugin.
More advanced modules might include database requests or other functionality in the helper class method.

Creating tmpl/default.php

The default.php file is the template which displays the module output.
The code for the default.php file is as follows:

<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<?php echo $hello; ?> 
 
An important point to note is that the template file has the same scope as the mod_helloworld.php file. What this means is that the variable $hello can be defined in the mod_helloworld.php file and then used in the template file without any extra declarations or function calls.

Creating mod_helloworld.xml

The mod_helloworld.xml is used to specify which files the installer needs to copy and is used by the Module Manager to determine which parameters are used to configure the module. Other information about the module is also specified in this file.
The code for mod_helloworld.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
<name>Hello, World!</name>
<author>John Doe</author>
<version>1.0.0</version>
<description>A simple Hello, World! module.</description>
<files>
<filename>mod_helloworld.xml</filename>
<filename module="mod_helloworld">mod_helloworld.php</filename>
<filename>index.html</filename>
<filename>helper.php</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
</files>
<config>
</config>
</extension> 
 
Manifest files explains the technical details of the elements used in the XML file.
You will notice that there are two additional files that we have not yet mentioned: index.html and tmpl/index.html. These files are included so that these directories cannot be browsed. If a user attempts to point their browser to these folders, the index.html file will be displayed. These files can be left empty or can contain the simple line:

<html><body bgcolor="#FFFFFF"></body></html> 
 
which will display an empty page.
Since our module does not use any form fields, the config section is empty.

Conclusion

Module development for Joomla! is a fairly simple, straightforward process. Using the techniques described in this tutorial, an endless variety of modules can be developed with little hassle.