Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email in Delphi without smtp and using php function at server

Tags:

php

email

delphi

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: https://www.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.

like image 579
volvox Avatar asked Oct 16 '25 10:10

volvox


1 Answers

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.

like image 157
Greg Avatar answered Oct 18 '25 07:10

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!