How to enable php mail from server

Enabling PHP mail from a server involves configuring the server's mail settings and PHP settings to allow PHP scripts to send emails. Here's a step-by-step guide to help you enable PHP mail from your server:

Step 1: Check if PHP mail is already enabled

Before you start, check if PHP mail is already enabled on your server. You can do this by creating a simple PHP script with the following code:

<?php
if (function_exists('mail')) {
    echo "PHP mail is enabled";
} else {
    echo "PHP mail is not enabled";
}
?>

If the script outputs "PHP mail is enabled", you can skip to Step 3. If it outputs "PHP mail is not enabled", proceed with the steps below.

Step 2: Check your server's mail settings

Check your server's mail settings to ensure that the mail server is configured correctly. You can do this by checking your server's configuration files, such as:

Look for settings such as mydestination, myhostname, myorigin, and relayhost. These settings should be configured correctly to allow your server to send emails.

Step 3: Configure PHP mail settings

Configure PHP to use your server's mail settings. You can do this by editing your PHP configuration file (php.ini) or by using a PHP configuration file (php5.ini or php7.ini).

Add the following settings to your PHP configuration file:

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
sendmail_from = [email protected]

; For Unix only.  You may adjust the following settings as needed.
; For a basic setup, leave these comments in place and only remove the ;
; lines that you are sure you don't need.
;sendmail_path = /usr/sbin/sendmail -t -i

Replace [email protected] with your actual email address.

Step 4: Restart your web server

Restart your web server to apply the changes. This will ensure that the PHP configuration changes take effect.

Step 5: Test PHP mail

Create a new PHP script with the following code:

<?php
$to = '[email protected]';
$subject = 'Test email from PHP';
$message = 'This is a test email sent from PHP';
$headers = 'From: [email protected]'. "\r\n".
    'Reply-To: [email protected]'. "\r\n".
    'MIME-Version: 1.0'. "\r\n".
    'Content-Type: text/plain; charset=UTF-8';

if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully";
} else {
    echo "Email failed to send";
}
?>

Replace [email protected] with your actual email address.

Run the script and check your email inbox to see if the email was sent successfully.

That's it! If you've followed these steps, PHP mail should now be enabled on your server.