PHP Mail & Sending Email

PHP Server mail Config

In the php.ini file, PHP must be set up correctly so that your system can send emails. Open the php.ini file in the /etc/ directory and look for the [mail() function] section.


Windows users should make sure that there are two directives. The first is called SMTP and tells you the address of your email server. The second is called sendmail from, and it lets you set your own email address.

Windows should be set up so that it looks something like this:

[mail function]
; For Win32 only.

; For win32 only
sendmail_from =


For Linux users, all they need to do is tell PHP where their sendmail program is. The sendmail  path directive should be given the path and any switches you want.

Linux should be set up so that it looks something like this:

[mail function]
; For Win32 only.

; For win32 only
sendmail_from =

; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i

You are now ready to go to sending email with php.

PHP Mail Intro

With the mail() function, you can send emails from a script itself.


PHP needs an installed and working email system in order for the mail functions to work. The settings in the php.ini file determine which programme will be used.


PHP’s core includes the mail functions. You don’t have to install anything to use these functions.

Runtime Configuration

Name Default Description Changeable
mail.add_x_header “0” Add X-PHP-Originating-Script, which will include the filename and the UID of the script. For PHP versions above 5.3.0 PHP_INI_PERDIR
mail.log NULL The path to a log file where all calls to mail() will be recorded.
The log shows the full path of the script, the line number, the To address, and the headers. For PHP versions above 5.3.0
SMTP “localhost” Only for Windows: The SMTP server’s DNS name or IP address PHP_INI_ALL
smtp_port “25” Only for Windows: The number of the SMTP port.
For PHP versions above 4.3.0
sendmail_from NULL Only for Windows: Sets the “from” address for mail sent from mail () PHP_INI_ALL
sendmail_path “/usr/sbin/sendmail -t -i” Sets the location of the sendmail programme. This command also works in Windows. SMTP, SMTP port, and sendmail from are ignored if this option is set. PHP_INI_SYSTEM



PHP Mail Functions

Function Description
ezmlm_hash() Calculate the hash value that EZMLM needs.
mail() Scripts can be used to send emails directly.

How to Use and Define php mail

PHP’s mail() function is used to send an email. There are three required arguments for this function: the email address of the recipient, the subject of the message, and the message itself. There are also two optional parameters.

With the mail() function, you can send emails from a script itself.



Here is what is said about each parameter.

Sr.No Parameter & Description
1 to

Identifies the person or people who will receive the email.

2 subject

Tells what the email is about.
There can’t be any newline characters in this parameter.

3 message

Sets the message that will be sent.
A LF (n) should come between each line.
Lines can’t be longer than 70 characters.

4 headers

Adds more headers, such as From, Cc, and Bcc.
A CRLF (rn) should come between each of the extra headers.

5 parameters

Tells the send mail programme about an extra parameter.

More Examples

Send an email with more header information:

$to = “”;
$subject = “My subject”;
$txt = “Hello world!”;
$headers = “From:” . “\r\n” .


Send an HTML email:
$to = “,”;
$subject = “PHP HTML email”;

$message = ”
<title>HTML Mail Example</title>
<p>This email contains HTML Tags!</p>

// Always set content-type when sending HTML email
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type:text/html;charset=UTF-8” . “\r\n”;

// More headers
$headers .= ‘From: <>’ . “\r\n”;
$headers .= ‘Cc:’ . “\r\n”;



PHP mail function with Form Example

<!DOCTYPE html>


<!– form starting –>
<form method=”post” action=”contact.php”>

<!– name –>
<input type=”text” placeholder=”NAME” name=”name” value=”” />

<!– email –>
<input type=”email” placeholder=”EMAIL” name=”email” value=”” />

<!– message –>
<textarea placeholder=”MESSAGE” name=”message”>

<input type=”submit” value=”Submit”>
<!– end of form –>



// Get data from form
$name = $_POST[‘name’];
$email= $_POST[’email’];
$message= $_POST[‘message’];

$to = “”;
$subject = “This is for subject”;

// The following text will be sent
// Name = user entered name
// Email = user entered email
// Message = user entered message
$txt =”Name = “. $name . “\r\n Email = ”
. $email . “\r\n Message =” . $message;

$headers = “From:” . “\r\n” .
if($email != NULL) {
mail($to, $subject, $txt, $headers);

// Redirect to

PHP ezmlm_hash() Function

How to Use and Define

The ezmlm hash() function figures out the required hash value for storing EZMLM mailing lists in a MySQL database.

This function takes an email address and makes an integer hash value out of it. This value works with the EZMLM mailing list manager, and the EZMLM database can then be used to manage users.


ezmlm hash(address);

Parameter Values

Information of how ezmlm_hash work

Parameter Description
address Required.Indicates the email address that is being hashed.

Technical Details

Return Value: Returns the address parameter’s hash value, or FALSE if it fails.
PHP Version: 4.0.2+


Calculating hash value for email address:

$user_email = “”;
$hash_value = ezmlm_hash($user_email);

echo “The hash value for $user_email is: $hash_value.”;

People also search

Leave a Comment

Scroll to Top