Help centre

PHP Mail Script with SMTP Authentication

Help article from the Fast2Host knowledgebase

PHP Email Script with SMTP Authentication

The following example demonstrates how to send an email in PHP using SMTP authentication. This is useful for enquiry forms or any application where you need reliable email delivery.

Sample Script

<?php
// Basic usage
$to = "to@fast2host.com";
$nameto = "Recipient Name";
$from = "from@fast2host.com";
$namefrom = "Sender Name";
$subject = "Hello World Again!";
$message = "World, Hello!";

authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);

// Function to send email with SMTP authentication
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message) {
    // SMTP + Server Details
    $smtpServer = "mail.YOUR-DOMAIN.com";
    $port = 25;
    $timeout = 30;
    $username = "your-email@domain.com";
    $password = "your-email-password";
    $localhost = "mail.YOUR-DOMAIN.com";
    $newLine = "\r\n";

    // Connect to SMTP server
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    $smtpResponse = fgets($smtpConnect, 515);

    if(empty($smtpConnect)) {
        return "Failed to connect: $smtpResponse";
    }

    // Authenticate
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    fgets($smtpConnect, 515);
    fputs($smtpConnect, base64_encode($username) . $newLine);
    fgets($smtpConnect, 515);
    fputs($smtpConnect, base64_encode($password) . $newLine);
    fgets($smtpConnect, 515);

    // Say Hello
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    fgets($smtpConnect, 515);

    // Mail From / RCPT To
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
    fgets($smtpConnect, 515);
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
    fgets($smtpConnect, 515);

    // Send Data
    fputs($smtpConnect, "DATA" . $newLine);
    fgets($smtpConnect, 515);

    $headers = "MIME-Version: 1.0" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "To: $nameto <$to>" . $newLine;
    $headers .= "From: $namefrom <$from>" . $newLine;

    fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
    fgets($smtpConnect, 515);

    // Quit
    fputs($smtpConnect,"QUIT" . $newLine);
    fgets($smtpConnect, 515);
}
?>
    

Configuration Notes

  • $smtpServer: Replace with your mail server (e.g., mail.yourdomain.com).
  • $username / $password: Use your full email address and password.
  • $port: Default is 25. Use 587 or 465 if SSL/TLS is required.
  • $localhost: Typically your domain name or mail server hostname.

Best Practices

  • Use authenticated SMTP for reliable delivery.
  • Consider using PHPMailer or SwiftMailer for modern, secure email handling.
  • Always test your script with a valid recipient before deploying live.
  • Check if your hosting requires SSL/TLS and adjust port settings accordingly.
✔ Tip: For modern PHP projects, libraries like PHPMailer provide easier configuration, built-in error handling, and support for secure protocols.

Was this article helpful?

Views: 31889

Cambridge Data Centre

Still need help?

Talk to a UK engineer — we'll help with hosting, servers, billing or migrations with no sales pressure.