PHP Code

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("/&#91;^a-z0-9&#93;/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 Comments

  1. 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. anyway of doing this with autentication?

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*
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.
Anti-spam image