PHP Mail & Sending Email

Powerful PHP Mail Techniques to Send Emails Successfully

PHP Mail functionality allows developers to send emails directly from PHP scripts. It is commonly used for contact forms, notifications, password resets, and system alerts.

This tutorial explains PHP Mail & Sending Email step by step with clear examples, outputs, best practices, and FAQs.


What Is PHP Mail Function

PHP provides the mail() function to send emails.

Key Features of PHP Mail

  • Sends plain text or HTML emails
  • Supports headers and attachments
  • Works with server mail configuration
  • Lightweight and easy to use

Why PHP Email Sending Is Important

Email communication is essential in web applications.

Benefits of PHP Mail

  • User notifications
  • Account verification
  • Password recovery
  • System alerts
  • Automated messaging

Basic PHP Mail Syntax Explained

The basic syntax of the PHP mail function is:

mail(to, subject, message, headers);

Each parameter has a specific purpose.


Sending Simple Email Using PHP Mail Function

Example: Send Basic Email

$to = "user@example.com";
$subject = "Welcome to PHP Online";
$message = "Thank you for learning PHP with us.";
$headers = "From: admin@phponline.in";

mail($to, $subject, $message, $headers);
echo "Email sent successfully";

Output

Email sent successfully

Note: Actual delivery depends on server configuration.


Sending HTML Email Using PHP

HTML emails allow better formatting.

Example: HTML Email

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: admin@phponline.in";

$message = "<h2>Welcome</h2><p>Learn PHP step by step.</p>";

mail("user@example.com", "HTML Email", $message, $headers);
echo "HTML email sent";

Output

HTML email sent

Adding Headers in PHP Mail Function

Headers control sender details and content type.

Common Headers

  • From
  • Reply-To
  • CC
  • BCC
  • MIME-Version

Proper headers improve deliverability.


Handling Email Sending Errors in PHP

The mail() function returns true or false.

Example: Check Email Status

if (mail($to, $subject, $message, $headers)) {
    echo "Mail sent";
} else {
    echo "Mail failed";
}

Output

Mail sent

Common Issues While Sending Email in PHP

Typical Problems

  • Mail not delivered
  • Missing mail server
  • Incorrect headers
  • Spam filtering
  • Hosting restrictions

Using SMTP libraries can solve many issues.

Runtime Configuration

 

NameDefaultDescriptionChangeable
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.0PHP_INI_PERDIR
mail.logNULLThe 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
PHP_INI_PERDIR
SMTP\”localhost\”Only for Windows: The SMTP server\’s DNS name or IP addressPHP_INI_ALL
smtp_port\”25\”Only for Windows: The number of the SMTP port.
For PHP versions above 4.3.0
PHP_INI_ALL
sendmail_fromNULLOnly 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

FunctionDescription
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.

Syntax

mail(to,subject,message,headers,parameters);

Here is what is said about each parameter.

Sr.NoParameter & Description
1to

Required.
Identifies the person or people who will receive the email.

2subject

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

3message

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

4headers

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

5parameters

Optional.
Tells the send mail programme about an extra parameter.

More Examples

Send an email with more header information:

<?php
$to = \”user@coderazaa.com\”;
$subject = \”My subject\”;
$txt = \”Hello world!\”;
$headers = \”From: webmaster@coderazaa.com\” . \”\\r\\n\” .
\”CC: userelse@coderazaa.com\”;

mail($to,$subject,$txt,$headers);
?>

Send an HTML email:
<?php
$to = \”user@coderazaa.com, user_two@coderazaa.com\”;
$subject = \”PHP HTML email\”;

$message = \”
<html>
<head>
<title>HTML Mail Example</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
\”;

// 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: <email@coderazaa.com>\’ . \”\\r\\n\”;
$headers .= \’Cc: other_cc@coderazaa.com\’ . \”\\r\\n\”;

mail($to,$subject,$message,$headers);
?>

 

PHP mail function with Form Example

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<!– 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\”>
</textarea>

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

</body>
</html>

 

<?php
// Get data from form
$name = $_POST[\’name\’];
$email= $_POST[\’email\’];
$message= $_POST[\’message\’];

$to = \”yourEmail@mail.com\”;
$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: noreply@coderazaa.com\” . \”\\r\\n\” .
\”CC: email@coderazaa.com\”;
if($email != NULL) {
mail($to, $subject, $txt, $headers);
}

// Redirect to
header(\”Location:thank-you.html\”);
?>

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.

Syntax

ezmlm hash(address);

Parameter Values

Information of how ezmlm_hash work

ParameterDescription
addressRequired.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+

Example

Calculating hash value for email address:

<?php
$user_email = \”someone@coderazaa.com\”;
$hash_value = ezmlm_hash($user_email);

echo \”The hash value for $user_email is: $hash_value.\”;
?>

PHP Mail vs SMTP Libraries Comparison

FeaturePHP MailSMTP Library
SetupEasyModerate
ReliabilityMediumHigh
AuthenticationLimitedStrong
Spam ControlWeakBetter

Best Practices for Sending Email in PHP

Follow these professional tips:

  • Validate email addresses

  • Use proper headers

  • Avoid spam words

  • Use SMTP for production

  • Log email activity


Real World Use Cases of PHP Mail

PHP mail is widely used.

Practical Applications

  • Contact forms

  • Registration confirmation

  • Password reset emails

  • Admin notifications

  • Order confirmations


Frequently Asked Questions About PHP Mail

Does PHP mail work on localhost

Usually no, unless configured.


Is PHP mail secure

It is basic; SMTP is more secure.


Can PHP send attachments

Yes, using MIME headers.


Why emails go to spam

Poor headers or server reputation.


Should beginners learn PHP mail

Yes, it is useful for form handling.


Final Conclusion on PHP Mail & Sending Email

PHP Mail provides a simple yet powerful way to send emails from web applications. While suitable for basic tasks, combining it with best practices ensures reliable communication.

By mastering PHP Mail, you can build interactive, user-friendly, and professional PHP applications.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Prove your humanity: 5   +   2   =