March 14th, 2006

PHP: socket_mail() – an alternative to mail()

If your host has disabled the nobody account (a common practice to prevent spamming), you won't be able to send emails by using the PHP mail() function anymore. This is an alternative function, called socket_mail()


<?php

$subject = 'test email';
$message = 'this is a test';
$to = array("name <name@email.com>", "name2 <name2@email.com>");

socket_mail($to, $subject, $message);

function socket_mail($toArray, $subject, $message) {

  // Setup
  $fromName = "your name";
  $fromEmail = "your admin email address";
  $fromMailer = "Socketmail v2.0";
  $smtp = "localhost";
  $smtp_port = 25;
  $charset = "ISO-8859-1";

  // Strip "\r" from the message (if it came from a form input)
  $message = str_replace(chr(13), "", $message);

  // Add a message signature (optional)
  $message .= "\n\n".str_repeat("_", 60)."\n";
  $message .= "$fromName <$fromEmail>\n";

  $message = str_replace("\r\n.", "\r\n..", str_replace("\n", "\r\n", stripslashes($message))." \r\n");

  ini_set(sendmail_from, $fromEmail);

  $connect = @fsockopen ($smtp, $smtp_port, $errno, $errstr, 5);
    if (!$connect) return false;
    $rcv = fgets($connect, 1024); 

  fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n");
    $rcv = fgets($connect, 1024); 

  foreach ($toArray as $to) {

    $toBits = explode(" ", $to);
    $toRcpt = trim($toBits[count($toBits) - 1], "<> ");

    fputs($connect, "RSET\r\n");
      $rcv = fgets($connect, 1024);

    fputs($connect, "MAIL FROM:$fromEmail\r\n");
      $rcv = fgets($connect, 1024);
    fputs($connect, "RCPT TO:$toRcpt\r\n");
      $rcv = fgets($connect, 1024);
    fputs($connect, "DATA\r\n");
      $rcv = fgets($connect, 1024);

    fputs($connect, "Subject: $subject\r\n");
    fputs($connect, "From: $fromName <$fromEmail>\r\n");
    fputs($connect, "To: $to\r\n");
    fputs($connect, "X-Sender: <$fromEmail>\r\n");
    fputs($connect, "Return-Path: <$fromEmail>\r\n");
    fputs($connect, "Errors-To: <$fromEmail>\r\n");
    fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $fromName)."@$smtp>\r\n");
    fputs($connect, "X-Mailer: PHP - $fromMailer\r\n");
    fputs($connect, "X-Priority: 3\r\n");
    fputs($connect, "Date: ".date("r")."\r\n");
    fputs($connect, "Content-Type: text/plain; charset=$charset\r\n");
    fputs($connect, "\r\n");
    fputs($connect, $message);

    fputs($connect, "\r\n.\r\n");
      $rcv = fgets($connect, 1024);
  }

  fputs ($connect, "QUIT\r\n");
    $rcv = fgets ($connect, 1024);
  fclose($connect);
  ini_restore(sendmail_from);
  return true;
}
?>
Share

3 Responses to ' PHP: socket_mail() – an alternative to mail() '

Subscribe to comments with RSS or TrackBack to ' PHP: socket_mail() – an alternative to mail() '.

  1. liza said,

    on March 25th, 2010 at 3:12 am

    hi i need contact form of php without database.
    i have 8 field but in mail function we could not taken more than five.so please help me

  2. quimkaos said,

    on February 28th, 2011 at 3:30 pm

    anyway of doing this with autentication?


  3. on August 31st, 2011 at 12:07 am

    Awesome that you posted it. After 5 years, though, does this code still work? And if so, how?

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word