Php mail smtp authentication without pear

You can use the mail() function in PHP to send emails using SMTP authentication without using PEAR. Here's an example:

<?php
// Define the SMTP server settings
$smtp_server = 'smtp.example.com';
$smtp_port = 587;
$smtp_username = '[email protected]';
$smtp_password = 'your_password';

// Define the email settings
$from_email = '[email protected]';
$to_email = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email';

// Create a new SMTP connection
$fp = fsockopen($smtp_server, $smtp_port, $errno, $errstr, 30);
if (!$fp) {
    echo "Error: $errstr ($errno)";
    exit;
}

// Send the EHLO command
fputs($fp, "EHLO\r\n");
$response = fgets($fp, 256);
echo $response;

// Authenticate with the SMTP server
fputs($fp, "AUTH LOGIN\r\n");
$response = fgets($fp, 256);
echo $response;
fputs($fp, base64_encode($smtp_username). "\r\n");
$response = fgets($fp, 256);
echo $response;
fputs($fp, base64_encode($smtp_password). "\r\n");
$response = fgets($fp, 256);
echo $response;

// Send the MAIL FROM command
fputs($fp, "MAIL FROM: <". $from_email. ">\r\n");
$response = fgets($fp, 256);
echo $response;

// Send the RCPT TO command
fputs($fp, "RCPT TO: <". $to_email. ">\r\n");
$response = fgets($fp, 256);
echo $response;

// Send the DATA command
fputs($fp, "DATA\r\n");
$response = fgets($fp, 256);
echo $response;
fputs($fp, "Subject: ". $subject. "\r\n\r\n". $message. "\r\n.");
$response = fgets($fp, 256);
echo $response;

// Send the QUIT command
fputs($fp, "QUIT\r\n");
$response = fgets($fp, 256);
echo $response;

// Close the socket
fclose($fp);
?>

This script uses the fsockopen() function to create a new socket connection to the SMTP server, and then sends the necessary commands to authenticate and send the email. Note that this script assumes that the SMTP server uses the AUTH LOGIN authentication method, and that the username and password are sent in base64 encoded format.

Also, this script does not handle any errors that may occur during the email sending process. You may want to add error handling code to make the script more robust.

It's worth noting that using the mail() function with SMTP authentication can be more complex and error-prone than using a library like PEAR, and may not work with all SMTP servers. If you're planning to send a large volume of emails or need more advanced features, you may want to consider using a dedicated email sending library or service.