Source Code

Example of a PHP script that sends an email using fsockopen function

This script handles the SMTP communication manually, which can be useful for understanding the underlying protocol but is generally more complex and error-prone compared to using a library like PHPMailer.

Here's a simplified example that sends a plain text email:

// Email information
$to = 'recipient@example.com'; // The recipient's email address
$subject = 'Test Email'; // Subject of the email
$message = 'Hello, this is a test email sent from a PHP script using SMTP.'; // Message body
$from = 'your-email@example.com'; // Sender email

// SMTP server configuration
$smtpHost = 'smtp.example.com'; // Your SMTP host
$smtpPort = 25; // SMTP port (commonly 25, 465 for SSL, or 587 for TLS)
$smtpUser = 'your-username'; // SMTP username
$smtpPass = 'your-password'; // SMTP password

// Open an SMTP connection
$smtp = fsockopen($smtpHost, $smtpPort, $errno, $errstr, 30);
if (!$smtp) {
    echo "Could not connect to SMTP host: $errstr ($errno)\n";
    exit;
}

// Function to send data to the server
function sendCommand($smtp, $cmd) {
    fputs($smtp, $cmd . "\r\n");
    $response = fgets($smtp, 512);
    if (substr($response, 0, 3) != '250' && substr($response, 0, 3) != '235') {
        echo "SMTP error: $response";
        exit;
    }
}

// Read greeting
fgets($smtp, 512);

// Initiate SMTP conversation
sendCommand($smtp, "EHLO " . $smtpHost);
sendCommand($smtp, "AUTH LOGIN");
sendCommand($smtp, base64_encode($smtpUser));
sendCommand($smtp, base64_encode($smtpPass));

// Email sending commands
sendCommand($smtp, "MAIL FROM: <$from>");
sendCommand($smtp, "RCPT TO: <$to>");
fputs($smtp, "DATA\r\n");
fgets($smtp, 512); // Read response
fputs($smtp, "Subject: $subject\r\n");
fputs($smtp, "To: $to\r\n");
fputs($smtp, "From: $from\r\n");
fputs($smtp, "\r\n"); // End of headers
fputs($smtp, "$message\r\n"); // Message body
fputs($smtp, ".\r\n"); // End of message
fgets($smtp, 512); // Read response

// Quit and close connection
sendCommand($smtp, "QUIT");
fclose($smtp);

echo "Email sent successfully.";
?>

Important Notes

1. SMTP Commands: The script sends raw SMTP commands to the server and reads the responses. Each command is followed by a response check to ensure the server processed the command without error.

2. Security: Sending emails using this method exposes your SMTP credentials in base64 encoding, which is not encrypted and only obfuscates the content. Always ensure that your connection is secure and possibly use an encrypted connection if the SMTP server supports it.

3. Error Handling: The script includes basic error handling that will stop execution if an SMTP command fails (does not return a 250 or 235 status code). More sophisticated error handling might be needed depending on the application requirements.

4. Email Headers: This script sends minimal headers. For more complex emails, such as those with HTML content, attachments, or additional headers, you would need to construct the headers accordingly.

5. Library Use: Despite this example, for most practical and production applications, using a robust library like PHPMailer is recommended as it handles many complexities and potential security issues for you.

This example demonstrates the basics of manually handling SMTP communication. However, ensure that you handle any errors and exceptions properly in real-world applications to avoid issues like email delivery failures or security vulnerabilities.