How can a local server without a domain or email account send emails using the mail() function?

To send emails using the mail() function from a local server without a domain or email account, you can use a third-party SMTP service like Gmail's SMTP server. This involves configuring your PHP script to use the SMTP server settings provided by the third-party service to send emails.

<?php

$to = 'recipient@example.com';
$subject = 'Test Email';
$message = 'This is a test email sent from a local server without a domain or email account.';
$headers = 'From: yourname@example.com';

// Configure SMTP settings
ini_set('SMTP', 'smtp.gmail.com');
ini_set('smtp_port', 587);
ini_set('sendmail_from', 'yourname@gmail.com');
ini_set('sendmail_path', '/usr/sbin/sendmail -t -i');

// Send email
$mail_sent = mail($to, $subject, $message, $headers);

if($mail_sent) {
    echo 'Email sent successfully.';
} else {
    echo 'Email sending failed.';
}

?>