If you're on a nix box which doesn't have an MTA with Sendmail wrappers (which is pretty unlikely but sounds like what you're talking about) you're going to have to manually open up a socket to your smtp server and send the mail like that. This may sound like a long winded way of doing things but if you're sending out a large number of emails it's a lot* quicker. I've knocked up a very skeletal class to handle this kind of thing. There's no commenting, no error handling and no debuging output but I can't go spoiling you can I.
If you need any help with it just ask.
<?php
class SMTPDriver {
var $server;
var $ip;
var $port;
var $timeout;
var $cnx;
var $errmsg;
var $errno;
function SMTPDriver($server, $ip, $port=25, $timeout=86400) {
if(''==$server || ''==$ip || !is_numeric($port) || !is_numeric($timeout)) {
} else {
$this->server=$server;
$this->ip=$ip;
$this->port=$port;
$this->timeout=$timeout;
}
}
function connect() {
$tm=time();
for($i=0; $i<10; $i++) {
if(!$this->cnx = fsockopen($this->server, $this->port, &$this->errno, &$this->errmsg)) {
while(time()<$tm+10) {}
$tm=time();
} else {
break;
}
}
if(!$this->cnx) {
return false;
} else {
return true;
}
}
function send($to, $from, $subject, $txtmsg, $htmlmsg=false) {
$email= "Date: ".date('r')."\r\n".
"To: %s\r\n".
"From: $from\r\n".
"Subject: $subject\r\n".
"MIME-Version: 1.0\r\n";
if($htmlmsg===false) {
$email.= "Content-Type: text/plain; charset=iso-8859-1\r\n".
"\r\n$textmsg\r\n";
} else {
$email.="Content-Type: multipart/alternative; boundary=sEcTiOn-BoUnDaRy\r\n".
"\r\n--sEcTiOn-BoUnDaRy\r\n\r\n".
"Content-Type: text/plain; charset=iso-8859-1\r\n".
"\r\n$textmsg\r\n".
"--sEcTiOn-BoUnDaRy\r\n".
"Content-Type text/html; charset=iso-8859-1\r\n".
"\r\n$htmlmsg\r\n".
"--sEcTiOn-BoUnDaRy--\r\n";
}
$email.=".\r\n";
if(!$this->helo()) {
return false;
}
if(!is_array($to)) {
$to=array($to);
}
for($i=0, $to_c=count($to); $i<$to_c; $i++) {
if(!$this->from($from)) {
if(!$this->rset()) return false;
continue;
}
if(!$this->rcpt($to[$i])) {
if(!$this->rset()) return false;
continue;
}
if(!$this->data()) {
if(!$this->rset()) return false;
continue;
}
if(!$this->mailit(sprintf($email, $to[$i])) {
if(!$this->rset()) return false;
continue;
}
}
if(!$this->kwit()) return false;
fclose($this->cnx);
return true;
}
function sendMessage($msg, $retchk='250') {
if(!fputs($this->cnx, $msg)) {
return false;
}
if(!$response=fgets($this->cnx, 1024)) {
return false;
}
if(substr($response, 0, 3)!=$retchk) {
return false;
}
return true;
}
function helo() {
return $this->sendMessage("HELO $this->ip\r\n");
}
function from($from) {
return $this->sendMessage("MAIL FROM:<$from>\r\n");
}
function rcpt($to) {
return $this->sendMessage("RCPT TO:<$to>\r\n");
}
function data() {
return $this->sendMessage("DATA\r\n", '354');
}
function rset() {
return $this->sendMessage("RSET\r\n");
}
function kwit() {
return $this->sendMessage("QUIT\r\n", '221');
}
function ehlo() {
return $this->sendMessage("EHLO\r\n");
}
} //end class
//Here's an example of how one might use it
$to=array('addr1@domain.com', 'addr2@domain.com', 'addr3@domain.com');
$from='fromaddr@domain.com';
$server='mail.domain.com';
$ip='111.111.111.111';
$subject="Here's a test mail";
$text='Here\'s the text part';
$html='<html><body><h1>Here\'s the html part</h1></body></html>';
$mailer=new SMTPDriver($server, $ip);
if($mailer->connect()) {
if(!$mailer->send($to, $from, $subject, $text, $html)) {
echo("Failed to send mails, although some may have gone");
} else {
echo("All the mails went ok");
}
} else {
echo("Couldn't even connect");
}
?>