note 62310 added to ref.mail
| From: | AntonyMale at osu1 dot php dot net | Date: | Fri, 24 Feb 2006 21:10:05 +0000 |
| Subject: | note 62310 added to ref.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-104381@lists.php.net to get a copy of this message | ||
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']);
?>
----
Server IP: 84.18.202.180
Probable Submitter: 81.77.29.231
----
Manual Page -- http://www.php.net/manual/en/ref.mail.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+62310
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62310&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+62310&report=yes
Search -- http://master.php.net/manage/user-notes.php