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 AssignStream Using SendMail


November 2006 Update:

Seems that this code below used to work, but now it doesn't ( fpc 2.0.4???):
  if AssignStream(SIN, SOUT, SENDMAIL_PATH, Args) <> -1 then
That needs to be changed to:
  if AssignStream(SIN, SOUT, SENDMAIL_PATH, [Args[0]]) <> -1 then
Or
var
  arg1: string;
begin
  Arg1:= 'YourEmail@hotmail.com'; //your crappy hotmail address
  ...
  if AssignStream(SIN, SOUT, SENDMAIL_PATH, [Arg1]) <> -1 then   
I don't know why the compiler accepts this and then sendmail is broken:
  if AssignStream(SIN, SOUT, SENDMAIL_PATH, Arg1) <> -1 then   
Note square brackets and lack of. When the square brackets are skipped sendmail is broken. It could be because an overloaded assignstream, or some other issue is causing a problem.

August 2006 Update:

If you have any problems with sendmail, find an existing email that worked and view the source of the email. Run sendmail -t address@domain.com and paste the email source into the command line. Make sure the email is to yourself so you can test it.

Since the source code of a working existing email will be pasted in, sendmail should have no trouble sending the email. This can help you troubleshoot sendmail since some servers expect an email to have perfect or better form than what you've currently configured.


Using the method below, you can run the sendmail program and send an email. To connect to any mail server, and not just sendmail, please see FreePascal Sockets Connect To SendMail. Using the methods mentioned below, you are targetting the sendmail program specifically, not any mail server in general.
Most servers use sendmail as a default way to send mail, and most servers will have sendmail installed (linux ones.. on windows, there might only be sendmail emulators or no sendmail program at all).
Also note that AssignStream is used on linux, but not MS Windows. AssignStream uses linux piping functions, which MS Windows does not have available. But this page is discussing SendMail, which is used on Linux machines, so we won't go into those details about MS Windows piping.
procedure WebSendMail;
var
  SIN,
  SOUT:text;
  Arg1: string;
begin

  Arg1:= 'YourEmail@hotmail.com'; //your crappy hotmail address

  if AssignStream(SIN, SOUT, SENDMAIL_PATH, [Arg]]) <> -1 then
  begin
    writeln(sout, 'This is a test usinging assignstream and sendmail');
    writeln(sout, '.'); //end connection
  end else
    writeln('AssignStream returned error -1');

  close(sin);
  close(sout);
end;


procedure WebSendMail;
var
  SIN,
  SOUT:text;
  Arg1: string;
begin
  setlength(args, 1);
  Arg1:= ' -t ';    //tells sendmail to parse for TO, FROM, CC, BCC in text

  if AssignStream(SIN, SOUT, SENDMAIL_PATH, [Arg1]) <> -1 then
  begin
    // Insert a legitimate email in the FROM, incase the server does not allow 
    // invalid emails. Usually it will allow any email, but use a legitimate one set 
    // up on your computer domain name just incase.
    writeln(sout, 'FROM: ' + 'LocalEmail@yourdomain.com'); 
    writeln(sout, 'TO: ' + 'YourEmail@yahoo.com'); //your crappy yahoo test address
    writeln(sout, 'SUBJECT: ' + 'YourSubject');
    writeln(sout, 'This is a test message number 2, using assignstream and sendmail');
    writeln(sout, '.'); //end connection
  end else
    writeln('AssignStream returned error -1');

  close(sin);
  close(sout);
end;



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