Ok, this is my standard rant about sending email from scripts, being a former system administrator of a mail system for 40.000+ users. If you are using this in anything else than a very trivial testing-only environment, 'mailhost
' in Jenda
's example above should be set to 'localhost
' and you should run an SMTP daemon on your localhost. This is for reasons of error handling and redundancy. This is also true if you are running this in a cgi script on a webserver. You want to send the email as quickly as possible to be able continue with the cgi script, and sending the mail to localhost is a lot quicker than over the network.
If you are not able to run an SMTP daemon on localhost, you can use it the way Jenda describes above, if mailhost is close to you, and under your control. This is a riskier solution, and will not take advantage of MX records (think sendmail 'smarterhost' set to an MX record of mailhost1, mailhost2 & mailhost3 (or, as in my case, virusscanner1, virusscanner2, virusscanner3...)).
Anyway, always check that new creates a valid $smtp object. If you get undefined or something, the connection with mailhost could not be established, and you will have to deal with that.
Don't even think about sending the mail directly to the recipient's mailhost. You won't be able to deal with spooling, error handling, DNS resolving and cacheing, MX records etc. as efficiently as sendmail (or other MTA's).