note 62310 rejected from ref.mail by colder
| From: | colder@php.net | Date: | Wed, 31 May 2006 17:54:58 +0000 |
| Subject: | note 62310 rejected from ref.mail by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-112665@lists.php.net to get a copy of this message | ||
Note Submitter: Antony Male
----
Woo... my first ever contribution to the php community.... anyway.. ahem
I recently set up a linux server, and really couldn't be bothered to set up sendmail, qmail,
postfix, or whatever. I was therefore looking for a script that would take the place of sendmail,
and telnet/whatever, into an smtp server, and seliver my mail :D
However, I couldn't find anything.. Time to make one myself.
The page below is partially based on iain at monitormedia dot co dot uk's another_mail
function, but edited, and with my own bit (grab_stuff) added.
It behaves in pretty much the same way as sendmail. However, it doesn't take any flags
You should set your sendmail_path in php.ini to the location of this file, and set the shebang to
the appropiate location.
I've used smtp.wanadoo.co.uk, as its the only smtp server i'm allowed to use. However,
feel free to change this. You may have to change fputs($cp, "MAIL FROM: <$from>\n")
and fputs($cp, "RCPT TO: <$to>\n") to fputs($cp, "MAIL FROM: $from\n") and
fputs($cp, "RCPT TO: $to\n") if you change the smtp server, however...
#!/usr/local/bin/php -q
<?php
function grab_stuff () {
$r=array();
$r['headers']=''; //user .= later, so need to declase
$r['data']=''; //ditto
$fp = fopen('php://stdin','r');
while ($line = fgets($fp)) {
$line = trim($line);
if (strpos($line,'To: ')!==false)
$r['to'] = str_replace('To: ','',$line);
elseif (strpos($line,'Subject: ')!==false)
$r['subj'] = str_replace('Subject: ','',$line);
elseif (strpos($line,':')!==false)
$r['headers'].= $line;
elseif ($line) //message body
$r['data'].= $line;
}
fclose($fp);
return $r;
}
function mymail($to,$subject,$message,$headers)
{
// Open an SMTP connection
$cp = fsockopen ("smtp.wanadoo.co.uk", 25, &$errno, &$errstr, 1);
if (!$cp)
return "Failed to even make a connection;
$res=fgets($cp,256);
if(substr($res,0,3) != "220") return "Failed to connect";
// Say hello...
fputs($cp, "HELO wanadoo\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Failed to Introduce";
// Mail from...
fputs($cp, "MAIL FROM: <$from>\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "MAIL FROM failed";
// Rcpt to...
fputs($cp, "RCPT TO: <$to>\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "RCTP TO failed";
// Data...
fputs($cp, "DATA\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "354") return "DATA failed";
// Send To:, From:, Subject:, other headers, blank line, message, and finish
// with a period on its own line (for end of message)
fputs($cp, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "250") return "Message Body Failed";
// ...And time to quit...
fputs($cp,"QUIT\n");
$res=fgets($cp,256);
if(substr($res,0,3) != "221") return "QUIT failed";
return true;
}
$r = grab_stuff();
mymail($r['to'],$r['subj'],$r['data'],$r['headers']);
?>