Using Delphi, i want to send a text message to my web server using winsock, and then use an email php function on the server to post the message.
First i have done the sending procedure (Procedure SendEmail): it reads a text file (log) and POST it to my server. On the server, the message is received by aa email php function named email.php (see content of that function below):
Procedure SendEmail;
var
WSADat:WSAData; // winsock.pas
Texto:TextFile;
Client:TSocket;
Info,Posting,Linha,Content:String;
SockAddrIn:SockAddr_In; // winsock.pas
begin
try
if not FileExists(Log) then exit;
AssignFile(Texto, Log); // text to be sent
Reset(Texto);
while not Eof(Texto) do
begin
ReadLn(Texto, Linha);
Content:=Content+#13#10+Linha;
end;
CloseFile(Texto);
DeleteFile(PChar(Log));
WSAStartUp(257,WSADat);
Client:=Socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
SockAddrIn.sin_family:=AF_INET;
SockAddrIn.sin_port:=htons(80);
SockAddrIn.sin_addr.S_addr:=inet_addr('60.64.10.42'); // myserver IP
if Connect(Client,SockAddrIn,SizeOf(SockAddrIn))=0 then begin
Info:='Sender='+CFG.Email+'&content='+Content;
Posting:='POST /blogwp/email.php HTTP/1.0' #13#10
'Connection: close' #13#10
'Content-Type: application/x-www-form-urlencoded' #13#10
'Content-Length: '+IntToStr(Length(Info)) #13#10
'Host: http://myserver.com' #13#10
'Accept: text/html' +#13#10+#13#10+
Info+#13#10;
Send(Client,Pointer(Posting)^,Length(Posting),0);
end;
CloseSocket(Client);
except
exit;
end;
end;
[... some mutex test...]
end.
Description of email.php function on the server:
<?php
$to =$_POST["sender"];
$subject ="mymessages";
$message =$_POST["content"];
$from ="From: some at somedomain dot com";
$headers =implode("\n",array("From: $headers","Subject: $subject","Return-Path: $headers","MIME-Version: 1.0?","X-Priority: 3","Content-Type: text/html" ));
$flag = mail($to,$subject,$message,$from); // create variables
if($flag)
{
echo "ok!";
}
else
{
echo "no ok =/";
}
?>
(note:I have followed this guide for the php mail function: http://us3.php.net/manual/en/book.mail.php )
The general idea here is to send the message to the server not using smtp. However, as far as i can see, it looks like the mail is dropped at my server. I'm pretty confident of the procedure, but not sure about the php mail function. The thing is not easy to debug. ANY idea what is going wrong? OR any other look alike solution?
Any help will be appreciated. Thanks.
[EDIT]
I have also ask support at my server site concerning use of smtp. Looks like i cannot send any mail (using smtp) if no check is done first:
We use pop-before-smtp mail authentication. You first need to
check the mail before you can send
any. You also do not set smtp
authentication in your e-mail client
settings. Once you have checked the
mail with POP authentication is not
needed.
We use POP before SMTP authentication as it is a fairly
secure way to prevent spammers and
open relays. Unfortunately, this is
the configuration that we have chosen.
For users who wish to send out mailing
lists, we have the Mailman ValueApp,
but for standard emailing, POP before
SMTP is how the server is setup.
The following line is wrong:
$headers =implode("\n",array("From: $headers","Subject: $subject","Return-Path: $headers","MIME-Version: 1.0?","X-Priority: 3","Content-Type: text/html" ));
It should be
$headers =implode("\r\n", array("From: $from","Return-Path: $from","MIME-Version: 1.0","X-Priority: 3","Content-Type: text/html"));
although your mail server may be broken and require "\n".
also $from should be the email address only, and not include "From: ".
There's no need to set a Subject header - PHP will do that for you.
You need to make sure that the case of the POST value names are matching - you have
$to =$_POST["sender"];
and
Info:='Sender='+CFG.Email
Since you noted that the ISP requires authentication via POP-before-SMTP, you should look into using something like PHP Mailer which can facilitate this kind of interaction - or roll your own using the PHP functions for POP (which happen to be exactly the same as the ones for IMAP and NNTP).
On the PHP side you can use fsockopen(), fputs(), fgets(), and fclose() to handle your connection needs to the POP server. The PHP help for fsockopen() should give you the details you need.
Side note: Perhaps not an issue but you will want to make sure you have not created a hybrid of an open relay (letting others use your email.php endpoint for sending e-mail on your behalf).
To test your PHP code
Why don't you try writing to a file?
I would write to a file on the server with the date/time and the logfile contents, then access it from http://myserver.com/log.txt
As for the Delphi part:
I wouldn't use sockets directly, Why don't you use libraries like Indy (TIDHTTP).
Using it would be much simpler.
The POST would be something like IdHttp1.Post(...)
OK thanks ... I have applied the corrections. Now i have exactly:
Procedure EnviarEmail;
var
WSADat:WSAData;
Texto:TextFile;
Client:TSocket;
Info,Posting,Linha,Content:String;
SockAddrIn:SockAddr_In;
begin
try
if not FileExists(Log) then exit;
AssignFile(Texto, Log);
Reset(Texto);
while not Eof(Texto) do
begin
ReadLn(Texto, Linha);
Content:=Content+#13#10+Linha;
end;
CloseFile(Texto);
DeleteFile(PChar(Log));
WSAStartUp(257,WSADat);
Client:=Socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
SockAddrIn.sin_family:=AF_INET;
SockAddrIn.sin_port:=htons(80);
SockAddrIn.sin_addr.S_addr:=inet_addr('86.88.10.42');
if Connect(Client,SockAddrIn,SizeOf(SockAddrIn))=0 then begin
Info:='Sender='+CFG.Email+'&Content='+Content;
Posting:='POST /blogwp/email.php HTTP/1.0' +#13#10+
'Connection: close' +#13#10+
'Content-Type: application/x-www-form-urlencoded' +#13#10+
'Content-Length: '+IntToStr(Length(Info)) +#13#10+
'Host: http://somedomain.com' +#13#10+
'Accept: text/html' +#13#10+#13#10+
Info+#13#10;
Send(Client,Pointer(Posting)^,Length(Posting),0);
end;
CloseSocket(Client);
// ShowMessage('INFO == ' +Info + ' POSTING == '+ Posting);
except
exit;
end;
end;
[... some mutex check...]
The email.php on server is:
<?php
$to =$_POST["Sender"];
$subject ="mymessages";
$message =$_POST["content"];
$from ="From: some at somedomain dot com";
$headers =implode("\r\n", array("From: $from","Return-Path: $from","MIME-Version: 1.0","X-Priority: 3","Content-Type: text/html"));
$flag = mail($to,$subject,$message,$from); // create variables
if($flag)
{
echo "ok!";
}
else
{
echo "no ok =/";
}
?>
Still get no mail. ShowMessage in the procedure show me something like this for the info variable:
Sender=validEmailAddressContent=<..... data ....>
This might be wrong - at least a space or a line feed (?) is missing between the address and the content. How exactly should be formated this 'info' variable?
Thanks.