A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

FreePascal Sockets Connect To SendMail


Tested on linux. It may work on BSD. For Windows, you must patch your sockets unit first before using or use my patched sockets unit. See below for more information.

It connects to send email using port 25 and let's your program talk to sendmail, or another email server.


How to use SendMail the actual executable program, without using sockets?
See FreePascal AssignStream Using SendMail

Using the method below, you can connect to any mail server, not just SendMail. This is because we aren't launching the sendmail program here. Instead, we are connecting to the mail server and talking to it - and all mail servers should offer the same communication language.


Read up on EHLO, HELO, DATA, QUIT, and those telnet commands for the mail server if you are not sure about them.
If you are looking at this page because you are interested in using SendMail for web programming purposes (websites, CGI, etc.), remember that there are functions already made for you in the Pascal Web Unit Project, which handle email through SendMail or generic mail servers.

For example, this unit wraps SendMail nicely: http://opensvn.csie.org/pspcgi/psp-1.6.x-devel/main/sendmail_unix.pas


Note: for below program to work on Windows, you may have to patch your sockets unit.. see this bug report and a temporary patch for windows Sockets.pp unit. This is an FPC bug regarding the writeln and readln drivers for simple sockets access. At least I had many problems with the windows write/read sockets functions in FPC 2.0.0 - 2.0.4 and the bug still isn't fixed according to bug report.
program Project1;
{$mode objfpc}{$H+}
uses
  sockets, ERRORS; // use sockets_patched on Windows
  
var
  sin,        // STANDARD IN
  sout: text; // STANDARD OUT
  socketABC:longint;
  addr: TInetSockAddr;
  line: string;

begin
  Addr.family:=AF_INET;
  addr.port := 25 shl 8;
  addr.addr := ((1 shl 24) or 127);
  socketABC:= socket(AF_INET, SOCK_STREAM, 0);
  if not Connect (socketABC,ADDR,SIN,SOUT) then
  begin
    Writeln ('Couldn''t connect to localhost');
    Writeln ('Socket error : ', strerror(SocketError));
    halt(1);
  end else
    Writeln ('Connected to localhost');
  rewrite (sout);
  reset(sin);
  writeln(sout, 'HELO YourDomainName');
  writeln(sout, 'QUIT');
  flush(sout);
  while not eof(sin) do
  begin
    readln (Sin,line);
    writeln (line);
  end;
  close(sin);
  close(sout);
  writeln ('hit enter to exit');
  readln;
end.


Note: if you don't have sendmail installed, then
 apt-get install sendmail

Do you want to send an email to an address to try sendmail?
program Project1;

{$mode objfpc}{$H+}

uses
  sockets, ERRORS; // use sockets_patched on Windows
  
var
  sin,        // STANDARD IN
  sout: text; // STANDARD OUT
  socketABC:longint;

  addr: TInetSockAddr;
  line: string;

begin
  Addr.family:=AF_INET;
  addr.port := 25 shl 8;
  addr.addr := ((1 shl 24) or 127);
  socketABC:= socket(AF_INET, SOCK_STREAM, 0);
  if not Connect (socketABC,ADDR,SIN,SOUT) then
  begin
    Writeln ('Couldn''t connect to localhost');
    Writeln ('Socket error : ', strerror(SocketError));
    halt(1);
  end else
    Writeln ('Connected to localhost');

  rewrite (sout);
  reset(sin);
  writeln(sout, 'HELO YourDomain');
  writeln(sout, 'MAIL FROM: test@test.com'); //this can be anything
  writeln(sout, 'RCPT TO: YourEmail@hotmail.com'); //your crappy hotmail address
  writeln(sout, 'DATA'); 
  writeln(sout, 'Testing email: hi self, how are you?');  //email text
  writeln(sout, '.');    //end of message always ends with dot
  writeln(sout, 'QUIT'); //ends connection
  flush(sout);
  while not eof(sin) do
  begin
    readln (Sin,line);
    writeln (line);
  end;

  close(sin);
  close(sout);

  writeln('hit enter to exit');
  readln;
end.


The above programs have been successfully tested on linux. You should have a mail server installed as you can't use the above code without a mail server. Make sure your firewall/ISP are not blocking port 25. Some ISP's block port 25.

About
This site is about programming and other things.
_ _ _