πŸŽ‰ New: Top 75 PHP Interview Questions for 2026 β€” Free for all learners

PHP Mail & Sending Email

P
php Guru
Β· February 9, 2023 Β· 7 min read Β· Updated February 9, 2023

πŸ“Œ Key Takeaways

  • PHP Mail & Sending Email
  • What Is PHP Mail Function
  • Why PHP Email Sending Is Important
  • Basic PHP Mail Syntax Explained
  • Sending Simple Email Using PHP Mail Function
  • Sending HTML Email Using PHP
Advertisement

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" . "rn";
$headers .= "Content-type:text/html;charset=UTF-8" . "rn";
$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

Β 

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
PHP_INI_PERDIR
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
PHP_INI_ALL
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.

Syntax

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

Here is what is said about each parameter.

Sr.No Parameter & Description
1 to

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

2 subject

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

3 message

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

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

5 parameters

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

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+

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

Feature PHP Mail SMTP Library
Setup Easy Moderate
Reliability Medium High
Authentication Limited Strong
Spam Control Weak Better

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.

P
php Guru
← Previous Post
PHP Form Example
Next Post β†’
PHP Date and Time

Leave a Reply

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

Prove your humanity: 10   +   7   =