Re: Trying to send mail from PHP with SMTP ?
| From: | Michael Samuel | Date: | Tue, 16 Nov 1999 22:13:06 +0000 |
| Subject: | Re: Trying to send mail from PHP with SMTP ? | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-12635@lists.php.net to get a copy of this message | ||
On Tue, Nov 16, 1999 at 05:49:54PM +0500, Mukul Gandhi wrote:
> $from = "MAIL FROM:<abc@pqr.com>";
> $to = "MAIL TO:<mgandhi@bhartitelesoft.com>";
That should be RCPT TO:
> $from = htmlspecialchars($from);
> $to = htmlspecialchars($to);
> $mesg = "Just a test message";
> $fp = fsockopen("smtpserver",25);
> if(!$fp) {
> echo htmlspecialchars("$errstr ($errno)<br>\n");
> } else
> {
> fputs($fp,"HELO mailserver");
> fputs($fp,$from);
> fputs($fp,$to);
> fputs($fp,"DATA");
> fputs($fp,$mesg);
> fputs($fp,"QUIT");
> }
You need to read the data back from the SMTP server. (eg. check the replies)
If you fail to do so, the SMTP server will most likely just block until you
do (or until it times out).
It will respond with a single line, 5xx for perm error, 4xx for temp error,
and 2xx for success. ('xx' being any 2 numbers, the string following them
should be enough to make those 2 numbers insignificant)
Also, $msg should be deliminated with "\r\n" (no bare newlines or carriage
returns; they come in pairs always), and it should finish with a line with a
fullstop on it's own (eg. "\r\n.\r\n"; the first \r\n is the end of the last
line of the message)
> I know mail() function of PHP can do this. But I am unable to work it the
> above way.
SMTP is a simple protocol, but be careful; some mistakes will be tolerated
by some mail servers (typically running Sendmail), while others will just
reject the message, rather than risk corrupting the message.
Re-using builtin functions is a good way of doing things, if possible,
otherwise, see if somebody's writting a library that does what you want; code
re-use is good.