How to use smtp mail function in php

In PHP, you can use the mail() function to send an email using SMTP (Simple Mail Transfer Protocol). Here's a basic example of how to use it:

Example:

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

// Use the mail() function to send the email
if (mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email!';
}
?>

In this example, we define the email settings:

We then use the mail() function to send the email. The mail() function takes four arguments:

If the email is sent successfully, the mail() function returns true. If there's an error sending the email, it returns false.

SMTP Settings:

If you want to use a specific SMTP server to send the email, you can use the mail() function with the following syntax:

mail($to, $subject, $message, $headers, 'smtp://username:[email protected]:25');

In this example, we specify the SMTP server settings:

Note: Make sure to replace the placeholders (username, password, smtp.example.com, and 25) with your actual SMTP server settings.

Additional Tips: